#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 2e5 + 5, inf = 1e15 + 5;
vector<tii> g[nmax];
int height[nmax], atrleaf[nmax], troot[nmax];
int wroot;
void initdfs(int node, int f) {
atrleaf[node] = node;
for(auto [x, c, alt] : g[node]) {
if(x == f) continue;
troot[x] = troot[node] - c + alt;
wroot += c;
initdfs(x, node);
tie(height[node], atrleaf[node]) = max(make_pair(height[x] + c, atrleaf[x]), make_pair(height[node], atrleaf[node]));
}
return;
}
int mxl, mxr, mxcost = inf;
void diamdfs(int node, int f) {
int ends[2] = {node, node}, cost[2] = {0, 0};
for(auto [x, c, alt] : g[node]) {
if(x == f) continue;
diamdfs(x, node);
alt = height[x] + c;
if(alt >= cost[0])
tie(ends[1], cost[1]) = pii{ends[0], cost[0]},
tie(ends[0], cost[0]) = pii{atrleaf[x], alt};
else if(alt > cost[1])
tie(ends[1], cost[1]) = pii{atrleaf[x], alt};
}
if(wroot - (cost[0] + cost[1]) + troot[node] < mxcost)
tie(mxl, mxr, mxcost) = tii{ends[0], ends[1], wroot - (cost[0] + cost[1]) + troot[node]};
}
void reinitdfs(int node, int f) {
atrleaf[node] = node;
for(auto [x, c, alt] : g[node]) {
if(x == f) continue;
troot[x] = troot[node] + c;
wroot += c;
reinitdfs(x, node);
if(troot[atrleaf[node]] < troot[atrleaf[x]])
atrleaf[node] = atrleaf[x];
}
return;
}
priority_queue<int> pq;
void initpqdfs(int node, int f, int offset = 0) {
if(g[node].size() == 1 && node != f) {
pq.push(troot[node] - offset);
return;
}
for(auto [x, c, cost] : g[node]) {
if(x == f) continue;
if(atrleaf[node] != atrleaf[x])
initpqdfs(x, node, troot[node]);
else
initpqdfs(x, node, offset);
}
return;
}
int rez[nmax];
signed main() {
int n;
cin >> n;
for(int i = 0, x, y, a, b; i < n - 1; i++) {
cin >> x >> y >> a >> b;
g[x].emplace_back(y, a, b);
g[y].emplace_back(x, b, a);
}
int root = 1;
initdfs(root, root);
rez[1] = inf;
for(int i = 1; i <= n; i++)
rez[1] = min(rez[1], wroot + troot[i]);
diamdfs(root, root);
troot[root = mxl] = 0;;
rez[2] = mxcost;
wroot = 0;
reinitdfs(root, root);
initpqdfs(root, root);
int ptr = 1;
while(!pq.empty()) {
wroot -= pq.top();
rez[++ptr] = wroot;
pq.pop();
}
++ptr;
while(ptr <= n)
rez[ptr] = rez[ptr - 1],
ptr++;
int q;
cin >> q;
for(int i = 0, x; i < q; i++) {
cin >> x;
cout << rez[x] << '\n';
}
}
/**
De-atâtea nopți aud plouând,
Aud materia plângând..
Sînt singur, și mă duce un gând
Spre locuințele lacustre.
Și parcă dorm pe scânduri ude,
În spate mă izbește-un val --
Tresar prin somn și mi se pare
Că n-am tras podul de la mal.
Un gol istoric se întinde,
Pe-același vremuri mă găsesc..
Și simt cum de atâta ploaie
Pilonii grei se prăbușesc.
De-atâtea nopți aud plouând,
Tot tresărind, tot așteptând..
Sînt singur, și mă duce-un gând
Spre locuințele lacustre.
-- George Bacovia, Lacustră
*/
Compilation message
designated_cities.cpp: In function 'void initdfs(ll, ll)':
designated_cities.cpp:22:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for(auto [x, c, alt] : g[node]) {
| ^
designated_cities.cpp: In function 'void diamdfs(ll, ll)':
designated_cities.cpp:35:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for(auto [x, c, alt] : g[node]) {
| ^
designated_cities.cpp: In function 'void reinitdfs(ll, ll)':
designated_cities.cpp:51:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
51 | for(auto [x, c, alt] : g[node]) {
| ^
designated_cities.cpp: In function 'void initpqdfs(ll, ll, ll)':
designated_cities.cpp:68:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for(auto [x, c, cost] : g[node]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
448 ms |
31268 KB |
Output is correct |
3 |
Correct |
493 ms |
49504 KB |
Output is correct |
4 |
Correct |
415 ms |
30080 KB |
Output is correct |
5 |
Correct |
428 ms |
32140 KB |
Output is correct |
6 |
Correct |
464 ms |
34024 KB |
Output is correct |
7 |
Correct |
393 ms |
31580 KB |
Output is correct |
8 |
Correct |
499 ms |
48952 KB |
Output is correct |
9 |
Correct |
336 ms |
30652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
444 ms |
31472 KB |
Output is correct |
3 |
Correct |
501 ms |
49484 KB |
Output is correct |
4 |
Correct |
434 ms |
30132 KB |
Output is correct |
5 |
Correct |
446 ms |
32220 KB |
Output is correct |
6 |
Correct |
476 ms |
34620 KB |
Output is correct |
7 |
Correct |
343 ms |
32076 KB |
Output is correct |
8 |
Correct |
499 ms |
43504 KB |
Output is correct |
9 |
Correct |
324 ms |
30400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
8 ms |
5320 KB |
Output is correct |
14 |
Correct |
8 ms |
5460 KB |
Output is correct |
15 |
Correct |
8 ms |
5204 KB |
Output is correct |
16 |
Correct |
8 ms |
5332 KB |
Output is correct |
17 |
Correct |
8 ms |
5288 KB |
Output is correct |
18 |
Correct |
9 ms |
5204 KB |
Output is correct |
19 |
Correct |
9 ms |
5204 KB |
Output is correct |
20 |
Correct |
10 ms |
5332 KB |
Output is correct |
21 |
Correct |
9 ms |
5332 KB |
Output is correct |
22 |
Correct |
11 ms |
5284 KB |
Output is correct |
23 |
Correct |
9 ms |
5288 KB |
Output is correct |
24 |
Correct |
8 ms |
5332 KB |
Output is correct |
25 |
Correct |
11 ms |
5420 KB |
Output is correct |
26 |
Correct |
8 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5020 KB |
Output is correct |
2 |
Correct |
448 ms |
31268 KB |
Output is correct |
3 |
Correct |
493 ms |
49504 KB |
Output is correct |
4 |
Correct |
415 ms |
30080 KB |
Output is correct |
5 |
Correct |
428 ms |
32140 KB |
Output is correct |
6 |
Correct |
464 ms |
34024 KB |
Output is correct |
7 |
Correct |
393 ms |
31580 KB |
Output is correct |
8 |
Correct |
499 ms |
48952 KB |
Output is correct |
9 |
Correct |
336 ms |
30652 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
444 ms |
31472 KB |
Output is correct |
12 |
Correct |
501 ms |
49484 KB |
Output is correct |
13 |
Correct |
434 ms |
30132 KB |
Output is correct |
14 |
Correct |
446 ms |
32220 KB |
Output is correct |
15 |
Correct |
476 ms |
34620 KB |
Output is correct |
16 |
Correct |
343 ms |
32076 KB |
Output is correct |
17 |
Correct |
499 ms |
43504 KB |
Output is correct |
18 |
Correct |
324 ms |
30400 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
463 ms |
31476 KB |
Output is correct |
21 |
Correct |
495 ms |
49452 KB |
Output is correct |
22 |
Correct |
406 ms |
30168 KB |
Output is correct |
23 |
Correct |
449 ms |
31736 KB |
Output is correct |
24 |
Correct |
437 ms |
30548 KB |
Output is correct |
25 |
Correct |
449 ms |
31768 KB |
Output is correct |
26 |
Correct |
439 ms |
30524 KB |
Output is correct |
27 |
Correct |
418 ms |
31692 KB |
Output is correct |
28 |
Correct |
464 ms |
34260 KB |
Output is correct |
29 |
Correct |
454 ms |
31944 KB |
Output is correct |
30 |
Correct |
397 ms |
31228 KB |
Output is correct |
31 |
Correct |
372 ms |
32484 KB |
Output is correct |
32 |
Correct |
503 ms |
44032 KB |
Output is correct |
33 |
Correct |
353 ms |
30808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5008 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5020 KB |
Output is correct |
13 |
Correct |
448 ms |
31268 KB |
Output is correct |
14 |
Correct |
493 ms |
49504 KB |
Output is correct |
15 |
Correct |
415 ms |
30080 KB |
Output is correct |
16 |
Correct |
428 ms |
32140 KB |
Output is correct |
17 |
Correct |
464 ms |
34024 KB |
Output is correct |
18 |
Correct |
393 ms |
31580 KB |
Output is correct |
19 |
Correct |
499 ms |
48952 KB |
Output is correct |
20 |
Correct |
336 ms |
30652 KB |
Output is correct |
21 |
Correct |
2 ms |
4948 KB |
Output is correct |
22 |
Correct |
444 ms |
31472 KB |
Output is correct |
23 |
Correct |
501 ms |
49484 KB |
Output is correct |
24 |
Correct |
434 ms |
30132 KB |
Output is correct |
25 |
Correct |
446 ms |
32220 KB |
Output is correct |
26 |
Correct |
476 ms |
34620 KB |
Output is correct |
27 |
Correct |
343 ms |
32076 KB |
Output is correct |
28 |
Correct |
499 ms |
43504 KB |
Output is correct |
29 |
Correct |
324 ms |
30400 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
8 ms |
5320 KB |
Output is correct |
32 |
Correct |
8 ms |
5460 KB |
Output is correct |
33 |
Correct |
8 ms |
5204 KB |
Output is correct |
34 |
Correct |
8 ms |
5332 KB |
Output is correct |
35 |
Correct |
8 ms |
5288 KB |
Output is correct |
36 |
Correct |
9 ms |
5204 KB |
Output is correct |
37 |
Correct |
9 ms |
5204 KB |
Output is correct |
38 |
Correct |
10 ms |
5332 KB |
Output is correct |
39 |
Correct |
9 ms |
5332 KB |
Output is correct |
40 |
Correct |
11 ms |
5284 KB |
Output is correct |
41 |
Correct |
9 ms |
5288 KB |
Output is correct |
42 |
Correct |
8 ms |
5332 KB |
Output is correct |
43 |
Correct |
11 ms |
5420 KB |
Output is correct |
44 |
Correct |
8 ms |
5204 KB |
Output is correct |
45 |
Correct |
4 ms |
4948 KB |
Output is correct |
46 |
Correct |
463 ms |
31476 KB |
Output is correct |
47 |
Correct |
495 ms |
49452 KB |
Output is correct |
48 |
Correct |
406 ms |
30168 KB |
Output is correct |
49 |
Correct |
449 ms |
31736 KB |
Output is correct |
50 |
Correct |
437 ms |
30548 KB |
Output is correct |
51 |
Correct |
449 ms |
31768 KB |
Output is correct |
52 |
Correct |
439 ms |
30524 KB |
Output is correct |
53 |
Correct |
418 ms |
31692 KB |
Output is correct |
54 |
Correct |
464 ms |
34260 KB |
Output is correct |
55 |
Correct |
454 ms |
31944 KB |
Output is correct |
56 |
Correct |
397 ms |
31228 KB |
Output is correct |
57 |
Correct |
372 ms |
32484 KB |
Output is correct |
58 |
Correct |
503 ms |
44032 KB |
Output is correct |
59 |
Correct |
353 ms |
30808 KB |
Output is correct |
60 |
Correct |
4 ms |
4948 KB |
Output is correct |
61 |
Correct |
761 ms |
34256 KB |
Output is correct |
62 |
Correct |
779 ms |
51216 KB |
Output is correct |
63 |
Correct |
708 ms |
32800 KB |
Output is correct |
64 |
Correct |
752 ms |
34672 KB |
Output is correct |
65 |
Correct |
735 ms |
33028 KB |
Output is correct |
66 |
Correct |
800 ms |
34584 KB |
Output is correct |
67 |
Correct |
727 ms |
33112 KB |
Output is correct |
68 |
Correct |
762 ms |
34668 KB |
Output is correct |
69 |
Correct |
781 ms |
36868 KB |
Output is correct |
70 |
Correct |
774 ms |
34620 KB |
Output is correct |
71 |
Correct |
710 ms |
33876 KB |
Output is correct |
72 |
Correct |
755 ms |
35824 KB |
Output is correct |
73 |
Correct |
958 ms |
46208 KB |
Output is correct |
74 |
Correct |
678 ms |
35088 KB |
Output is correct |