답안 #458054

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
458054 2021-08-07T22:52:49 Z izhang05 Džumbus (COCI19_dzumbus) C++17
0 / 110
48 ms 5080 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];

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 < (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;
}

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


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; i >= 2; --i) {
        while (j < q && dp[0][i] <= queries[j].first) {
            sol[queries[j].second] = i;
            ++j;
        }
    }
    for (auto &i : sol) {
        cout << i << "\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 5080 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 2892 KB Output isn't correct
2 Halted 0 ms 0 KB -