#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll
const int N=1e6+99,inf=1e15;
int n,t,rt,X,Y,q,sum,cnt,to[N],ts[N],w[N],res[N],ans[N],pes[N],h[N],mark[N],par[N],st[N],ft[N],wp[N],lazy[N];
pair<int,int> dp[N],seg[N];
vector<int> g[N];
void dfs1(int u,int p){
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
res[rt]+=w[id];
dfs1(v,u);
}
}
void dfs2(int u,int p){
vector<pair<int,int> > vec(3);
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
res[v]=res[u]-w[id]+w[id^1];
dfs2(v,u);
dp[v].F+=w[id];
vec[0]=dp[v];
sort(all(vec));
}
if(vec[2].S==0){
dp[u]={0,u};
}
else{
dp[u]=vec[2];
}
pes[u]=inf;
if(vec[1].S!=0 && res[u]-vec[1].F-vec[2].F<ans[2]){
ans[2]=res[u]-vec[1].F-vec[2].F;
X=vec[1].S;
Y=vec[2].S;
}
minm(ans[1],res[u]);
}
void dfs3(int u,int p){
ts[cnt]=u;
st[u]=cnt++;
par[u]=p;
for(auto id : g[u]){
int v=to[id];
if(v==p) continue ;
h[v]=h[u]+w[id];
wp[v]=w[id];
sum+=w[id];
dfs3(v,u);
}
ft[u]=cnt;
}
void shift(int id){
seg[id<<1].F+=lazy[id];
seg[id<<1|1].F+=lazy[id];
lazy[id<<1]+=lazy[id];
lazy[id<<1|1]+=lazy[id];
lazy[id]=0;
}
void build(int id=1,int l=0,int r=n){
if(l+1==r){
seg[id]={h[ts[l]],ts[l]};
return ;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid,r);
seg[id]=max(seg[id<<1],seg[id<<1|1]);
}
void add(int L,int R,int val,int id=1,int l=0,int r=n){
if(r<=L || R<=l) return ;
if(L<=l && r<=R){
seg[id].F+=val;
lazy[id]+=val;
return ;
}
int mid=(l+r)>>1;
shift(id);
add(L,R,val,id<<1,l,mid);
add(L,R,val,id<<1|1,mid,r);
seg[id]=max(seg[id<<1],seg[id<<1|1]);
}
void find(){
fill(ans,ans+N,inf);
rt=1;
while(g[rt].size()==1) rt++;
dfs1(rt,0);
dfs2(rt,0);
rt=X;
dfs3(rt,0);
mark[rt]=1;
}
void do_it(){
build();
f(i,2,n+1){
int u=seg[1].S;
//eror(u);
//sum-=seg[1].F;
while(!mark[u]){
mark[u]=1;
//eror(wp[u]);
sum-=wp[u];
add(st[u],ft[u],-wp[u]);
u=par[u];
}
//eror(sum);
ans[i]=sum;
}
}
main(){
ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
cin>>n;
f(i,1,n){
int u,v;
cin>>u>>v>>w[2*i]>>w[2*i+1];
to[2*i]=v;
to[2*i+1]=u;
g[u].pb(2*i);
g[v].pb(2*i+1);
}
if(n==2){
ans[1]=min(w[2],w[3]);
cin>>q;
while(q--){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
exit(0);
}
find();
do_it();
cin>>q;
while(q--){
int x;
cin>>x;
cout<<ans[x]<<'\n';
}
}
Compilation message
designated_cities.cpp:135:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
135 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
18 ms |
31680 KB |
Output is correct |
3 |
Correct |
17 ms |
31660 KB |
Output is correct |
4 |
Correct |
17 ms |
31692 KB |
Output is correct |
5 |
Correct |
17 ms |
31668 KB |
Output is correct |
6 |
Correct |
16 ms |
31692 KB |
Output is correct |
7 |
Correct |
16 ms |
31692 KB |
Output is correct |
8 |
Correct |
16 ms |
31736 KB |
Output is correct |
9 |
Correct |
15 ms |
31692 KB |
Output is correct |
10 |
Correct |
16 ms |
31656 KB |
Output is correct |
11 |
Correct |
17 ms |
31740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
451 ms |
74956 KB |
Output is correct |
3 |
Correct |
474 ms |
99216 KB |
Output is correct |
4 |
Correct |
372 ms |
74760 KB |
Output is correct |
5 |
Correct |
433 ms |
74812 KB |
Output is correct |
6 |
Correct |
418 ms |
78268 KB |
Output is correct |
7 |
Correct |
370 ms |
74868 KB |
Output is correct |
8 |
Correct |
516 ms |
99932 KB |
Output is correct |
9 |
Correct |
300 ms |
75256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
415 ms |
74860 KB |
Output is correct |
3 |
Correct |
524 ms |
103532 KB |
Output is correct |
4 |
Correct |
410 ms |
74916 KB |
Output is correct |
5 |
Correct |
418 ms |
74812 KB |
Output is correct |
6 |
Correct |
438 ms |
78788 KB |
Output is correct |
7 |
Correct |
424 ms |
75112 KB |
Output is correct |
8 |
Correct |
472 ms |
90512 KB |
Output is correct |
9 |
Correct |
295 ms |
75372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
18 ms |
31680 KB |
Output is correct |
3 |
Correct |
17 ms |
31660 KB |
Output is correct |
4 |
Correct |
17 ms |
31692 KB |
Output is correct |
5 |
Correct |
17 ms |
31668 KB |
Output is correct |
6 |
Correct |
16 ms |
31692 KB |
Output is correct |
7 |
Correct |
16 ms |
31692 KB |
Output is correct |
8 |
Correct |
16 ms |
31736 KB |
Output is correct |
9 |
Correct |
15 ms |
31692 KB |
Output is correct |
10 |
Correct |
16 ms |
31656 KB |
Output is correct |
11 |
Correct |
17 ms |
31740 KB |
Output is correct |
12 |
Correct |
13 ms |
23768 KB |
Output is correct |
13 |
Correct |
18 ms |
32076 KB |
Output is correct |
14 |
Correct |
18 ms |
32332 KB |
Output is correct |
15 |
Correct |
18 ms |
32164 KB |
Output is correct |
16 |
Correct |
18 ms |
32168 KB |
Output is correct |
17 |
Correct |
19 ms |
32076 KB |
Output is correct |
18 |
Correct |
21 ms |
32176 KB |
Output is correct |
19 |
Correct |
23 ms |
32112 KB |
Output is correct |
20 |
Correct |
19 ms |
32152 KB |
Output is correct |
21 |
Correct |
19 ms |
32076 KB |
Output is correct |
22 |
Correct |
18 ms |
32076 KB |
Output is correct |
23 |
Correct |
18 ms |
32112 KB |
Output is correct |
24 |
Correct |
20 ms |
32060 KB |
Output is correct |
25 |
Correct |
18 ms |
32404 KB |
Output is correct |
26 |
Correct |
18 ms |
32184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23756 KB |
Output is correct |
2 |
Correct |
451 ms |
74956 KB |
Output is correct |
3 |
Correct |
474 ms |
99216 KB |
Output is correct |
4 |
Correct |
372 ms |
74760 KB |
Output is correct |
5 |
Correct |
433 ms |
74812 KB |
Output is correct |
6 |
Correct |
418 ms |
78268 KB |
Output is correct |
7 |
Correct |
370 ms |
74868 KB |
Output is correct |
8 |
Correct |
516 ms |
99932 KB |
Output is correct |
9 |
Correct |
300 ms |
75256 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
415 ms |
74860 KB |
Output is correct |
12 |
Correct |
524 ms |
103532 KB |
Output is correct |
13 |
Correct |
410 ms |
74916 KB |
Output is correct |
14 |
Correct |
418 ms |
74812 KB |
Output is correct |
15 |
Correct |
438 ms |
78788 KB |
Output is correct |
16 |
Correct |
424 ms |
75112 KB |
Output is correct |
17 |
Correct |
472 ms |
90512 KB |
Output is correct |
18 |
Correct |
295 ms |
75372 KB |
Output is correct |
19 |
Correct |
13 ms |
23792 KB |
Output is correct |
20 |
Correct |
435 ms |
74892 KB |
Output is correct |
21 |
Correct |
449 ms |
103888 KB |
Output is correct |
22 |
Correct |
455 ms |
74980 KB |
Output is correct |
23 |
Correct |
476 ms |
75060 KB |
Output is correct |
24 |
Correct |
405 ms |
74948 KB |
Output is correct |
25 |
Correct |
403 ms |
75000 KB |
Output is correct |
26 |
Correct |
499 ms |
75028 KB |
Output is correct |
27 |
Correct |
403 ms |
74944 KB |
Output is correct |
28 |
Correct |
438 ms |
77772 KB |
Output is correct |
29 |
Correct |
510 ms |
75272 KB |
Output is correct |
30 |
Correct |
394 ms |
75036 KB |
Output is correct |
31 |
Correct |
346 ms |
75084 KB |
Output is correct |
32 |
Correct |
509 ms |
91768 KB |
Output is correct |
33 |
Correct |
291 ms |
75372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
18 ms |
31680 KB |
Output is correct |
3 |
Correct |
17 ms |
31660 KB |
Output is correct |
4 |
Correct |
17 ms |
31692 KB |
Output is correct |
5 |
Correct |
17 ms |
31668 KB |
Output is correct |
6 |
Correct |
16 ms |
31692 KB |
Output is correct |
7 |
Correct |
16 ms |
31692 KB |
Output is correct |
8 |
Correct |
16 ms |
31736 KB |
Output is correct |
9 |
Correct |
15 ms |
31692 KB |
Output is correct |
10 |
Correct |
16 ms |
31656 KB |
Output is correct |
11 |
Correct |
17 ms |
31740 KB |
Output is correct |
12 |
Correct |
12 ms |
23756 KB |
Output is correct |
13 |
Correct |
451 ms |
74956 KB |
Output is correct |
14 |
Correct |
474 ms |
99216 KB |
Output is correct |
15 |
Correct |
372 ms |
74760 KB |
Output is correct |
16 |
Correct |
433 ms |
74812 KB |
Output is correct |
17 |
Correct |
418 ms |
78268 KB |
Output is correct |
18 |
Correct |
370 ms |
74868 KB |
Output is correct |
19 |
Correct |
516 ms |
99932 KB |
Output is correct |
20 |
Correct |
300 ms |
75256 KB |
Output is correct |
21 |
Correct |
14 ms |
23756 KB |
Output is correct |
22 |
Correct |
415 ms |
74860 KB |
Output is correct |
23 |
Correct |
524 ms |
103532 KB |
Output is correct |
24 |
Correct |
410 ms |
74916 KB |
Output is correct |
25 |
Correct |
418 ms |
74812 KB |
Output is correct |
26 |
Correct |
438 ms |
78788 KB |
Output is correct |
27 |
Correct |
424 ms |
75112 KB |
Output is correct |
28 |
Correct |
472 ms |
90512 KB |
Output is correct |
29 |
Correct |
295 ms |
75372 KB |
Output is correct |
30 |
Correct |
13 ms |
23768 KB |
Output is correct |
31 |
Correct |
18 ms |
32076 KB |
Output is correct |
32 |
Correct |
18 ms |
32332 KB |
Output is correct |
33 |
Correct |
18 ms |
32164 KB |
Output is correct |
34 |
Correct |
18 ms |
32168 KB |
Output is correct |
35 |
Correct |
19 ms |
32076 KB |
Output is correct |
36 |
Correct |
21 ms |
32176 KB |
Output is correct |
37 |
Correct |
23 ms |
32112 KB |
Output is correct |
38 |
Correct |
19 ms |
32152 KB |
Output is correct |
39 |
Correct |
19 ms |
32076 KB |
Output is correct |
40 |
Correct |
18 ms |
32076 KB |
Output is correct |
41 |
Correct |
18 ms |
32112 KB |
Output is correct |
42 |
Correct |
20 ms |
32060 KB |
Output is correct |
43 |
Correct |
18 ms |
32404 KB |
Output is correct |
44 |
Correct |
18 ms |
32184 KB |
Output is correct |
45 |
Correct |
13 ms |
23792 KB |
Output is correct |
46 |
Correct |
435 ms |
74892 KB |
Output is correct |
47 |
Correct |
449 ms |
103888 KB |
Output is correct |
48 |
Correct |
455 ms |
74980 KB |
Output is correct |
49 |
Correct |
476 ms |
75060 KB |
Output is correct |
50 |
Correct |
405 ms |
74948 KB |
Output is correct |
51 |
Correct |
403 ms |
75000 KB |
Output is correct |
52 |
Correct |
499 ms |
75028 KB |
Output is correct |
53 |
Correct |
403 ms |
74944 KB |
Output is correct |
54 |
Correct |
438 ms |
77772 KB |
Output is correct |
55 |
Correct |
510 ms |
75272 KB |
Output is correct |
56 |
Correct |
394 ms |
75036 KB |
Output is correct |
57 |
Correct |
346 ms |
75084 KB |
Output is correct |
58 |
Correct |
509 ms |
91768 KB |
Output is correct |
59 |
Correct |
291 ms |
75372 KB |
Output is correct |
60 |
Correct |
14 ms |
23756 KB |
Output is correct |
61 |
Correct |
456 ms |
76312 KB |
Output is correct |
62 |
Correct |
498 ms |
99860 KB |
Output is correct |
63 |
Correct |
436 ms |
76356 KB |
Output is correct |
64 |
Correct |
455 ms |
76372 KB |
Output is correct |
65 |
Correct |
427 ms |
76096 KB |
Output is correct |
66 |
Correct |
445 ms |
76316 KB |
Output is correct |
67 |
Correct |
437 ms |
76164 KB |
Output is correct |
68 |
Correct |
418 ms |
76384 KB |
Output is correct |
69 |
Correct |
481 ms |
79396 KB |
Output is correct |
70 |
Correct |
441 ms |
76408 KB |
Output is correct |
71 |
Correct |
407 ms |
76600 KB |
Output is correct |
72 |
Correct |
424 ms |
77220 KB |
Output is correct |
73 |
Correct |
502 ms |
90436 KB |
Output is correct |
74 |
Correct |
314 ms |
78484 KB |
Output is correct |