Submission #102498

# Submission time Handle Problem Language Result Execution time Memory
102498 2019-03-25T11:35:29 Z Andrei1998 Designated Cities (JOI19_designated_cities) C++17
100 / 100
1628 ms 94620 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long int lint;

const int NMAX = 200000 + 5;

int N;
set <int> tree[NMAX];
inline int parent(int node) { return *tree[node].begin(); }
map <lint, int> cost[NMAX];

lint ans[NMAX];

int main() {
    ios_base :: sync_with_stdio(false), cin.tie(nullptr);

    cin >> N;
    for (int i = 1; i < N; ++i) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        tree[a].insert(b);
        tree[b].insert(a);
        cost[a][b] = c;
        cost[b][a] = d;
    }

    // E = 1
    lint sum = 0;
    function <void(int, int)> dfs_compute_from_1 = [&sum, &dfs_compute_from_1](const int node, const int father) {
        for (auto it: tree[node]) {
            if (it != father) {
                sum += cost[node][it];
                dfs_compute_from_1(it, node);
            }
        }
    };

    function <void(int, int)> dfs_compute = [&sum, &dfs_compute](const int node, const int father) {
        ans[1] = min(ans[1], sum);
        for (auto it: tree[node]) {
            if (it != father) {
                sum += cost[it][node] - cost[node][it];
                dfs_compute(it, node);
                sum -= cost[it][node] - cost[node][it];
            }
        }
    };
    dfs_compute_from_1(1, 0);
    ans[1] = sum;
    dfs_compute(1, 0);

    // E > 1
    priority_queue <pair <lint, int> > pq;
    for (int i = 1; i <= N; ++i) {
        if (tree[i].size() == 1) {
            pq.emplace(-cost[parent(i)][i], i);
        }
    }

    for (int i = pq.size(); i <= N; ++i) {
        ans[i] = 0;
    }

    int where = pq.size() - 1;
    while (where > 1) {
        int node; lint cst;
        tie(cst, node) = pq.top();
        cst = -cst;
        pq.pop();

        const int father = parent(node);
        tree[father].erase(node);
        if (tree[father].size() == 1) {
            pq.emplace(-(cst + cost[parent(father)][father]), father);
        } else {
            ans[where] = ans[where + 1] + cst;
            --where;
        }
    }

    int Q = 0;
    cin >> Q;
    for (int i = 1; i <= Q; ++i) {
        int E;
        cin >> E;
        cout << ans[E] << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19072 KB Output is correct
2 Correct 18 ms 19072 KB Output is correct
3 Correct 21 ms 19072 KB Output is correct
4 Correct 19 ms 19072 KB Output is correct
5 Correct 20 ms 19172 KB Output is correct
6 Correct 21 ms 19192 KB Output is correct
7 Correct 18 ms 19200 KB Output is correct
8 Correct 18 ms 19204 KB Output is correct
9 Correct 19 ms 19072 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19072 KB Output is correct
2 Correct 972 ms 66084 KB Output is correct
3 Correct 811 ms 90016 KB Output is correct
4 Correct 900 ms 65972 KB Output is correct
5 Correct 1180 ms 66152 KB Output is correct
6 Correct 1035 ms 69340 KB Output is correct
7 Correct 1359 ms 66644 KB Output is correct
8 Correct 826 ms 91032 KB Output is correct
9 Correct 1598 ms 67840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19244 KB Output is correct
2 Correct 957 ms 65932 KB Output is correct
3 Correct 787 ms 94200 KB Output is correct
4 Correct 854 ms 66052 KB Output is correct
5 Correct 1082 ms 66072 KB Output is correct
6 Correct 1014 ms 70008 KB Output is correct
7 Correct 1451 ms 67628 KB Output is correct
8 Correct 841 ms 81416 KB Output is correct
9 Correct 1467 ms 67756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19072 KB Output is correct
2 Correct 18 ms 19072 KB Output is correct
3 Correct 21 ms 19072 KB Output is correct
4 Correct 19 ms 19072 KB Output is correct
5 Correct 20 ms 19172 KB Output is correct
6 Correct 21 ms 19192 KB Output is correct
7 Correct 18 ms 19200 KB Output is correct
8 Correct 18 ms 19204 KB Output is correct
9 Correct 19 ms 19072 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
12 Correct 20 ms 19072 KB Output is correct
13 Correct 22 ms 19584 KB Output is correct
14 Correct 21 ms 19712 KB Output is correct
15 Correct 21 ms 19584 KB Output is correct
16 Correct 21 ms 19584 KB Output is correct
17 Correct 21 ms 19584 KB Output is correct
18 Correct 22 ms 19584 KB Output is correct
19 Correct 22 ms 19712 KB Output is correct
20 Correct 22 ms 19584 KB Output is correct
21 Correct 29 ms 19704 KB Output is correct
22 Correct 22 ms 19584 KB Output is correct
23 Correct 27 ms 19584 KB Output is correct
24 Correct 18 ms 19712 KB Output is correct
25 Correct 24 ms 19840 KB Output is correct
26 Correct 24 ms 19624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 19072 KB Output is correct
2 Correct 972 ms 66084 KB Output is correct
3 Correct 811 ms 90016 KB Output is correct
4 Correct 900 ms 65972 KB Output is correct
5 Correct 1180 ms 66152 KB Output is correct
6 Correct 1035 ms 69340 KB Output is correct
7 Correct 1359 ms 66644 KB Output is correct
8 Correct 826 ms 91032 KB Output is correct
9 Correct 1598 ms 67840 KB Output is correct
10 Correct 18 ms 19244 KB Output is correct
11 Correct 957 ms 65932 KB Output is correct
12 Correct 787 ms 94200 KB Output is correct
13 Correct 854 ms 66052 KB Output is correct
14 Correct 1082 ms 66072 KB Output is correct
15 Correct 1014 ms 70008 KB Output is correct
16 Correct 1451 ms 67628 KB Output is correct
17 Correct 841 ms 81416 KB Output is correct
18 Correct 1467 ms 67756 KB Output is correct
19 Correct 23 ms 19072 KB Output is correct
20 Correct 990 ms 66020 KB Output is correct
21 Correct 820 ms 94620 KB Output is correct
22 Correct 871 ms 66068 KB Output is correct
23 Correct 927 ms 66036 KB Output is correct
24 Correct 977 ms 66176 KB Output is correct
25 Correct 1096 ms 66016 KB Output is correct
26 Correct 926 ms 66008 KB Output is correct
27 Correct 1092 ms 66144 KB Output is correct
28 Correct 971 ms 69392 KB Output is correct
29 Correct 1018 ms 66272 KB Output is correct
30 Correct 1085 ms 66272 KB Output is correct
31 Correct 1359 ms 67296 KB Output is correct
32 Correct 891 ms 82592 KB Output is correct
33 Correct 1584 ms 67836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19072 KB Output is correct
2 Correct 18 ms 19072 KB Output is correct
3 Correct 21 ms 19072 KB Output is correct
4 Correct 19 ms 19072 KB Output is correct
5 Correct 20 ms 19172 KB Output is correct
6 Correct 21 ms 19192 KB Output is correct
7 Correct 18 ms 19200 KB Output is correct
8 Correct 18 ms 19204 KB Output is correct
9 Correct 19 ms 19072 KB Output is correct
10 Correct 19 ms 19200 KB Output is correct
11 Correct 18 ms 19072 KB Output is correct
12 Correct 18 ms 19072 KB Output is correct
13 Correct 972 ms 66084 KB Output is correct
14 Correct 811 ms 90016 KB Output is correct
15 Correct 900 ms 65972 KB Output is correct
16 Correct 1180 ms 66152 KB Output is correct
17 Correct 1035 ms 69340 KB Output is correct
18 Correct 1359 ms 66644 KB Output is correct
19 Correct 826 ms 91032 KB Output is correct
20 Correct 1598 ms 67840 KB Output is correct
21 Correct 18 ms 19244 KB Output is correct
22 Correct 957 ms 65932 KB Output is correct
23 Correct 787 ms 94200 KB Output is correct
24 Correct 854 ms 66052 KB Output is correct
25 Correct 1082 ms 66072 KB Output is correct
26 Correct 1014 ms 70008 KB Output is correct
27 Correct 1451 ms 67628 KB Output is correct
28 Correct 841 ms 81416 KB Output is correct
29 Correct 1467 ms 67756 KB Output is correct
30 Correct 20 ms 19072 KB Output is correct
31 Correct 22 ms 19584 KB Output is correct
32 Correct 21 ms 19712 KB Output is correct
33 Correct 21 ms 19584 KB Output is correct
34 Correct 21 ms 19584 KB Output is correct
35 Correct 21 ms 19584 KB Output is correct
36 Correct 22 ms 19584 KB Output is correct
37 Correct 22 ms 19712 KB Output is correct
38 Correct 22 ms 19584 KB Output is correct
39 Correct 29 ms 19704 KB Output is correct
40 Correct 22 ms 19584 KB Output is correct
41 Correct 27 ms 19584 KB Output is correct
42 Correct 18 ms 19712 KB Output is correct
43 Correct 24 ms 19840 KB Output is correct
44 Correct 24 ms 19624 KB Output is correct
45 Correct 23 ms 19072 KB Output is correct
46 Correct 990 ms 66020 KB Output is correct
47 Correct 820 ms 94620 KB Output is correct
48 Correct 871 ms 66068 KB Output is correct
49 Correct 927 ms 66036 KB Output is correct
50 Correct 977 ms 66176 KB Output is correct
51 Correct 1096 ms 66016 KB Output is correct
52 Correct 926 ms 66008 KB Output is correct
53 Correct 1092 ms 66144 KB Output is correct
54 Correct 971 ms 69392 KB Output is correct
55 Correct 1018 ms 66272 KB Output is correct
56 Correct 1085 ms 66272 KB Output is correct
57 Correct 1359 ms 67296 KB Output is correct
58 Correct 891 ms 82592 KB Output is correct
59 Correct 1584 ms 67836 KB Output is correct
60 Correct 21 ms 19192 KB Output is correct
61 Correct 1008 ms 67464 KB Output is correct
62 Correct 767 ms 90652 KB Output is correct
63 Correct 856 ms 67580 KB Output is correct
64 Correct 1004 ms 67652 KB Output is correct
65 Correct 997 ms 67260 KB Output is correct
66 Correct 976 ms 67488 KB Output is correct
67 Correct 912 ms 67176 KB Output is correct
68 Correct 1067 ms 67680 KB Output is correct
69 Correct 950 ms 70252 KB Output is correct
70 Correct 968 ms 67408 KB Output is correct
71 Correct 1172 ms 67960 KB Output is correct
72 Correct 1350 ms 68896 KB Output is correct
73 Correct 851 ms 81436 KB Output is correct
74 Correct 1628 ms 70668 KB Output is correct