#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 |
19 ms |
19328 KB |
Output is correct |
2 |
Correct |
19 ms |
19072 KB |
Output is correct |
3 |
Correct |
22 ms |
19200 KB |
Output is correct |
4 |
Correct |
19 ms |
19200 KB |
Output is correct |
5 |
Correct |
17 ms |
19072 KB |
Output is correct |
6 |
Correct |
19 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19152 KB |
Output is correct |
9 |
Correct |
18 ms |
19200 KB |
Output is correct |
10 |
Correct |
19 ms |
19108 KB |
Output is correct |
11 |
Correct |
21 ms |
19240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
19072 KB |
Output is correct |
2 |
Correct |
926 ms |
65948 KB |
Output is correct |
3 |
Correct |
725 ms |
90112 KB |
Output is correct |
4 |
Correct |
827 ms |
66016 KB |
Output is correct |
5 |
Correct |
1024 ms |
66272 KB |
Output is correct |
6 |
Correct |
879 ms |
69372 KB |
Output is correct |
7 |
Correct |
1286 ms |
66900 KB |
Output is correct |
8 |
Correct |
740 ms |
90864 KB |
Output is correct |
9 |
Correct |
1457 ms |
67888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
19072 KB |
Output is correct |
2 |
Correct |
882 ms |
66136 KB |
Output is correct |
3 |
Correct |
743 ms |
94200 KB |
Output is correct |
4 |
Correct |
846 ms |
66016 KB |
Output is correct |
5 |
Correct |
1024 ms |
66256 KB |
Output is correct |
6 |
Correct |
977 ms |
69984 KB |
Output is correct |
7 |
Correct |
1446 ms |
67564 KB |
Output is correct |
8 |
Correct |
800 ms |
81560 KB |
Output is correct |
9 |
Correct |
1464 ms |
67980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
19328 KB |
Output is correct |
2 |
Correct |
19 ms |
19072 KB |
Output is correct |
3 |
Correct |
22 ms |
19200 KB |
Output is correct |
4 |
Correct |
19 ms |
19200 KB |
Output is correct |
5 |
Correct |
17 ms |
19072 KB |
Output is correct |
6 |
Correct |
19 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19152 KB |
Output is correct |
9 |
Correct |
18 ms |
19200 KB |
Output is correct |
10 |
Correct |
19 ms |
19108 KB |
Output is correct |
11 |
Correct |
21 ms |
19240 KB |
Output is correct |
12 |
Correct |
21 ms |
19204 KB |
Output is correct |
13 |
Correct |
23 ms |
19584 KB |
Output is correct |
14 |
Correct |
24 ms |
19960 KB |
Output is correct |
15 |
Correct |
21 ms |
19584 KB |
Output is correct |
16 |
Correct |
22 ms |
19712 KB |
Output is correct |
17 |
Correct |
22 ms |
19584 KB |
Output is correct |
18 |
Correct |
29 ms |
19616 KB |
Output is correct |
19 |
Correct |
23 ms |
19584 KB |
Output is correct |
20 |
Correct |
24 ms |
19584 KB |
Output is correct |
21 |
Correct |
25 ms |
19712 KB |
Output is correct |
22 |
Correct |
22 ms |
19712 KB |
Output is correct |
23 |
Correct |
23 ms |
19584 KB |
Output is correct |
24 |
Correct |
22 ms |
19712 KB |
Output is correct |
25 |
Correct |
24 ms |
19840 KB |
Output is correct |
26 |
Correct |
25 ms |
19712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
19072 KB |
Output is correct |
2 |
Correct |
926 ms |
65948 KB |
Output is correct |
3 |
Correct |
725 ms |
90112 KB |
Output is correct |
4 |
Correct |
827 ms |
66016 KB |
Output is correct |
5 |
Correct |
1024 ms |
66272 KB |
Output is correct |
6 |
Correct |
879 ms |
69372 KB |
Output is correct |
7 |
Correct |
1286 ms |
66900 KB |
Output is correct |
8 |
Correct |
740 ms |
90864 KB |
Output is correct |
9 |
Correct |
1457 ms |
67888 KB |
Output is correct |
10 |
Correct |
18 ms |
19072 KB |
Output is correct |
11 |
Correct |
882 ms |
66136 KB |
Output is correct |
12 |
Correct |
743 ms |
94200 KB |
Output is correct |
13 |
Correct |
846 ms |
66016 KB |
Output is correct |
14 |
Correct |
1024 ms |
66256 KB |
Output is correct |
15 |
Correct |
977 ms |
69984 KB |
Output is correct |
16 |
Correct |
1446 ms |
67564 KB |
Output is correct |
17 |
Correct |
800 ms |
81560 KB |
Output is correct |
18 |
Correct |
1464 ms |
67980 KB |
Output is correct |
19 |
Correct |
20 ms |
19200 KB |
Output is correct |
20 |
Correct |
1008 ms |
66160 KB |
Output is correct |
21 |
Correct |
757 ms |
94584 KB |
Output is correct |
22 |
Correct |
837 ms |
66016 KB |
Output is correct |
23 |
Correct |
920 ms |
66116 KB |
Output is correct |
24 |
Correct |
934 ms |
66144 KB |
Output is correct |
25 |
Correct |
1050 ms |
65968 KB |
Output is correct |
26 |
Correct |
1000 ms |
66016 KB |
Output is correct |
27 |
Correct |
1102 ms |
66144 KB |
Output is correct |
28 |
Correct |
972 ms |
69432 KB |
Output is correct |
29 |
Correct |
953 ms |
66272 KB |
Output is correct |
30 |
Correct |
1102 ms |
66400 KB |
Output is correct |
31 |
Correct |
1526 ms |
67296 KB |
Output is correct |
32 |
Correct |
908 ms |
82664 KB |
Output is correct |
33 |
Correct |
1601 ms |
68052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
19328 KB |
Output is correct |
2 |
Correct |
19 ms |
19072 KB |
Output is correct |
3 |
Correct |
22 ms |
19200 KB |
Output is correct |
4 |
Correct |
19 ms |
19200 KB |
Output is correct |
5 |
Correct |
17 ms |
19072 KB |
Output is correct |
6 |
Correct |
19 ms |
19200 KB |
Output is correct |
7 |
Correct |
19 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19152 KB |
Output is correct |
9 |
Correct |
18 ms |
19200 KB |
Output is correct |
10 |
Correct |
19 ms |
19108 KB |
Output is correct |
11 |
Correct |
21 ms |
19240 KB |
Output is correct |
12 |
Correct |
19 ms |
19072 KB |
Output is correct |
13 |
Correct |
926 ms |
65948 KB |
Output is correct |
14 |
Correct |
725 ms |
90112 KB |
Output is correct |
15 |
Correct |
827 ms |
66016 KB |
Output is correct |
16 |
Correct |
1024 ms |
66272 KB |
Output is correct |
17 |
Correct |
879 ms |
69372 KB |
Output is correct |
18 |
Correct |
1286 ms |
66900 KB |
Output is correct |
19 |
Correct |
740 ms |
90864 KB |
Output is correct |
20 |
Correct |
1457 ms |
67888 KB |
Output is correct |
21 |
Correct |
18 ms |
19072 KB |
Output is correct |
22 |
Correct |
882 ms |
66136 KB |
Output is correct |
23 |
Correct |
743 ms |
94200 KB |
Output is correct |
24 |
Correct |
846 ms |
66016 KB |
Output is correct |
25 |
Correct |
1024 ms |
66256 KB |
Output is correct |
26 |
Correct |
977 ms |
69984 KB |
Output is correct |
27 |
Correct |
1446 ms |
67564 KB |
Output is correct |
28 |
Correct |
800 ms |
81560 KB |
Output is correct |
29 |
Correct |
1464 ms |
67980 KB |
Output is correct |
30 |
Correct |
21 ms |
19204 KB |
Output is correct |
31 |
Correct |
23 ms |
19584 KB |
Output is correct |
32 |
Correct |
24 ms |
19960 KB |
Output is correct |
33 |
Correct |
21 ms |
19584 KB |
Output is correct |
34 |
Correct |
22 ms |
19712 KB |
Output is correct |
35 |
Correct |
22 ms |
19584 KB |
Output is correct |
36 |
Correct |
29 ms |
19616 KB |
Output is correct |
37 |
Correct |
23 ms |
19584 KB |
Output is correct |
38 |
Correct |
24 ms |
19584 KB |
Output is correct |
39 |
Correct |
25 ms |
19712 KB |
Output is correct |
40 |
Correct |
22 ms |
19712 KB |
Output is correct |
41 |
Correct |
23 ms |
19584 KB |
Output is correct |
42 |
Correct |
22 ms |
19712 KB |
Output is correct |
43 |
Correct |
24 ms |
19840 KB |
Output is correct |
44 |
Correct |
25 ms |
19712 KB |
Output is correct |
45 |
Correct |
20 ms |
19200 KB |
Output is correct |
46 |
Correct |
1008 ms |
66160 KB |
Output is correct |
47 |
Correct |
757 ms |
94584 KB |
Output is correct |
48 |
Correct |
837 ms |
66016 KB |
Output is correct |
49 |
Correct |
920 ms |
66116 KB |
Output is correct |
50 |
Correct |
934 ms |
66144 KB |
Output is correct |
51 |
Correct |
1050 ms |
65968 KB |
Output is correct |
52 |
Correct |
1000 ms |
66016 KB |
Output is correct |
53 |
Correct |
1102 ms |
66144 KB |
Output is correct |
54 |
Correct |
972 ms |
69432 KB |
Output is correct |
55 |
Correct |
953 ms |
66272 KB |
Output is correct |
56 |
Correct |
1102 ms |
66400 KB |
Output is correct |
57 |
Correct |
1526 ms |
67296 KB |
Output is correct |
58 |
Correct |
908 ms |
82664 KB |
Output is correct |
59 |
Correct |
1601 ms |
68052 KB |
Output is correct |
60 |
Correct |
18 ms |
19072 KB |
Output is correct |
61 |
Correct |
981 ms |
67528 KB |
Output is correct |
62 |
Correct |
869 ms |
90700 KB |
Output is correct |
63 |
Correct |
958 ms |
67436 KB |
Output is correct |
64 |
Correct |
944 ms |
67604 KB |
Output is correct |
65 |
Correct |
943 ms |
67168 KB |
Output is correct |
66 |
Correct |
936 ms |
67520 KB |
Output is correct |
67 |
Correct |
946 ms |
67360 KB |
Output is correct |
68 |
Correct |
1103 ms |
67776 KB |
Output is correct |
69 |
Correct |
1069 ms |
70352 KB |
Output is correct |
70 |
Correct |
972 ms |
67588 KB |
Output is correct |
71 |
Correct |
1117 ms |
68084 KB |
Output is correct |
72 |
Correct |
1318 ms |
68896 KB |
Output is correct |
73 |
Correct |
869 ms |
81384 KB |
Output is correct |
74 |
Correct |
1450 ms |
70916 KB |
Output is correct |