#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, Q, Sum, C1, C[202020], R[202020];
vector<pair<ll,ll>> G[202020];
int S[202020], U[202020];
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 n, int b=-1){
for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > n) return GetCent(i, n, v);
return v;
}
void TreeDP(int v, int b=-1, ll up=0, ll dw=0){
for(auto [i,w] : G[v]) if(i == b) C1 += 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 C1 + C[root];
}
vector<pair<ll,ll>> PathsFromRoot(int root){
vector<pair<ll,ll>> paths;
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(mx, nxt);
if(mx != 0) paths.emplace_back(nxt, st);
}
return mx;
};
for(auto [i,w] : G[root]) if(!U[i]) paths.emplace_back(dfs(i, i, root) + w, i);
return paths;
}
void Go(int root){
root = GetCent(root, GetSize(root)); U[root] = 1;
ll to_root = CostToRoot(root);
vector<pair<ll,ll>> paths = PathsFromRoot(root);
sort(paths.begin(), paths.end(), greater<>());
// case 1. only root
R[1] = max(R[1], to_root);
if(paths.empty()) return;
// case 2. root and some leaves
ll cost2 = to_root + paths[0].first;
R[2] = max(R[2], cost2);
for(int i=1; i<paths.size(); i++) R[i+2] = max(R[i+2], cost2 += paths[i].first);
// case 3. only leaves
int idx = find_if(paths.begin(), paths.end(), [&](auto v){ return paths[0].second != v.second; }) - paths.begin();
if(idx != paths.size()){
ll cost3 = to_root + paths[0].first + paths[idx].first;
paths.erase(paths.begin() + idx);
R[2] = max(R[2], cost3);
for(int i=1; i<paths.size(); i++) R[i+2] = max(R[i+2], cost3 += paths[i].first);
}
for(auto [i,w] : G[root]) if(!U[i]) Go(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);
Go(1);
for(int i=2; i<=N; i++) R[i] = max(R[i], R[i-1]);
cin >> Q;
for(int i=1,t; i<=Q; i++) cin >> t, cout << Sum - R[t] << "\n";
}
Compilation message
designated_cities.cpp: In function 'int GetSize(int, int)':
designated_cities.cpp:11:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
11 | 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:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
16 | for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > n) return GetCent(i, n, v);
| ^
designated_cities.cpp: In function 'void TreeDP(int, int, ll, ll)':
designated_cities.cpp:21:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
21 | for(auto [i,w] : G[v]) if(i == b) C1 += w, up += w;
| ^
designated_cities.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
23 | for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
| ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:34:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [i,w] : G[v]){
| ^
designated_cities.cpp: In function 'std::vector<std::pair<long long int, long long int> > PathsFromRoot(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[root]) if(!U[i]) paths.emplace_back(dfs(i, i, root) + w, i);
| ^
designated_cities.cpp: In function 'void Go(int)':
designated_cities.cpp:60: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]
60 | for(int i=1; i<paths.size(); i++) R[i+2] = max(R[i+2], cost2 += paths[i].first);
| ~^~~~~~~~~~~~~
designated_cities.cpp:64:12: 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]
64 | if(idx != paths.size()){
| ~~~~^~~~~~~~~~~~~~~
designated_cities.cpp:68:23: 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]
68 | for(int i=1; i<paths.size(); i++) R[i+2] = max(R[i+2], cost3 += paths[i].first);
| ~^~~~~~~~~~~~~
designated_cities.cpp:71:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
71 | for(auto [i,w] : G[root]) if(!U[i]) Go(i);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
624 ms |
26404 KB |
Output is correct |
3 |
Correct |
862 ms |
42472 KB |
Output is correct |
4 |
Correct |
581 ms |
26684 KB |
Output is correct |
5 |
Correct |
335 ms |
23132 KB |
Output is correct |
6 |
Correct |
733 ms |
28968 KB |
Output is correct |
7 |
Correct |
221 ms |
22608 KB |
Output is correct |
8 |
Correct |
934 ms |
41872 KB |
Output is correct |
9 |
Correct |
150 ms |
22960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
627 ms |
26828 KB |
Output is correct |
3 |
Correct |
863 ms |
49020 KB |
Output is correct |
4 |
Correct |
632 ms |
31852 KB |
Output is correct |
5 |
Correct |
301 ms |
29584 KB |
Output is correct |
6 |
Correct |
704 ms |
35892 KB |
Output is correct |
7 |
Correct |
187 ms |
29232 KB |
Output is correct |
8 |
Correct |
895 ms |
43368 KB |
Output is correct |
9 |
Correct |
157 ms |
29000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
6 ms |
5204 KB |
Output is correct |
14 |
Correct |
6 ms |
5460 KB |
Output is correct |
15 |
Correct |
6 ms |
5308 KB |
Output is correct |
16 |
Correct |
6 ms |
5332 KB |
Output is correct |
17 |
Correct |
6 ms |
5312 KB |
Output is correct |
18 |
Correct |
5 ms |
5332 KB |
Output is correct |
19 |
Correct |
5 ms |
5324 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
5 ms |
5228 KB |
Output is correct |
22 |
Correct |
5 ms |
5332 KB |
Output is correct |
23 |
Correct |
5 ms |
5232 KB |
Output is correct |
24 |
Correct |
4 ms |
5332 KB |
Output is correct |
25 |
Correct |
5 ms |
5452 KB |
Output is correct |
26 |
Correct |
4 ms |
5320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
624 ms |
26404 KB |
Output is correct |
3 |
Correct |
862 ms |
42472 KB |
Output is correct |
4 |
Correct |
581 ms |
26684 KB |
Output is correct |
5 |
Correct |
335 ms |
23132 KB |
Output is correct |
6 |
Correct |
733 ms |
28968 KB |
Output is correct |
7 |
Correct |
221 ms |
22608 KB |
Output is correct |
8 |
Correct |
934 ms |
41872 KB |
Output is correct |
9 |
Correct |
150 ms |
22960 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
627 ms |
26828 KB |
Output is correct |
12 |
Correct |
863 ms |
49020 KB |
Output is correct |
13 |
Correct |
632 ms |
31852 KB |
Output is correct |
14 |
Correct |
301 ms |
29584 KB |
Output is correct |
15 |
Correct |
704 ms |
35892 KB |
Output is correct |
16 |
Correct |
187 ms |
29232 KB |
Output is correct |
17 |
Correct |
895 ms |
43368 KB |
Output is correct |
18 |
Correct |
157 ms |
29000 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
631 ms |
32980 KB |
Output is correct |
21 |
Correct |
946 ms |
49708 KB |
Output is correct |
22 |
Correct |
612 ms |
31736 KB |
Output is correct |
23 |
Correct |
625 ms |
33356 KB |
Output is correct |
24 |
Correct |
636 ms |
32280 KB |
Output is correct |
25 |
Correct |
621 ms |
33468 KB |
Output is correct |
26 |
Correct |
628 ms |
32248 KB |
Output is correct |
27 |
Correct |
368 ms |
29532 KB |
Output is correct |
28 |
Correct |
707 ms |
35668 KB |
Output is correct |
29 |
Correct |
606 ms |
33532 KB |
Output is correct |
30 |
Correct |
577 ms |
32000 KB |
Output is correct |
31 |
Correct |
207 ms |
28996 KB |
Output is correct |
32 |
Correct |
906 ms |
43916 KB |
Output is correct |
33 |
Correct |
184 ms |
29492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
2 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
624 ms |
26404 KB |
Output is correct |
14 |
Correct |
862 ms |
42472 KB |
Output is correct |
15 |
Correct |
581 ms |
26684 KB |
Output is correct |
16 |
Correct |
335 ms |
23132 KB |
Output is correct |
17 |
Correct |
733 ms |
28968 KB |
Output is correct |
18 |
Correct |
221 ms |
22608 KB |
Output is correct |
19 |
Correct |
934 ms |
41872 KB |
Output is correct |
20 |
Correct |
150 ms |
22960 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
627 ms |
26828 KB |
Output is correct |
23 |
Correct |
863 ms |
49020 KB |
Output is correct |
24 |
Correct |
632 ms |
31852 KB |
Output is correct |
25 |
Correct |
301 ms |
29584 KB |
Output is correct |
26 |
Correct |
704 ms |
35892 KB |
Output is correct |
27 |
Correct |
187 ms |
29232 KB |
Output is correct |
28 |
Correct |
895 ms |
43368 KB |
Output is correct |
29 |
Correct |
157 ms |
29000 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
6 ms |
5204 KB |
Output is correct |
32 |
Correct |
6 ms |
5460 KB |
Output is correct |
33 |
Correct |
6 ms |
5308 KB |
Output is correct |
34 |
Correct |
6 ms |
5332 KB |
Output is correct |
35 |
Correct |
6 ms |
5312 KB |
Output is correct |
36 |
Correct |
5 ms |
5332 KB |
Output is correct |
37 |
Correct |
5 ms |
5324 KB |
Output is correct |
38 |
Correct |
4 ms |
5204 KB |
Output is correct |
39 |
Correct |
5 ms |
5228 KB |
Output is correct |
40 |
Correct |
5 ms |
5332 KB |
Output is correct |
41 |
Correct |
5 ms |
5232 KB |
Output is correct |
42 |
Correct |
4 ms |
5332 KB |
Output is correct |
43 |
Correct |
5 ms |
5452 KB |
Output is correct |
44 |
Correct |
4 ms |
5320 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
631 ms |
32980 KB |
Output is correct |
47 |
Correct |
946 ms |
49708 KB |
Output is correct |
48 |
Correct |
612 ms |
31736 KB |
Output is correct |
49 |
Correct |
625 ms |
33356 KB |
Output is correct |
50 |
Correct |
636 ms |
32280 KB |
Output is correct |
51 |
Correct |
621 ms |
33468 KB |
Output is correct |
52 |
Correct |
628 ms |
32248 KB |
Output is correct |
53 |
Correct |
368 ms |
29532 KB |
Output is correct |
54 |
Correct |
707 ms |
35668 KB |
Output is correct |
55 |
Correct |
606 ms |
33532 KB |
Output is correct |
56 |
Correct |
577 ms |
32000 KB |
Output is correct |
57 |
Correct |
207 ms |
28996 KB |
Output is correct |
58 |
Correct |
906 ms |
43916 KB |
Output is correct |
59 |
Correct |
184 ms |
29492 KB |
Output is correct |
60 |
Correct |
3 ms |
5088 KB |
Output is correct |
61 |
Correct |
638 ms |
33080 KB |
Output is correct |
62 |
Correct |
920 ms |
49248 KB |
Output is correct |
63 |
Correct |
669 ms |
31856 KB |
Output is correct |
64 |
Correct |
620 ms |
33472 KB |
Output is correct |
65 |
Correct |
593 ms |
32212 KB |
Output is correct |
66 |
Correct |
630 ms |
33684 KB |
Output is correct |
67 |
Correct |
720 ms |
32512 KB |
Output is correct |
68 |
Correct |
397 ms |
29712 KB |
Output is correct |
69 |
Correct |
784 ms |
35812 KB |
Output is correct |
70 |
Correct |
651 ms |
34372 KB |
Output is correct |
71 |
Correct |
533 ms |
31836 KB |
Output is correct |
72 |
Correct |
271 ms |
29236 KB |
Output is correct |
73 |
Correct |
902 ms |
44004 KB |
Output is correct |
74 |
Correct |
187 ms |
29944 KB |
Output is correct |