Submission #458052

# Submission time Handle Problem Language Result Execution time Memory
458052 2021-08-07T22:39:31 Z izhang05 Džumbus (COCI19_dzumbus) C++17
0 / 110
43 ms 6360 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][2];

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

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, d[c]};
    for (auto &i : adj[c]) {
        if (i != p) {
            dfs2(i, c);
            dp[c][0] = merge(dp[c][0], dp[i][0]);
        }
    }
}


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

    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]) {
            adj[0].push_back(i);
            adj[i].push_back(0);
            dfs1(i, -1);
        }
    }
    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;
    for (int i = n - 1; i >= 2; --i) {
        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> merge(std::vector<long long int>, std::vector<long long int>)':
dzumbus.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (int i = 0; i < a.size(); ++i) {
      |                     ~~^~~~~~~~~~
dzumbus.cpp:27:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         for (int j = 0; j < b.size(); ++j) {
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 3020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 3020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 6360 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 3020 KB Output isn't correct
2 Halted 0 ms 0 KB -