#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() {
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 |
19 ms |
19200 KB |
Output is correct |
3 |
Correct |
19 ms |
19072 KB |
Output is correct |
4 |
Correct |
22 ms |
19192 KB |
Output is correct |
5 |
Correct |
20 ms |
19072 KB |
Output is correct |
6 |
Correct |
21 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19072 KB |
Output is correct |
8 |
Correct |
19 ms |
19044 KB |
Output is correct |
9 |
Correct |
19 ms |
19200 KB |
Output is correct |
10 |
Correct |
22 ms |
19200 KB |
Output is correct |
11 |
Correct |
19 ms |
19200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
19200 KB |
Output is correct |
2 |
Correct |
1188 ms |
65932 KB |
Output is correct |
3 |
Correct |
1014 ms |
89984 KB |
Output is correct |
4 |
Correct |
1048 ms |
66140 KB |
Output is correct |
5 |
Correct |
1441 ms |
66216 KB |
Output is correct |
6 |
Correct |
1264 ms |
69400 KB |
Output is correct |
7 |
Correct |
1581 ms |
66816 KB |
Output is correct |
8 |
Correct |
1167 ms |
90868 KB |
Output is correct |
9 |
Correct |
1745 ms |
68104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
19072 KB |
Output is correct |
2 |
Correct |
1195 ms |
66040 KB |
Output is correct |
3 |
Correct |
1053 ms |
94284 KB |
Output is correct |
4 |
Correct |
1082 ms |
65996 KB |
Output is correct |
5 |
Correct |
1320 ms |
66180 KB |
Output is correct |
6 |
Correct |
1390 ms |
70012 KB |
Output is correct |
7 |
Correct |
1834 ms |
67676 KB |
Output is correct |
8 |
Correct |
1111 ms |
81488 KB |
Output is correct |
9 |
Correct |
1808 ms |
68032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19072 KB |
Output is correct |
2 |
Correct |
19 ms |
19200 KB |
Output is correct |
3 |
Correct |
19 ms |
19072 KB |
Output is correct |
4 |
Correct |
22 ms |
19192 KB |
Output is correct |
5 |
Correct |
20 ms |
19072 KB |
Output is correct |
6 |
Correct |
21 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19072 KB |
Output is correct |
8 |
Correct |
19 ms |
19044 KB |
Output is correct |
9 |
Correct |
19 ms |
19200 KB |
Output is correct |
10 |
Correct |
22 ms |
19200 KB |
Output is correct |
11 |
Correct |
19 ms |
19200 KB |
Output is correct |
12 |
Correct |
22 ms |
19072 KB |
Output is correct |
13 |
Correct |
29 ms |
19712 KB |
Output is correct |
14 |
Correct |
29 ms |
19968 KB |
Output is correct |
15 |
Correct |
29 ms |
19840 KB |
Output is correct |
16 |
Correct |
30 ms |
19584 KB |
Output is correct |
17 |
Correct |
30 ms |
19584 KB |
Output is correct |
18 |
Correct |
32 ms |
19456 KB |
Output is correct |
19 |
Correct |
29 ms |
19760 KB |
Output is correct |
20 |
Correct |
36 ms |
19576 KB |
Output is correct |
21 |
Correct |
30 ms |
19704 KB |
Output is correct |
22 |
Correct |
29 ms |
19584 KB |
Output is correct |
23 |
Correct |
29 ms |
19648 KB |
Output is correct |
24 |
Correct |
33 ms |
19712 KB |
Output is correct |
25 |
Correct |
29 ms |
19840 KB |
Output is correct |
26 |
Correct |
32 ms |
19712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
19200 KB |
Output is correct |
2 |
Correct |
1188 ms |
65932 KB |
Output is correct |
3 |
Correct |
1014 ms |
89984 KB |
Output is correct |
4 |
Correct |
1048 ms |
66140 KB |
Output is correct |
5 |
Correct |
1441 ms |
66216 KB |
Output is correct |
6 |
Correct |
1264 ms |
69400 KB |
Output is correct |
7 |
Correct |
1581 ms |
66816 KB |
Output is correct |
8 |
Correct |
1167 ms |
90868 KB |
Output is correct |
9 |
Correct |
1745 ms |
68104 KB |
Output is correct |
10 |
Correct |
21 ms |
19072 KB |
Output is correct |
11 |
Correct |
1195 ms |
66040 KB |
Output is correct |
12 |
Correct |
1053 ms |
94284 KB |
Output is correct |
13 |
Correct |
1082 ms |
65996 KB |
Output is correct |
14 |
Correct |
1320 ms |
66180 KB |
Output is correct |
15 |
Correct |
1390 ms |
70012 KB |
Output is correct |
16 |
Correct |
1834 ms |
67676 KB |
Output is correct |
17 |
Correct |
1111 ms |
81488 KB |
Output is correct |
18 |
Correct |
1808 ms |
68032 KB |
Output is correct |
19 |
Correct |
20 ms |
19156 KB |
Output is correct |
20 |
Correct |
1385 ms |
66196 KB |
Output is correct |
21 |
Correct |
1004 ms |
100956 KB |
Output is correct |
22 |
Correct |
1091 ms |
71180 KB |
Output is correct |
23 |
Correct |
1239 ms |
72364 KB |
Output is correct |
24 |
Correct |
1150 ms |
71212 KB |
Output is correct |
25 |
Correct |
1267 ms |
72548 KB |
Output is correct |
26 |
Correct |
1181 ms |
71400 KB |
Output is correct |
27 |
Correct |
1300 ms |
72696 KB |
Output is correct |
28 |
Correct |
1363 ms |
75636 KB |
Output is correct |
29 |
Correct |
1259 ms |
72548 KB |
Output is correct |
30 |
Correct |
1321 ms |
71924 KB |
Output is correct |
31 |
Correct |
1775 ms |
73956 KB |
Output is correct |
32 |
Correct |
1191 ms |
89164 KB |
Output is correct |
33 |
Correct |
1794 ms |
74300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19072 KB |
Output is correct |
2 |
Correct |
19 ms |
19200 KB |
Output is correct |
3 |
Correct |
19 ms |
19072 KB |
Output is correct |
4 |
Correct |
22 ms |
19192 KB |
Output is correct |
5 |
Correct |
20 ms |
19072 KB |
Output is correct |
6 |
Correct |
21 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19072 KB |
Output is correct |
8 |
Correct |
19 ms |
19044 KB |
Output is correct |
9 |
Correct |
19 ms |
19200 KB |
Output is correct |
10 |
Correct |
22 ms |
19200 KB |
Output is correct |
11 |
Correct |
19 ms |
19200 KB |
Output is correct |
12 |
Correct |
21 ms |
19200 KB |
Output is correct |
13 |
Correct |
1188 ms |
65932 KB |
Output is correct |
14 |
Correct |
1014 ms |
89984 KB |
Output is correct |
15 |
Correct |
1048 ms |
66140 KB |
Output is correct |
16 |
Correct |
1441 ms |
66216 KB |
Output is correct |
17 |
Correct |
1264 ms |
69400 KB |
Output is correct |
18 |
Correct |
1581 ms |
66816 KB |
Output is correct |
19 |
Correct |
1167 ms |
90868 KB |
Output is correct |
20 |
Correct |
1745 ms |
68104 KB |
Output is correct |
21 |
Correct |
21 ms |
19072 KB |
Output is correct |
22 |
Correct |
1195 ms |
66040 KB |
Output is correct |
23 |
Correct |
1053 ms |
94284 KB |
Output is correct |
24 |
Correct |
1082 ms |
65996 KB |
Output is correct |
25 |
Correct |
1320 ms |
66180 KB |
Output is correct |
26 |
Correct |
1390 ms |
70012 KB |
Output is correct |
27 |
Correct |
1834 ms |
67676 KB |
Output is correct |
28 |
Correct |
1111 ms |
81488 KB |
Output is correct |
29 |
Correct |
1808 ms |
68032 KB |
Output is correct |
30 |
Correct |
22 ms |
19072 KB |
Output is correct |
31 |
Correct |
29 ms |
19712 KB |
Output is correct |
32 |
Correct |
29 ms |
19968 KB |
Output is correct |
33 |
Correct |
29 ms |
19840 KB |
Output is correct |
34 |
Correct |
30 ms |
19584 KB |
Output is correct |
35 |
Correct |
30 ms |
19584 KB |
Output is correct |
36 |
Correct |
32 ms |
19456 KB |
Output is correct |
37 |
Correct |
29 ms |
19760 KB |
Output is correct |
38 |
Correct |
36 ms |
19576 KB |
Output is correct |
39 |
Correct |
30 ms |
19704 KB |
Output is correct |
40 |
Correct |
29 ms |
19584 KB |
Output is correct |
41 |
Correct |
29 ms |
19648 KB |
Output is correct |
42 |
Correct |
33 ms |
19712 KB |
Output is correct |
43 |
Correct |
29 ms |
19840 KB |
Output is correct |
44 |
Correct |
32 ms |
19712 KB |
Output is correct |
45 |
Correct |
20 ms |
19156 KB |
Output is correct |
46 |
Correct |
1385 ms |
66196 KB |
Output is correct |
47 |
Correct |
1004 ms |
100956 KB |
Output is correct |
48 |
Correct |
1091 ms |
71180 KB |
Output is correct |
49 |
Correct |
1239 ms |
72364 KB |
Output is correct |
50 |
Correct |
1150 ms |
71212 KB |
Output is correct |
51 |
Correct |
1267 ms |
72548 KB |
Output is correct |
52 |
Correct |
1181 ms |
71400 KB |
Output is correct |
53 |
Correct |
1300 ms |
72696 KB |
Output is correct |
54 |
Correct |
1363 ms |
75636 KB |
Output is correct |
55 |
Correct |
1259 ms |
72548 KB |
Output is correct |
56 |
Correct |
1321 ms |
71924 KB |
Output is correct |
57 |
Correct |
1775 ms |
73956 KB |
Output is correct |
58 |
Correct |
1191 ms |
89164 KB |
Output is correct |
59 |
Correct |
1794 ms |
74300 KB |
Output is correct |
60 |
Correct |
20 ms |
19072 KB |
Output is correct |
61 |
Correct |
1786 ms |
73904 KB |
Output is correct |
62 |
Correct |
1452 ms |
96992 KB |
Output is correct |
63 |
Correct |
1481 ms |
73696 KB |
Output is correct |
64 |
Correct |
1636 ms |
74000 KB |
Output is correct |
65 |
Correct |
1802 ms |
73556 KB |
Output is correct |
66 |
Correct |
1671 ms |
73844 KB |
Output is correct |
67 |
Correct |
1669 ms |
73664 KB |
Output is correct |
68 |
Correct |
1660 ms |
74072 KB |
Output is correct |
69 |
Correct |
1549 ms |
76572 KB |
Output is correct |
70 |
Correct |
1709 ms |
73816 KB |
Output is correct |
71 |
Correct |
1704 ms |
74096 KB |
Output is correct |
72 |
Correct |
1955 ms |
75084 KB |
Output is correct |
73 |
Correct |
1484 ms |
87488 KB |
Output is correct |
74 |
Execution timed out |
2025 ms |
77100 KB |
Time limit exceeded |