#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, Q, Sum, CostTo1, C[202020], R[202020];
vector<pair<int,int>> G[202020];
int S[202020], U[202020];
void TreeDP(int v, int b=-1, ll up=0, ll dw=0){
for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
C[v] = dw - up;
for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
}
ll CostToRoot(int root){
return CostTo1 + C[root];
}
vector<pair<ll,ll>> CostFromRoot(int root){
vector<pair<ll,ll>> vec;
function<ll(int,int,int)> dfs = [&](int st, int v, int b) -> ll {
ll mx = 0;
for(auto [i,w] : G[v]){
if(i == b || U[i]) continue;
ll nxt = dfs(st, i, v) + w;
if(nxt > mx) swap(nxt, mx);
if(nxt > 0) vec.emplace_back(nxt, st);
}
return mx;
};
for(auto [i,w] : G[root]) if(!U[i]) vec.emplace_back(dfs(i, i, -1) + w, i);
return vec;
}
int GetSize(int v, int b=-1){
S[v] = 1;
for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v);
return S[v];
}
int GetCent(int v, int tot, int b=-1){
for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) return GetCent(i, tot, v);
return v;
}
void GetAnswer(int v=1){
v = GetCent(v, GetSize(v)); U[v] = 1;
auto vec = CostFromRoot(v);
sort(vec.begin(), vec.end(), greater<>());
ll now = CostToRoot(v);
for(int k=2; k-2<vec.size(); k++){
now += vec[k-2].first;
R[k] = min(R[k], Sum - now);
}
int mx2 = -1;
for(int i=1; i<vec.size(); i++) if(vec[i].second != vec[0].second) { mx2 = i; break; }
if(mx2 != -1){
now = CostToRoot(v) + vec[mx2].first;
vec.erase(vec.begin()+mx2);
for(int k=2; k-2<vec.size(); k++){
now += vec[k-2].first;
R[k] = min(R[k], Sum - now);
}
}
for(auto [i,w] : G[v]) if(!U[i]) GetAnswer(i);
}
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; Sum += c + d;
G[a].emplace_back(b, c); G[b].emplace_back(a, d);
}
TreeDP(1);
memset(R, 0x3f, sizeof R);
R[1] = Sum - CostTo1 - *max_element(C+1, C+N+1);
GetAnswer();
for(int i=2; i<=N; i++) R[i] = min(R[i], R[i-1]);
cin >> Q;
for(int i=1; i<=Q; i++){
int t; cin >> t;
cout << R[t] << "\n";
}
}
Compilation message
designated_cities.cpp: In function 'void TreeDP(int, int, ll, ll)':
designated_cities.cpp:10:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
10 | for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
| ^
designated_cities.cpp:12:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
12 | for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
| ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:23:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [i,w] : G[v]){
| ^
designated_cities.cpp: In function 'std::vector<std::pair<long long int, long long int> > CostFromRoot(int)':
designated_cities.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [i,w] : G[root]) if(!U[i]) vec.emplace_back(dfs(i, i, -1) + w, i);
| ^
designated_cities.cpp: In function 'int GetSize(int, int)':
designated_cities.cpp:37:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v);
| ^
designated_cities.cpp: In function 'int GetCent(int, int, int)':
designated_cities.cpp:42:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) return GetCent(i, tot, v);
| ^
designated_cities.cpp: In function 'void GetAnswer(int)':
designated_cities.cpp:52:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int k=2; k-2<vec.size(); k++){
| ~~~^~~~~~~~~~~
designated_cities.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=1; i<vec.size(); i++) if(vec[i].second != vec[0].second) { mx2 = i; break; }
| ~^~~~~~~~~~~
designated_cities.cpp:62:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int k=2; k-2<vec.size(); k++){
| ~~~^~~~~~~~~~~
designated_cities.cpp:68:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
68 | for(auto [i,w] : G[v]) if(!U[i]) GetAnswer(i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6624 KB |
Output is correct |
3 |
Correct |
4 ms |
6672 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6616 KB |
Output is correct |
8 |
Correct |
4 ms |
6620 KB |
Output is correct |
9 |
Correct |
4 ms |
6616 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
5 ms |
6652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
606 ms |
20196 KB |
Output is correct |
3 |
Correct |
924 ms |
31948 KB |
Output is correct |
4 |
Correct |
570 ms |
20372 KB |
Output is correct |
5 |
Correct |
330 ms |
18792 KB |
Output is correct |
6 |
Correct |
714 ms |
22548 KB |
Output is correct |
7 |
Correct |
224 ms |
19524 KB |
Output is correct |
8 |
Correct |
879 ms |
31556 KB |
Output is correct |
9 |
Correct |
186 ms |
21300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6556 KB |
Output is correct |
2 |
Correct |
577 ms |
20608 KB |
Output is correct |
3 |
Correct |
868 ms |
31940 KB |
Output is correct |
4 |
Correct |
553 ms |
20376 KB |
Output is correct |
5 |
Correct |
305 ms |
18908 KB |
Output is correct |
6 |
Correct |
687 ms |
23080 KB |
Output is correct |
7 |
Correct |
185 ms |
21088 KB |
Output is correct |
8 |
Correct |
808 ms |
28864 KB |
Output is correct |
9 |
Correct |
162 ms |
21284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6624 KB |
Output is correct |
3 |
Correct |
4 ms |
6672 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6616 KB |
Output is correct |
8 |
Correct |
4 ms |
6620 KB |
Output is correct |
9 |
Correct |
4 ms |
6616 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
5 ms |
6652 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
6 ms |
6868 KB |
Output is correct |
14 |
Correct |
6 ms |
6868 KB |
Output is correct |
15 |
Correct |
6 ms |
6868 KB |
Output is correct |
16 |
Correct |
8 ms |
6868 KB |
Output is correct |
17 |
Correct |
6 ms |
6888 KB |
Output is correct |
18 |
Correct |
5 ms |
6868 KB |
Output is correct |
19 |
Correct |
6 ms |
6868 KB |
Output is correct |
20 |
Correct |
5 ms |
6892 KB |
Output is correct |
21 |
Correct |
6 ms |
6868 KB |
Output is correct |
22 |
Correct |
5 ms |
6884 KB |
Output is correct |
23 |
Correct |
6 ms |
6868 KB |
Output is correct |
24 |
Correct |
5 ms |
6868 KB |
Output is correct |
25 |
Correct |
6 ms |
6996 KB |
Output is correct |
26 |
Correct |
5 ms |
6888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
606 ms |
20196 KB |
Output is correct |
3 |
Correct |
924 ms |
31948 KB |
Output is correct |
4 |
Correct |
570 ms |
20372 KB |
Output is correct |
5 |
Correct |
330 ms |
18792 KB |
Output is correct |
6 |
Correct |
714 ms |
22548 KB |
Output is correct |
7 |
Correct |
224 ms |
19524 KB |
Output is correct |
8 |
Correct |
879 ms |
31556 KB |
Output is correct |
9 |
Correct |
186 ms |
21300 KB |
Output is correct |
10 |
Correct |
4 ms |
6556 KB |
Output is correct |
11 |
Correct |
577 ms |
20608 KB |
Output is correct |
12 |
Correct |
868 ms |
31940 KB |
Output is correct |
13 |
Correct |
553 ms |
20376 KB |
Output is correct |
14 |
Correct |
305 ms |
18908 KB |
Output is correct |
15 |
Correct |
687 ms |
23080 KB |
Output is correct |
16 |
Correct |
185 ms |
21088 KB |
Output is correct |
17 |
Correct |
808 ms |
28864 KB |
Output is correct |
18 |
Correct |
162 ms |
21284 KB |
Output is correct |
19 |
Correct |
3 ms |
6612 KB |
Output is correct |
20 |
Correct |
507 ms |
20256 KB |
Output is correct |
21 |
Correct |
862 ms |
31996 KB |
Output is correct |
22 |
Correct |
619 ms |
20500 KB |
Output is correct |
23 |
Correct |
576 ms |
20412 KB |
Output is correct |
24 |
Correct |
518 ms |
20592 KB |
Output is correct |
25 |
Correct |
567 ms |
20640 KB |
Output is correct |
26 |
Correct |
595 ms |
20396 KB |
Output is correct |
27 |
Correct |
271 ms |
18948 KB |
Output is correct |
28 |
Correct |
668 ms |
22964 KB |
Output is correct |
29 |
Correct |
545 ms |
19928 KB |
Output is correct |
30 |
Correct |
498 ms |
21588 KB |
Output is correct |
31 |
Correct |
208 ms |
20972 KB |
Output is correct |
32 |
Correct |
866 ms |
29080 KB |
Output is correct |
33 |
Correct |
175 ms |
21284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
4 ms |
6624 KB |
Output is correct |
3 |
Correct |
4 ms |
6672 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6616 KB |
Output is correct |
8 |
Correct |
4 ms |
6620 KB |
Output is correct |
9 |
Correct |
4 ms |
6616 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
5 ms |
6652 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
606 ms |
20196 KB |
Output is correct |
14 |
Correct |
924 ms |
31948 KB |
Output is correct |
15 |
Correct |
570 ms |
20372 KB |
Output is correct |
16 |
Correct |
330 ms |
18792 KB |
Output is correct |
17 |
Correct |
714 ms |
22548 KB |
Output is correct |
18 |
Correct |
224 ms |
19524 KB |
Output is correct |
19 |
Correct |
879 ms |
31556 KB |
Output is correct |
20 |
Correct |
186 ms |
21300 KB |
Output is correct |
21 |
Correct |
4 ms |
6556 KB |
Output is correct |
22 |
Correct |
577 ms |
20608 KB |
Output is correct |
23 |
Correct |
868 ms |
31940 KB |
Output is correct |
24 |
Correct |
553 ms |
20376 KB |
Output is correct |
25 |
Correct |
305 ms |
18908 KB |
Output is correct |
26 |
Correct |
687 ms |
23080 KB |
Output is correct |
27 |
Correct |
185 ms |
21088 KB |
Output is correct |
28 |
Correct |
808 ms |
28864 KB |
Output is correct |
29 |
Correct |
162 ms |
21284 KB |
Output is correct |
30 |
Correct |
3 ms |
6612 KB |
Output is correct |
31 |
Correct |
6 ms |
6868 KB |
Output is correct |
32 |
Correct |
6 ms |
6868 KB |
Output is correct |
33 |
Correct |
6 ms |
6868 KB |
Output is correct |
34 |
Correct |
8 ms |
6868 KB |
Output is correct |
35 |
Correct |
6 ms |
6888 KB |
Output is correct |
36 |
Correct |
5 ms |
6868 KB |
Output is correct |
37 |
Correct |
6 ms |
6868 KB |
Output is correct |
38 |
Correct |
5 ms |
6892 KB |
Output is correct |
39 |
Correct |
6 ms |
6868 KB |
Output is correct |
40 |
Correct |
5 ms |
6884 KB |
Output is correct |
41 |
Correct |
6 ms |
6868 KB |
Output is correct |
42 |
Correct |
5 ms |
6868 KB |
Output is correct |
43 |
Correct |
6 ms |
6996 KB |
Output is correct |
44 |
Correct |
5 ms |
6888 KB |
Output is correct |
45 |
Correct |
3 ms |
6612 KB |
Output is correct |
46 |
Correct |
507 ms |
20256 KB |
Output is correct |
47 |
Correct |
862 ms |
31996 KB |
Output is correct |
48 |
Correct |
619 ms |
20500 KB |
Output is correct |
49 |
Correct |
576 ms |
20412 KB |
Output is correct |
50 |
Correct |
518 ms |
20592 KB |
Output is correct |
51 |
Correct |
567 ms |
20640 KB |
Output is correct |
52 |
Correct |
595 ms |
20396 KB |
Output is correct |
53 |
Correct |
271 ms |
18948 KB |
Output is correct |
54 |
Correct |
668 ms |
22964 KB |
Output is correct |
55 |
Correct |
545 ms |
19928 KB |
Output is correct |
56 |
Correct |
498 ms |
21588 KB |
Output is correct |
57 |
Correct |
208 ms |
20972 KB |
Output is correct |
58 |
Correct |
866 ms |
29080 KB |
Output is correct |
59 |
Correct |
175 ms |
21284 KB |
Output is correct |
60 |
Correct |
4 ms |
6612 KB |
Output is correct |
61 |
Correct |
596 ms |
20060 KB |
Output is correct |
62 |
Correct |
937 ms |
32332 KB |
Output is correct |
63 |
Correct |
542 ms |
20236 KB |
Output is correct |
64 |
Correct |
576 ms |
20352 KB |
Output is correct |
65 |
Correct |
611 ms |
20232 KB |
Output is correct |
66 |
Correct |
584 ms |
20296 KB |
Output is correct |
67 |
Correct |
625 ms |
20376 KB |
Output is correct |
68 |
Correct |
306 ms |
18864 KB |
Output is correct |
69 |
Correct |
697 ms |
23176 KB |
Output is correct |
70 |
Correct |
603 ms |
20436 KB |
Output is correct |
71 |
Correct |
507 ms |
20872 KB |
Output is correct |
72 |
Correct |
256 ms |
20660 KB |
Output is correct |
73 |
Correct |
851 ms |
29156 KB |
Output is correct |
74 |
Correct |
187 ms |
21164 KB |
Output is correct |