#include<bits/stdc++.h>
#define vla vector <pair <long long, pair <long long, long long> > >
using namespace std;
long long a,b,c,d,e,i,j,ii,jj,zx,xc,tes,t,C,D,dp[200009],dp2[200009],ans[200009],JM,DP2[200009],rt,dp3[200009],dp4[200009],p[200009],dep[200009],MSH[200009];
pair <long long, long long> DP[200009];
vla v[200009];
void dfs(int q, int w){
for(vla::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dfs((*it).first,q);
dp[q]+=dp[(*it).first]+(*it).second.second;
}
}
void dfs2(int q, int w){
if(w!=0) dp2[q]=dp2[w]+dp[w]-dp[q]-C+D;
for(vla::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
C=(*it).second.second;D=(*it).second.first;
dfs2((*it).first,q);
}
}
void upd(long long q, long long w){
if(DP[q].first<=w){
DP[q].second=DP[q].first;DP[q].first=w;
}else{
if(DP[q].second<w) DP[q].second=w;
}
}
void dfs3(int q, int w){
for(vla::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dfs3((*it).first,q);
upd(q,DP[(*it).first].first+(*it).second.first);
}
}
void dfs4(int q, int w){
if(w!=0){
DP2[q]=DP2[w]+C;
zx=0;
if(DP[w].first!=DP[q].first+D){
zx=DP[w].first+C;
}else{
zx=DP[w].second+C;
}
DP2[q]=max(DP2[q],zx);
}
for(vla::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
C=(*it).second.second;D=(*it).second.first;
dfs4((*it).first,q);
}
}
void dfs5(int q, int w){
dp4[q]=q;
for(vla::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it).first==w) continue;
dep[(*it).first]=dep[q]+(*it).second.first;MSH[(*it).first]=(*it).second.first;
dfs5((*it).first,q);
if(dep[dp4[q]]<dep[dp4[(*it).first]]){
dp4[q]=dp4[(*it).first];
}
}
p[dp4[q]]+=MSH[q];
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a;
for(i=1; i<a; i++){
cin>>c>>d>>C>>D;JM+=C+D;
v[c].push_back({d,{C,D}});
v[d].push_back({c,{D,C}});
}
dfs(1,0);
C=0;D=0;
dfs2(1,0);
for(i=1; i<=a; i++){
ans[1]=max(ans[1],dp[i]+dp2[i]);
}
dfs3(1,0);
C=0;D=0;
dfs4(1,0);
for(i=1; i<=a; i++){
zx=dp[i]+dp2[i];zx+=max(DP[i].first,DP2[i]);
if(ans[2]<zx){
ans[2]=zx;rt=i;
}
}
//
/*cout<<JM-ans[1]<<" "<<JM-ans[2]<<"\n";
cout<<rt<<"\n";*/
//
dfs5(rt,0);
sort(p+1,p+a+1);
for(i=1; i<=a/2; i++) swap(p[i],p[a-i+1]);
zx=dp[rt]+dp2[rt];
for(i=2; i<=a; i++){
zx+=p[i-1];
ans[i]=zx;
}
cin>>tes;
for(t=1; t<=tes; t++){
cin>>c;
cout<<JM-ans[c]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
315 ms |
33860 KB |
Output is correct |
3 |
Correct |
408 ms |
49452 KB |
Output is correct |
4 |
Correct |
304 ms |
33688 KB |
Output is correct |
5 |
Correct |
320 ms |
33508 KB |
Output is correct |
6 |
Correct |
314 ms |
35780 KB |
Output is correct |
7 |
Correct |
241 ms |
32472 KB |
Output is correct |
8 |
Correct |
404 ms |
48716 KB |
Output is correct |
9 |
Correct |
176 ms |
26996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
348 ms |
33808 KB |
Output is correct |
3 |
Correct |
433 ms |
49408 KB |
Output is correct |
4 |
Correct |
294 ms |
33700 KB |
Output is correct |
5 |
Correct |
305 ms |
33500 KB |
Output is correct |
6 |
Correct |
357 ms |
36456 KB |
Output is correct |
7 |
Correct |
184 ms |
31696 KB |
Output is correct |
8 |
Correct |
387 ms |
43868 KB |
Output is correct |
9 |
Correct |
169 ms |
26848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5012 KB |
Output is correct |
13 |
Correct |
5 ms |
5416 KB |
Output is correct |
14 |
Correct |
5 ms |
5604 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
5 ms |
5412 KB |
Output is correct |
17 |
Correct |
5 ms |
5420 KB |
Output is correct |
18 |
Correct |
5 ms |
5452 KB |
Output is correct |
19 |
Correct |
5 ms |
5324 KB |
Output is correct |
20 |
Correct |
4 ms |
5416 KB |
Output is correct |
21 |
Correct |
5 ms |
5420 KB |
Output is correct |
22 |
Correct |
5 ms |
5412 KB |
Output is correct |
23 |
Correct |
4 ms |
5420 KB |
Output is correct |
24 |
Correct |
5 ms |
5364 KB |
Output is correct |
25 |
Correct |
7 ms |
5572 KB |
Output is correct |
26 |
Correct |
4 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
315 ms |
33860 KB |
Output is correct |
3 |
Correct |
408 ms |
49452 KB |
Output is correct |
4 |
Correct |
304 ms |
33688 KB |
Output is correct |
5 |
Correct |
320 ms |
33508 KB |
Output is correct |
6 |
Correct |
314 ms |
35780 KB |
Output is correct |
7 |
Correct |
241 ms |
32472 KB |
Output is correct |
8 |
Correct |
404 ms |
48716 KB |
Output is correct |
9 |
Correct |
176 ms |
26996 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
348 ms |
33808 KB |
Output is correct |
12 |
Correct |
433 ms |
49408 KB |
Output is correct |
13 |
Correct |
294 ms |
33700 KB |
Output is correct |
14 |
Correct |
305 ms |
33500 KB |
Output is correct |
15 |
Correct |
357 ms |
36456 KB |
Output is correct |
16 |
Correct |
184 ms |
31696 KB |
Output is correct |
17 |
Correct |
387 ms |
43868 KB |
Output is correct |
18 |
Correct |
169 ms |
26848 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
347 ms |
33632 KB |
Output is correct |
21 |
Correct |
436 ms |
49468 KB |
Output is correct |
22 |
Correct |
322 ms |
33700 KB |
Output is correct |
23 |
Correct |
345 ms |
34060 KB |
Output is correct |
24 |
Correct |
342 ms |
33984 KB |
Output is correct |
25 |
Correct |
316 ms |
33996 KB |
Output is correct |
26 |
Correct |
335 ms |
33896 KB |
Output is correct |
27 |
Correct |
335 ms |
33548 KB |
Output is correct |
28 |
Correct |
349 ms |
36168 KB |
Output is correct |
29 |
Correct |
352 ms |
34284 KB |
Output is correct |
30 |
Correct |
285 ms |
33364 KB |
Output is correct |
31 |
Correct |
242 ms |
32316 KB |
Output is correct |
32 |
Correct |
388 ms |
44508 KB |
Output is correct |
33 |
Correct |
165 ms |
26936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
315 ms |
33860 KB |
Output is correct |
14 |
Correct |
408 ms |
49452 KB |
Output is correct |
15 |
Correct |
304 ms |
33688 KB |
Output is correct |
16 |
Correct |
320 ms |
33508 KB |
Output is correct |
17 |
Correct |
314 ms |
35780 KB |
Output is correct |
18 |
Correct |
241 ms |
32472 KB |
Output is correct |
19 |
Correct |
404 ms |
48716 KB |
Output is correct |
20 |
Correct |
176 ms |
26996 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
348 ms |
33808 KB |
Output is correct |
23 |
Correct |
433 ms |
49408 KB |
Output is correct |
24 |
Correct |
294 ms |
33700 KB |
Output is correct |
25 |
Correct |
305 ms |
33500 KB |
Output is correct |
26 |
Correct |
357 ms |
36456 KB |
Output is correct |
27 |
Correct |
184 ms |
31696 KB |
Output is correct |
28 |
Correct |
387 ms |
43868 KB |
Output is correct |
29 |
Correct |
169 ms |
26848 KB |
Output is correct |
30 |
Correct |
3 ms |
5012 KB |
Output is correct |
31 |
Correct |
5 ms |
5416 KB |
Output is correct |
32 |
Correct |
5 ms |
5604 KB |
Output is correct |
33 |
Correct |
6 ms |
5324 KB |
Output is correct |
34 |
Correct |
5 ms |
5412 KB |
Output is correct |
35 |
Correct |
5 ms |
5420 KB |
Output is correct |
36 |
Correct |
5 ms |
5452 KB |
Output is correct |
37 |
Correct |
5 ms |
5324 KB |
Output is correct |
38 |
Correct |
4 ms |
5416 KB |
Output is correct |
39 |
Correct |
5 ms |
5420 KB |
Output is correct |
40 |
Correct |
5 ms |
5412 KB |
Output is correct |
41 |
Correct |
4 ms |
5420 KB |
Output is correct |
42 |
Correct |
5 ms |
5364 KB |
Output is correct |
43 |
Correct |
7 ms |
5572 KB |
Output is correct |
44 |
Correct |
4 ms |
5324 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
347 ms |
33632 KB |
Output is correct |
47 |
Correct |
436 ms |
49468 KB |
Output is correct |
48 |
Correct |
322 ms |
33700 KB |
Output is correct |
49 |
Correct |
345 ms |
34060 KB |
Output is correct |
50 |
Correct |
342 ms |
33984 KB |
Output is correct |
51 |
Correct |
316 ms |
33996 KB |
Output is correct |
52 |
Correct |
335 ms |
33896 KB |
Output is correct |
53 |
Correct |
335 ms |
33548 KB |
Output is correct |
54 |
Correct |
349 ms |
36168 KB |
Output is correct |
55 |
Correct |
352 ms |
34284 KB |
Output is correct |
56 |
Correct |
285 ms |
33364 KB |
Output is correct |
57 |
Correct |
242 ms |
32316 KB |
Output is correct |
58 |
Correct |
388 ms |
44508 KB |
Output is correct |
59 |
Correct |
165 ms |
26936 KB |
Output is correct |
60 |
Correct |
3 ms |
5068 KB |
Output is correct |
61 |
Correct |
373 ms |
35092 KB |
Output is correct |
62 |
Correct |
466 ms |
49852 KB |
Output is correct |
63 |
Correct |
358 ms |
35116 KB |
Output is correct |
64 |
Correct |
385 ms |
35388 KB |
Output is correct |
65 |
Correct |
360 ms |
35108 KB |
Output is correct |
66 |
Correct |
364 ms |
35444 KB |
Output is correct |
67 |
Correct |
334 ms |
35076 KB |
Output is correct |
68 |
Correct |
335 ms |
35284 KB |
Output is correct |
69 |
Correct |
334 ms |
37456 KB |
Output is correct |
70 |
Correct |
347 ms |
35676 KB |
Output is correct |
71 |
Correct |
296 ms |
34760 KB |
Output is correct |
72 |
Correct |
277 ms |
34532 KB |
Output is correct |
73 |
Correct |
408 ms |
45380 KB |
Output is correct |
74 |
Correct |
219 ms |
29796 KB |
Output is correct |