#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
struct no{
vector<pair<int,pair<int,int> > > ch;
int sum=0;
int sum2=0;
pair<int,int> dp={0,-1},dp2={0,-1};
};
int n;
int zz=0;
vector<no> v;
vector<int> ans;
void dfs(int r,int f){
for(auto h:v[r].ch){
if(h.fs!=f){
dfs(h.fs,r);
v[r].sum+=v[h.fs].sum+h.sc.sc;
}
}
}
void dfs2(int r,int f,int z=0){
v[r].sum+=z;
ans[1]=max(ans[1],v[r].sum);
for(auto h:v[r].ch){
if(h.fs!=f){
dfs2(h.fs,r,h.sc.fs+v[r].sum-v[h.fs].sum-h.sc.sc);
}
}
}
void calc1(){
dfs(0,0);
ans[1]=max(ans[1],v[0].sum);
dfs2(0,0);
}
vector<int> t;
void dfs3(int r,int f){
vector<int> g;
for(auto h:v[r].ch){
if(h.fs!=f){
dfs3(h.fs,r);
g.push_back(v[h.fs].sum2+h.sc.fs);
}
}
sort(g.begin(),g.end(),greater<int>());
if(!g.size()){
return;
}
if(g.size()>1){
for(int i=1;i<g.size();i++){
t.push_back(g[i]);
}
}
v[r].sum2=g[0];
}
pair<int,pair<int,int> > dfs4(int r,int f){
pair<int,pair<int,int> > ret={0,{-1,-1}};
auto c=v[r].dp,d=v[r].dp2;
for(auto h:v[r].ch){
if(h.fs!=f){
ret=max(ret,dfs4(h.fs,r));
ret=max(ret,{v[r].dp.fs+v[h.fs].dp2.fs+h.sc.fs,{v[r].dp.sc,v[h.fs].dp2.sc}});
ret=max(ret,{v[r].dp2.fs+v[h.fs].dp.fs+h.sc.sc,{v[r].dp2.sc,v[h.fs].dp.sc}});
v[r].dp=max(v[r].dp,{v[h.fs].dp.fs+h.sc.sc,v[h.fs].dp.sc});
v[r].dp2=max(v[r].dp2,{v[h.fs].dp2.fs+h.sc.fs,v[h.fs].dp2.sc});
}
}
reverse(v[r].ch.begin(),v[r].ch.end());
v[r].dp=c;
v[r].dp2=d;
for(auto h:v[r].ch){
if(h.fs!=f){
ret=max(ret,{v[r].dp.fs+v[h.fs].dp2.fs+h.sc.fs,{v[r].dp.sc,v[h.fs].dp2.sc}});
ret=max(ret,{v[r].dp2.fs+v[h.fs].dp.fs+h.sc.sc,{v[r].dp2.sc,v[h.fs].dp.sc}});
v[r].dp=max(v[r].dp,{v[h.fs].dp.fs+h.sc.sc,v[h.fs].dp.sc});
v[r].dp2=max(v[r].dp2,{v[h.fs].dp2.fs+h.sc.fs,v[h.fs].dp2.sc});
}
}
return ret;
}
void calc2(){
for(int i=0;i<n;i++){
if(v[i].ch.size()==1){
v[i].dp.fs=v[i].sum;
v[i].dp.sc=i;
v[i].dp2.sc=i;
}
}
auto h=dfs4(0,0);
for(int i=0;i<n;i++){
if(i==h.sc.fs || i==h.sc.sc){
for(int j=0;j<n;j++){
v[j].sum2=0;
}
t.clear();
dfs3(i,i);
t.push_back(v[i].sum2);
sort(t.begin(),t.end(),greater<int>());
int nw=v[i].sum;
while(t.size()<=n){
t.push_back(0);
}
for(int j=2;j<=n;j++){
nw+=t[j-2];
ans[j]=max(ans[j],nw);
}
}
}
assert(ans[2]==h.fs);
}
signed main(){
AquA;
cin >> n;
v.resize(n);
for(int i=1;i<n;i++){
int a,b;
cin >> a >> b;
a--;
b--;
int c,d;
cin >> c >> d;
zz+=c+d;
v[a].ch.push_back({b,{c,d}});
v[b].ch.push_back({a,{d,c}});
}
ans.resize(n+1);
calc1();
calc2();
int q;
cin >> q;
for(int i=0;i<q;i++){
int a;
cin >> a;
cout << zz-ans[a] << "\n";
}
return 0;
}
Compilation message
designated_cities.cpp: In function 'void dfs3(long long int, long long int)':
designated_cities.cpp:56:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int i=1;i<g.size();i++){
| ~^~~~~~~~~
designated_cities.cpp: In function 'void calc2()':
designated_cities.cpp:106:27: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
106 | while(t.size()<=n){
| ~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
373 ms |
31192 KB |
Output is correct |
3 |
Correct |
470 ms |
59360 KB |
Output is correct |
4 |
Correct |
375 ms |
31192 KB |
Output is correct |
5 |
Correct |
346 ms |
32236 KB |
Output is correct |
6 |
Correct |
405 ms |
35328 KB |
Output is correct |
7 |
Correct |
294 ms |
31764 KB |
Output is correct |
8 |
Correct |
474 ms |
60112 KB |
Output is correct |
9 |
Correct |
243 ms |
32052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
372 ms |
31248 KB |
Output is correct |
3 |
Correct |
477 ms |
70336 KB |
Output is correct |
4 |
Correct |
371 ms |
36260 KB |
Output is correct |
5 |
Correct |
357 ms |
38760 KB |
Output is correct |
6 |
Correct |
400 ms |
42264 KB |
Output is correct |
7 |
Correct |
237 ms |
38404 KB |
Output is correct |
8 |
Correct |
458 ms |
55824 KB |
Output is correct |
9 |
Correct |
250 ms |
38224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
852 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
852 KB |
Output is correct |
26 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
373 ms |
31192 KB |
Output is correct |
3 |
Correct |
470 ms |
59360 KB |
Output is correct |
4 |
Correct |
375 ms |
31192 KB |
Output is correct |
5 |
Correct |
346 ms |
32236 KB |
Output is correct |
6 |
Correct |
405 ms |
35328 KB |
Output is correct |
7 |
Correct |
294 ms |
31764 KB |
Output is correct |
8 |
Correct |
474 ms |
60112 KB |
Output is correct |
9 |
Correct |
243 ms |
32052 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
372 ms |
31248 KB |
Output is correct |
12 |
Correct |
477 ms |
70336 KB |
Output is correct |
13 |
Correct |
371 ms |
36260 KB |
Output is correct |
14 |
Correct |
357 ms |
38760 KB |
Output is correct |
15 |
Correct |
400 ms |
42264 KB |
Output is correct |
16 |
Correct |
237 ms |
38404 KB |
Output is correct |
17 |
Correct |
458 ms |
55824 KB |
Output is correct |
18 |
Correct |
250 ms |
38224 KB |
Output is correct |
19 |
Correct |
0 ms |
328 KB |
Output is correct |
20 |
Correct |
390 ms |
37740 KB |
Output is correct |
21 |
Correct |
473 ms |
70760 KB |
Output is correct |
22 |
Correct |
381 ms |
36320 KB |
Output is correct |
23 |
Correct |
365 ms |
37924 KB |
Output is correct |
24 |
Correct |
351 ms |
36744 KB |
Output is correct |
25 |
Correct |
357 ms |
37932 KB |
Output is correct |
26 |
Correct |
353 ms |
36692 KB |
Output is correct |
27 |
Correct |
369 ms |
38180 KB |
Output is correct |
28 |
Correct |
376 ms |
41572 KB |
Output is correct |
29 |
Correct |
379 ms |
38448 KB |
Output is correct |
30 |
Correct |
342 ms |
37292 KB |
Output is correct |
31 |
Correct |
281 ms |
38704 KB |
Output is correct |
32 |
Correct |
464 ms |
57096 KB |
Output is correct |
33 |
Correct |
262 ms |
38688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
373 ms |
31192 KB |
Output is correct |
14 |
Correct |
470 ms |
59360 KB |
Output is correct |
15 |
Correct |
375 ms |
31192 KB |
Output is correct |
16 |
Correct |
346 ms |
32236 KB |
Output is correct |
17 |
Correct |
405 ms |
35328 KB |
Output is correct |
18 |
Correct |
294 ms |
31764 KB |
Output is correct |
19 |
Correct |
474 ms |
60112 KB |
Output is correct |
20 |
Correct |
243 ms |
32052 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
372 ms |
31248 KB |
Output is correct |
23 |
Correct |
477 ms |
70336 KB |
Output is correct |
24 |
Correct |
371 ms |
36260 KB |
Output is correct |
25 |
Correct |
357 ms |
38760 KB |
Output is correct |
26 |
Correct |
400 ms |
42264 KB |
Output is correct |
27 |
Correct |
237 ms |
38404 KB |
Output is correct |
28 |
Correct |
458 ms |
55824 KB |
Output is correct |
29 |
Correct |
250 ms |
38224 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
596 KB |
Output is correct |
38 |
Correct |
3 ms |
596 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
3 ms |
596 KB |
Output is correct |
42 |
Correct |
3 ms |
596 KB |
Output is correct |
43 |
Correct |
2 ms |
852 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
0 ms |
328 KB |
Output is correct |
46 |
Correct |
390 ms |
37740 KB |
Output is correct |
47 |
Correct |
473 ms |
70760 KB |
Output is correct |
48 |
Correct |
381 ms |
36320 KB |
Output is correct |
49 |
Correct |
365 ms |
37924 KB |
Output is correct |
50 |
Correct |
351 ms |
36744 KB |
Output is correct |
51 |
Correct |
357 ms |
37932 KB |
Output is correct |
52 |
Correct |
353 ms |
36692 KB |
Output is correct |
53 |
Correct |
369 ms |
38180 KB |
Output is correct |
54 |
Correct |
376 ms |
41572 KB |
Output is correct |
55 |
Correct |
379 ms |
38448 KB |
Output is correct |
56 |
Correct |
342 ms |
37292 KB |
Output is correct |
57 |
Correct |
281 ms |
38704 KB |
Output is correct |
58 |
Correct |
464 ms |
57096 KB |
Output is correct |
59 |
Correct |
262 ms |
38688 KB |
Output is correct |
60 |
Correct |
1 ms |
320 KB |
Output is correct |
61 |
Correct |
419 ms |
39780 KB |
Output is correct |
62 |
Correct |
501 ms |
67180 KB |
Output is correct |
63 |
Correct |
393 ms |
38508 KB |
Output is correct |
64 |
Correct |
414 ms |
40036 KB |
Output is correct |
65 |
Correct |
390 ms |
38740 KB |
Output is correct |
66 |
Correct |
387 ms |
40156 KB |
Output is correct |
67 |
Correct |
396 ms |
38828 KB |
Output is correct |
68 |
Correct |
394 ms |
40224 KB |
Output is correct |
69 |
Correct |
408 ms |
43344 KB |
Output is correct |
70 |
Correct |
402 ms |
40576 KB |
Output is correct |
71 |
Correct |
379 ms |
40116 KB |
Output is correct |
72 |
Correct |
326 ms |
40944 KB |
Output is correct |
73 |
Correct |
519 ms |
56116 KB |
Output is correct |
74 |
Correct |
299 ms |
42744 KB |
Output is correct |