답안 #458289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
458289 2021-08-08T03:58:09 Z izhang05 Džumbus (COCI19_dzumbus) C++17
50 / 110
67 ms 18372 KB
#include <bits/stdc++.h>

using namespace std;

//#define DEBUG
void setIO(const string &name) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin.exceptions(istream::failbit);
//#ifdef LOCAL
//    freopen((name + ".in").c_str(), "r", stdin);
//    freopen((name + ".out").c_str(), "w", stdout);
//    freopen((name + ".out").c_str(), "w", stderr);
//#endif
}

const int inf = 0x3f3f3f3f, mod = 1e9 + 7, maxn = 1e3 + 5;
const long long INFL = 0x3f3f3f3f3f3f3f3f;
long long d[maxn];
bool vis[maxn];
vector<int> adj[maxn];
vector<long long> dp[maxn][3];

vector<long long> ad(vector<long long> a, vector<long long> b) {
    vector<long long> res(a.size() + b.size() - 1, INFL);
    for (int i = 0; i < (int) a.size(); ++i) {
        for (int j = 0; j < (int) b.size(); ++j) {
            res[i + j] = min(res[i + j], a[i] + b[j]);
        }
    }
    return res;
}

vector<long long> mn(vector<long long> a, vector<long long> b) {
    while (a.size() < b.size()) {
        a.push_back(INFL);
    }
    for (int i = 0; i < b.size(); ++i) {
        a[i] = min(a[i], b[i]);
    }
    return a;
}


void dfs1(int c, int p) {
    vis[c] = true;
    for (auto &i : adj[c]) {
        if (i != p) {
            dfs1(i, c);
        }
    }
}

void dfs2(int c, int p) {
    dp[c][0] = {0};
    dp[c][1] = {INFL, d[c]};
    for (auto &i : adj[c]) {
        if (i != p) {
            dfs2(i, c);
            dp[c][0] = ad(dp[c][0], mn(dp[i][0], dp[i][2]));
            dp[c][2] = mn(ad(dp[c][2], mn(mn(dp[i][0], dp[i][1]), dp[i][2])), ad(dp[c][1], mn(dp[i][1], dp[i][2])));
            dp[c][1] = ad(dp[c][1], dp[i][0]);
        }
    }
}


int main() {
    setIO("6");

    int n, m;
    cin >> n >> m;
    ++n;
    d[0] = INFL;
    for (int i = 1; i < n; ++i) {
        cin >> d[i];
    }
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    // connect roots of all forests to node 0
    for (int i = 1; i < n; ++i) {
        if (!vis[i]) {
            dfs1(i, -1);
            adj[0].push_back(i);
            adj[i].push_back(0);
        }
    }
    dfs2(0, -1);

    int q;
    cin >> q;
    vector<pair<long long, int>> queries(q);
    for (int i = 0; i < q; ++i) {
        cin >> queries[i].first;
        queries[i].second = i;
    }
    sort(queries.begin(), queries.end());
    reverse(queries.begin(), queries.end());
    vector<int> sol(q);
    int j = 0;
#ifdef DEBUG
    for (int i = 0; i < n; ++i) {
        cout << i << ":\n";
        for (int k = 0; k <(int) dp[i][0].size(); ++k) {
            cout << k << " " << dp[i][0][k] << "\n";
        }
        cout << "\n";
        for (int k = 0; k < (int)dp[i][1].size(); ++k) {
            cout << k << " " << dp[i][1][k] << "\n";
        }
        cout << "\n";
    }
#endif
    for (int i = n - 1; i >= 0; --i) {
#ifdef DEBUG
        cout << i << " " << dp[0][0][i] << "\n";
#endif
        while (j < q && dp[0][0][i] <= queries[j].first) {
            sol[queries[j].second] = i;
            ++j;
        }
    }
    for (auto &i : sol) {
        cout << i << "\n";
    }
    return 0;
}

Compilation message

dzumbus.cpp: In function 'std::vector<long long int> mn(std::vector<long long int>, std::vector<long long int>)':
dzumbus.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 0; i < b.size(); ++i) {
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9856 KB Output is correct
2 Correct 15 ms 13816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9856 KB Output is correct
2 Correct 15 ms 13816 KB Output is correct
3 Correct 61 ms 18372 KB Output is correct
4 Correct 62 ms 16204 KB Output is correct
5 Correct 67 ms 14576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 5396 KB Output is correct
2 Incorrect 44 ms 5244 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 9856 KB Output is correct
2 Correct 15 ms 13816 KB Output is correct
3 Correct 61 ms 18372 KB Output is correct
4 Correct 62 ms 16204 KB Output is correct
5 Correct 67 ms 14576 KB Output is correct
6 Correct 44 ms 5396 KB Output is correct
7 Incorrect 44 ms 5244 KB Output isn't correct
8 Halted 0 ms 0 KB -