#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
vector<pair<ll,ll>>V[LIM];
pair<ll,ll>sred={-1, -1};
ll wynik[LIM], sum, dp[LIM], dp_up[LIM], oc[LIM], ile[LIM];
void DFS(int x, int o) {
for(auto i : V[x]) if(i.st==o) oc[x]=i.nd; else DFS(i.st, x);
sum+=oc[x];
}
void DFS2(int x, int o) {
for(auto i : V[x]) if(i.st!=o) {
DFS2(i.st, x);
dp[x]=max(dp[x], dp[i.st]+i.nd);
}
}
void DFS3(int x, int o) {
pair<ll,ll>ma={-1, -1};
for(auto i : V[x]) if(i.st!=o) {
ma=max(ma, {dp[i.st]+i.nd, i.st});
}
for(auto i : V[x]) if(i.st!=o) {
dp_up[i.st]=max(dp_up[i.st], dp_up[x]+oc[i.st]);
if(i.st!=ma.nd) {
dp_up[i.st]=max(dp_up[i.st], ma.st+oc[i.st]);
} else {
for(auto j : V[x]) if(j.st!=o && j.st!=ma.nd) {
dp_up[i.st]=max(dp_up[i.st], dp[j.st]+j.nd+oc[i.st]);
}
}
}
for(auto i : V[x]) if(i.st!=o) DFS3(i.st, x);
}
void DFS4(int x, int o) {
for(auto i : V[x]) if(i.st==o) sum-=i.nd;
wynik[1]=max(wynik[1], sum);
sred=max(sred, {sum+max(dp[x], dp_up[x]), x});
for(auto i : V[x]) if(i.st!=o) {
sum+=i.nd;
DFS4(i.st, x);
sum-=i.nd;
}
for(auto i : V[x]) if(i.st==o) sum+=i.nd;
}
void DFS5(int x, int o) {
ile[x]=1;
for(auto i : V[x]) if(i.st!=o) {
DFS5(i.st, x);
ile[x]+=ile[i.st];
}
rep(i, V[x].size()) if(V[x][0].st==o || (V[x][i].st!=o && ile[V[x][i].st]>ile[V[x][0].st])) swap(V[x][0], V[x][i]);
}
void DFS6(int x, int o, multiset<ll>&S) {
if(V[x][0].st==o) {
S.insert(0);
return;
}
DFS6(V[x][0].st, x, S);
auto it=S.end(); --it;
auto a=*it;
S.erase(it);
S.insert(a+V[x][0].nd);
for(int i=1; i<V[x].size(); ++i) if(V[x][i].st!=o) {
multiset<ll>tmp;
DFS6(V[x][i].st, x, tmp);
it=tmp.end(); --it;
a=*it;
tmp.erase(it);
tmp.insert(a+V[x][i].nd);
for(auto j : tmp) S.insert(j);
tmp.clear();
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
ll summ=0;
rep(i, n-1) {
ll a, b, c, d;
cin >> a >> b >> c >> d; --a; --b;
V[a].pb({b, c});
V[b].pb({a, d});
summ+=c+d;
}
DFS(0, 0);
DFS2(0, 0);
DFS3(0, 0);
DFS4(0, 0);
oc[sred.nd]=0;
sum=0;
DFS(sred.nd, sred.nd);
DFS5(sred.nd, sred.nd);
multiset<ll>A;
DFS6(sred.nd, sred.nd, A);
rep(i, n) {
if(A.size()==0) break;
auto it=A.end(); --it;
auto a=*it;
A.erase(it);
sum+=a;
wynik[i+2]=sum;
}
rep(i, n) wynik[i+1]=max(wynik[i+1], wynik[i]);
rep(i, n+1) wynik[i]=summ-wynik[i];
int q;
cin >> q;
while(q--) {
int x;
cin >> x;
cout << wynik[x] << '\n';
}
}
Compilation message
designated_cities.cpp: In function 'void DFS5(int, int)':
designated_cities.cpp:5:36: 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]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
designated_cities.cpp:58:3: note: in expansion of macro 'rep'
58 | rep(i, V[x].size()) if(V[x][0].st==o || (V[x][i].st!=o && ile[V[x][i].st]>ile[V[x][0].st])) swap(V[x][0], V[x][i]);
| ^~~
designated_cities.cpp: In function 'void DFS6(int, int, std::multiset<long long int>&)':
designated_cities.cpp:70:17: 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]
70 | for(int i=1; i<V[x].size(); ++i) if(V[x][i].st!=o) {
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10752 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10752 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
155 ms |
27088 KB |
Output is correct |
3 |
Correct |
204 ms |
60792 KB |
Output is correct |
4 |
Correct |
172 ms |
27472 KB |
Output is correct |
5 |
Correct |
148 ms |
27252 KB |
Output is correct |
6 |
Correct |
166 ms |
31568 KB |
Output is correct |
7 |
Correct |
138 ms |
28352 KB |
Output is correct |
8 |
Correct |
216 ms |
59476 KB |
Output is correct |
9 |
Correct |
111 ms |
31696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
165 ms |
27196 KB |
Output is correct |
3 |
Correct |
195 ms |
60756 KB |
Output is correct |
4 |
Correct |
136 ms |
26964 KB |
Output is correct |
5 |
Correct |
166 ms |
27464 KB |
Output is correct |
6 |
Correct |
157 ms |
33128 KB |
Output is correct |
7 |
Correct |
119 ms |
30808 KB |
Output is correct |
8 |
Correct |
234 ms |
49488 KB |
Output is correct |
9 |
Correct |
118 ms |
31668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10752 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10752 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
3 ms |
11020 KB |
Output is correct |
16 |
Correct |
3 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
3 ms |
10868 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10848 KB |
Output is correct |
22 |
Correct |
3 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
11040 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
155 ms |
27088 KB |
Output is correct |
3 |
Correct |
204 ms |
60792 KB |
Output is correct |
4 |
Correct |
172 ms |
27472 KB |
Output is correct |
5 |
Correct |
148 ms |
27252 KB |
Output is correct |
6 |
Correct |
166 ms |
31568 KB |
Output is correct |
7 |
Correct |
138 ms |
28352 KB |
Output is correct |
8 |
Correct |
216 ms |
59476 KB |
Output is correct |
9 |
Correct |
111 ms |
31696 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
165 ms |
27196 KB |
Output is correct |
12 |
Correct |
195 ms |
60756 KB |
Output is correct |
13 |
Correct |
136 ms |
26964 KB |
Output is correct |
14 |
Correct |
166 ms |
27464 KB |
Output is correct |
15 |
Correct |
157 ms |
33128 KB |
Output is correct |
16 |
Correct |
119 ms |
30808 KB |
Output is correct |
17 |
Correct |
234 ms |
49488 KB |
Output is correct |
18 |
Correct |
118 ms |
31668 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
188 ms |
27108 KB |
Output is correct |
21 |
Correct |
240 ms |
67124 KB |
Output is correct |
22 |
Correct |
162 ms |
32760 KB |
Output is correct |
23 |
Correct |
180 ms |
33908 KB |
Output is correct |
24 |
Correct |
164 ms |
33108 KB |
Output is correct |
25 |
Correct |
173 ms |
33624 KB |
Output is correct |
26 |
Correct |
196 ms |
32536 KB |
Output is correct |
27 |
Correct |
161 ms |
33680 KB |
Output is correct |
28 |
Correct |
174 ms |
38664 KB |
Output is correct |
29 |
Correct |
189 ms |
33684 KB |
Output is correct |
30 |
Correct |
158 ms |
33068 KB |
Output is correct |
31 |
Correct |
141 ms |
35388 KB |
Output is correct |
32 |
Correct |
246 ms |
56804 KB |
Output is correct |
33 |
Correct |
129 ms |
38108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10752 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10752 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
155 ms |
27088 KB |
Output is correct |
14 |
Correct |
204 ms |
60792 KB |
Output is correct |
15 |
Correct |
172 ms |
27472 KB |
Output is correct |
16 |
Correct |
148 ms |
27252 KB |
Output is correct |
17 |
Correct |
166 ms |
31568 KB |
Output is correct |
18 |
Correct |
138 ms |
28352 KB |
Output is correct |
19 |
Correct |
216 ms |
59476 KB |
Output is correct |
20 |
Correct |
111 ms |
31696 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
165 ms |
27196 KB |
Output is correct |
23 |
Correct |
195 ms |
60756 KB |
Output is correct |
24 |
Correct |
136 ms |
26964 KB |
Output is correct |
25 |
Correct |
166 ms |
27464 KB |
Output is correct |
26 |
Correct |
157 ms |
33128 KB |
Output is correct |
27 |
Correct |
119 ms |
30808 KB |
Output is correct |
28 |
Correct |
234 ms |
49488 KB |
Output is correct |
29 |
Correct |
118 ms |
31668 KB |
Output is correct |
30 |
Correct |
2 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
3 ms |
11100 KB |
Output is correct |
33 |
Correct |
3 ms |
11020 KB |
Output is correct |
34 |
Correct |
3 ms |
10844 KB |
Output is correct |
35 |
Correct |
3 ms |
10844 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10868 KB |
Output is correct |
38 |
Correct |
3 ms |
10844 KB |
Output is correct |
39 |
Correct |
3 ms |
10848 KB |
Output is correct |
40 |
Correct |
3 ms |
10844 KB |
Output is correct |
41 |
Correct |
3 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
11040 KB |
Output is correct |
43 |
Correct |
3 ms |
11100 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Correct |
188 ms |
27108 KB |
Output is correct |
47 |
Correct |
240 ms |
67124 KB |
Output is correct |
48 |
Correct |
162 ms |
32760 KB |
Output is correct |
49 |
Correct |
180 ms |
33908 KB |
Output is correct |
50 |
Correct |
164 ms |
33108 KB |
Output is correct |
51 |
Correct |
173 ms |
33624 KB |
Output is correct |
52 |
Correct |
196 ms |
32536 KB |
Output is correct |
53 |
Correct |
161 ms |
33680 KB |
Output is correct |
54 |
Correct |
174 ms |
38664 KB |
Output is correct |
55 |
Correct |
189 ms |
33684 KB |
Output is correct |
56 |
Correct |
158 ms |
33068 KB |
Output is correct |
57 |
Correct |
141 ms |
35388 KB |
Output is correct |
58 |
Correct |
246 ms |
56804 KB |
Output is correct |
59 |
Correct |
129 ms |
38108 KB |
Output is correct |
60 |
Correct |
1 ms |
10588 KB |
Output is correct |
61 |
Correct |
193 ms |
35924 KB |
Output is correct |
62 |
Correct |
246 ms |
68904 KB |
Output is correct |
63 |
Correct |
169 ms |
34992 KB |
Output is correct |
64 |
Correct |
181 ms |
36688 KB |
Output is correct |
65 |
Correct |
176 ms |
35668 KB |
Output is correct |
66 |
Correct |
185 ms |
36728 KB |
Output is correct |
67 |
Correct |
207 ms |
36212 KB |
Output is correct |
68 |
Correct |
166 ms |
36552 KB |
Output is correct |
69 |
Correct |
190 ms |
41208 KB |
Output is correct |
70 |
Correct |
198 ms |
36424 KB |
Output is correct |
71 |
Correct |
181 ms |
36120 KB |
Output is correct |
72 |
Correct |
150 ms |
38268 KB |
Output is correct |
73 |
Correct |
263 ms |
58964 KB |
Output is correct |
74 |
Correct |
153 ms |
42364 KB |
Output is correct |