#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
using pii = pair<int, int>;
const int MAXN = 2e5;
int N;
int W[MAXN * 2];
vector<pii> adj[MAXN];
i64 sum_in[MAXN];
i64 tot, save[MAXN + 1];
namespace S {
void dfs1(int p, int i) {
for (auto [j, h] : adj[i]) if (p != j) {
dfs1(i, j);
sum_in[i] += sum_in[j] + W[h ^ 1];
}
}
void dfs2(int p, int i) {
for (auto [j, h] : adj[i]) if (p != j) {
sum_in[j] = sum_in[i] - W[h ^ 1] + W[h];
dfs2(i, j);
}
}
}
namespace C {
bool done[MAXN];
int sz[MAXN];
vector<i64> paths, best;
int dfs_sz(int p, int i) {
sz[i] = 1;
for (auto [j, h] : adj[i]) if (p != j && !done[j])
sz[i] += dfs_sz(i, j);
return sz[i];
}
int dfs_cen(int p, int i, int s) {
for (auto [j, h] : adj[i]) if (p != j && !done[j] && sz[j] * 2 > s)
return dfs_cen(i, j, s);
return i;
}
i64 make_paths(int p, int i) {
i64 top = 0;
for (auto [j, h] : adj[i]) if (p != j && !done[j]) {
i64 x = make_paths(i, j) + W[h];
if (x > top) {
if (top > 0) paths.push_back(top);
top = x;
} else {
paths.push_back(x);
}
}
return top;
}
void decomp(int i) {
i = dfs_cen(-1, i, dfs_sz(-1, i));
done[i] = 1;
for (auto [j, h] : adj[i]) if (!done[j]) {
i64 x = make_paths(i, j) + W[h];
paths.push_back(x);
best.push_back(x);
}
sort(rbegin(paths), rend(paths));
sort(rbegin(best), rend(best));
{
i64 x = sum_in[i]; int c = 1;
save[c] = max(save[c], x);
for (i64 y : paths) {
x += y, c++;
save[c] = max(save[c], x);
}
}
if (sz(best) >= 2) {
i64 x = sum_in[i] + best[0] + best[1]; int c = 2;
paths.erase(find(begin(paths), end(paths), best[0]));
paths.erase(find(begin(paths), end(paths), best[1]));
save[c] = max(save[c], x);
for (i64 y : paths) {
x += y, c++;
save[c] = max(save[c], x);
}
}
exchange(paths, {});
exchange(best, {});
for (auto [j, h] : adj[i]) if (!done[j]) decomp(j);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int h = 0; h < N - 1; h++) {
int i, j;
cin >> i >> j, i--, j--;
cin >> W[h * 2] >> W[h * 2 + 1];
adj[i].push_back({j, h * 2});
adj[j].push_back({i, h * 2 + 1});
tot += W[h * 2] + W[h * 2 + 1];
}
S::dfs1(-1, 0);
S::dfs2(-1, 0);
C::decomp(0);
for (int i = 1; i <= N; i++) save[i] = max(save[i], save[i - 1]);
int Q;
cin >> Q;
while (Q--) {
int k;
cin >> k;
cout << tot - save[k] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
336 ms |
25196 KB |
Output is correct |
3 |
Correct |
487 ms |
36180 KB |
Output is correct |
4 |
Correct |
344 ms |
23956 KB |
Output is correct |
5 |
Correct |
219 ms |
25160 KB |
Output is correct |
6 |
Correct |
413 ms |
26992 KB |
Output is correct |
7 |
Correct |
176 ms |
26068 KB |
Output is correct |
8 |
Correct |
589 ms |
36648 KB |
Output is correct |
9 |
Correct |
123 ms |
29884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
322 ms |
25260 KB |
Output is correct |
3 |
Correct |
600 ms |
38220 KB |
Output is correct |
4 |
Correct |
412 ms |
23772 KB |
Output is correct |
5 |
Correct |
266 ms |
25280 KB |
Output is correct |
6 |
Correct |
608 ms |
27120 KB |
Output is correct |
7 |
Correct |
152 ms |
29292 KB |
Output is correct |
8 |
Correct |
717 ms |
32776 KB |
Output is correct |
9 |
Correct |
127 ms |
29376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
7040 KB |
Output is correct |
13 |
Correct |
4 ms |
7260 KB |
Output is correct |
14 |
Correct |
4 ms |
7152 KB |
Output is correct |
15 |
Correct |
4 ms |
7260 KB |
Output is correct |
16 |
Correct |
4 ms |
7260 KB |
Output is correct |
17 |
Correct |
5 ms |
7004 KB |
Output is correct |
18 |
Correct |
3 ms |
7256 KB |
Output is correct |
19 |
Correct |
3 ms |
7052 KB |
Output is correct |
20 |
Correct |
3 ms |
7260 KB |
Output is correct |
21 |
Correct |
4 ms |
7256 KB |
Output is correct |
22 |
Correct |
5 ms |
7260 KB |
Output is correct |
23 |
Correct |
4 ms |
7084 KB |
Output is correct |
24 |
Correct |
3 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7352 KB |
Output is correct |
26 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
336 ms |
25196 KB |
Output is correct |
3 |
Correct |
487 ms |
36180 KB |
Output is correct |
4 |
Correct |
344 ms |
23956 KB |
Output is correct |
5 |
Correct |
219 ms |
25160 KB |
Output is correct |
6 |
Correct |
413 ms |
26992 KB |
Output is correct |
7 |
Correct |
176 ms |
26068 KB |
Output is correct |
8 |
Correct |
589 ms |
36648 KB |
Output is correct |
9 |
Correct |
123 ms |
29884 KB |
Output is correct |
10 |
Correct |
1 ms |
7000 KB |
Output is correct |
11 |
Correct |
322 ms |
25260 KB |
Output is correct |
12 |
Correct |
600 ms |
38220 KB |
Output is correct |
13 |
Correct |
412 ms |
23772 KB |
Output is correct |
14 |
Correct |
266 ms |
25280 KB |
Output is correct |
15 |
Correct |
608 ms |
27120 KB |
Output is correct |
16 |
Correct |
152 ms |
29292 KB |
Output is correct |
17 |
Correct |
717 ms |
32776 KB |
Output is correct |
18 |
Correct |
127 ms |
29376 KB |
Output is correct |
19 |
Correct |
2 ms |
7040 KB |
Output is correct |
20 |
Correct |
431 ms |
25392 KB |
Output is correct |
21 |
Correct |
664 ms |
38352 KB |
Output is correct |
22 |
Correct |
387 ms |
23872 KB |
Output is correct |
23 |
Correct |
415 ms |
25284 KB |
Output is correct |
24 |
Correct |
439 ms |
23992 KB |
Output is correct |
25 |
Correct |
414 ms |
25280 KB |
Output is correct |
26 |
Correct |
443 ms |
24068 KB |
Output is correct |
27 |
Correct |
214 ms |
25000 KB |
Output is correct |
28 |
Correct |
469 ms |
27036 KB |
Output is correct |
29 |
Correct |
375 ms |
24992 KB |
Output is correct |
30 |
Correct |
371 ms |
24648 KB |
Output is correct |
31 |
Correct |
218 ms |
26572 KB |
Output is correct |
32 |
Correct |
551 ms |
33172 KB |
Output is correct |
33 |
Correct |
120 ms |
29732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
7004 KB |
Output is correct |
6 |
Correct |
2 ms |
7004 KB |
Output is correct |
7 |
Correct |
2 ms |
7004 KB |
Output is correct |
8 |
Correct |
1 ms |
7000 KB |
Output is correct |
9 |
Correct |
2 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
7004 KB |
Output is correct |
11 |
Correct |
1 ms |
7004 KB |
Output is correct |
12 |
Correct |
1 ms |
7000 KB |
Output is correct |
13 |
Correct |
336 ms |
25196 KB |
Output is correct |
14 |
Correct |
487 ms |
36180 KB |
Output is correct |
15 |
Correct |
344 ms |
23956 KB |
Output is correct |
16 |
Correct |
219 ms |
25160 KB |
Output is correct |
17 |
Correct |
413 ms |
26992 KB |
Output is correct |
18 |
Correct |
176 ms |
26068 KB |
Output is correct |
19 |
Correct |
589 ms |
36648 KB |
Output is correct |
20 |
Correct |
123 ms |
29884 KB |
Output is correct |
21 |
Correct |
1 ms |
7000 KB |
Output is correct |
22 |
Correct |
322 ms |
25260 KB |
Output is correct |
23 |
Correct |
600 ms |
38220 KB |
Output is correct |
24 |
Correct |
412 ms |
23772 KB |
Output is correct |
25 |
Correct |
266 ms |
25280 KB |
Output is correct |
26 |
Correct |
608 ms |
27120 KB |
Output is correct |
27 |
Correct |
152 ms |
29292 KB |
Output is correct |
28 |
Correct |
717 ms |
32776 KB |
Output is correct |
29 |
Correct |
127 ms |
29376 KB |
Output is correct |
30 |
Correct |
2 ms |
7040 KB |
Output is correct |
31 |
Correct |
4 ms |
7260 KB |
Output is correct |
32 |
Correct |
4 ms |
7152 KB |
Output is correct |
33 |
Correct |
4 ms |
7260 KB |
Output is correct |
34 |
Correct |
4 ms |
7260 KB |
Output is correct |
35 |
Correct |
5 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7256 KB |
Output is correct |
37 |
Correct |
3 ms |
7052 KB |
Output is correct |
38 |
Correct |
3 ms |
7260 KB |
Output is correct |
39 |
Correct |
4 ms |
7256 KB |
Output is correct |
40 |
Correct |
5 ms |
7260 KB |
Output is correct |
41 |
Correct |
4 ms |
7084 KB |
Output is correct |
42 |
Correct |
3 ms |
7260 KB |
Output is correct |
43 |
Correct |
3 ms |
7352 KB |
Output is correct |
44 |
Correct |
3 ms |
7260 KB |
Output is correct |
45 |
Correct |
2 ms |
7040 KB |
Output is correct |
46 |
Correct |
431 ms |
25392 KB |
Output is correct |
47 |
Correct |
664 ms |
38352 KB |
Output is correct |
48 |
Correct |
387 ms |
23872 KB |
Output is correct |
49 |
Correct |
415 ms |
25284 KB |
Output is correct |
50 |
Correct |
439 ms |
23992 KB |
Output is correct |
51 |
Correct |
414 ms |
25280 KB |
Output is correct |
52 |
Correct |
443 ms |
24068 KB |
Output is correct |
53 |
Correct |
214 ms |
25000 KB |
Output is correct |
54 |
Correct |
469 ms |
27036 KB |
Output is correct |
55 |
Correct |
375 ms |
24992 KB |
Output is correct |
56 |
Correct |
371 ms |
24648 KB |
Output is correct |
57 |
Correct |
218 ms |
26572 KB |
Output is correct |
58 |
Correct |
551 ms |
33172 KB |
Output is correct |
59 |
Correct |
120 ms |
29732 KB |
Output is correct |
60 |
Correct |
2 ms |
7000 KB |
Output is correct |
61 |
Correct |
415 ms |
27616 KB |
Output is correct |
62 |
Correct |
618 ms |
37868 KB |
Output is correct |
63 |
Correct |
425 ms |
26416 KB |
Output is correct |
64 |
Correct |
383 ms |
28004 KB |
Output is correct |
65 |
Correct |
423 ms |
26432 KB |
Output is correct |
66 |
Correct |
415 ms |
28028 KB |
Output is correct |
67 |
Correct |
439 ms |
26588 KB |
Output is correct |
68 |
Correct |
321 ms |
27836 KB |
Output is correct |
69 |
Correct |
531 ms |
29496 KB |
Output is correct |
70 |
Correct |
445 ms |
27704 KB |
Output is correct |
71 |
Correct |
300 ms |
27528 KB |
Output is correct |
72 |
Correct |
170 ms |
28116 KB |
Output is correct |
73 |
Correct |
542 ms |
34228 KB |
Output is correct |
74 |
Correct |
140 ms |
32676 KB |
Output is correct |