#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define int long long
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
const int maxn=2e5+5;
vector<iii> AdjList[maxn];
int n;
int cost[maxn];
int res[maxn];
int sum;
int ans[maxn];
int ans1[maxn];
int path[maxn];
bool used[maxn];
pair<int,int> par[maxn];
void prep(int s=1, int p=-1) {
////cout<<"prep "<<s<<endl;
for(iii tmp: AdjList[s]) {
int u=tmp.fi;
if(u==p) continue;
int w=tmp.se.se;
cost[1]+=w;
prep(u,s);
}
}
void dfs(int s=1, int p=-1) {
////cout<<"dfs "<<s<<endl;
for(iii tmp: AdjList[s]) {
int u=tmp.fi;
if(u==p) continue;
int fw=tmp.se.fi;
int bw=tmp.se.se; // currently bw
cost[u]=cost[s]+fw-bw;
dfs(u,s);
}
}
int in[maxn];
int out[maxn];
int rev[maxn];
int up[maxn][20];
int curt=0;
bool ancestor(int a, int b) {
if(!a) return true;
if(!b) return false;
return in[a]<=in[b]&&out[a]>=out[b];
}
void dfs2(int s, int p=0) {
////cout<<"dfs2 "<<s<<endl;
in[s]=++curt;
//cout<<"in "<<s<<" = "<<in[s]<<endl;
rev[in[s]]=s;
up[s][0]=p;
for(int i=1 ; i<20 ; i++) {
up[s][i]=up[up[s][i-1]][i-1];
////cout<<"up "<<s<<" "<<i<<" = "<<up[s][i]<<endl;
}
for(iii tmp: AdjList[s]) {
int u=tmp.fi;
if(u==p) continue;
int fw=tmp.se.fi;
path[u]=path[s]+fw;
par[u]=ii(s,fw);
////cout<<"par "<<u<<" = "<<s<<" "<<fw<<endl;
////cout<<"u = "<<u<<endl;
dfs2(u,s);
}
out[s]=curt;
//cout<<"out "<<s<<" = "<<out[s]<<endl;
}
int lca(int u, int v) {
////cout<<"lca "<<u<<" "<<v<<" = "<<endl;
if(ancestor(u,v)) return u;
if(ancestor(v,u)) return v;
for(int i=20 ; i>0 ; i--) {
////cout<<"up "<<u<<" "<<i<<" = "<<up[u][i]<<endl;
if(!ancestor(up[u][i],v)) u=up[u][i];
}
////cout<<up[u][0]<<endl;
return up[u][0];
}
// segment tree
pair<int,int> st[maxn*4]; // max
int lz[maxn*4];
void build(int id=1, int tl=1, int tr=n) {
////cout<<"build "<<tl<<" "<<tr<<endl;
if(tl==tr) {
st[id]=make_pair(path[rev[tl]],tl);
lz[id]=0;
////cout<<"st "<<id<<" = "<<st[id].first<<" "<<st[id].second<<'\n';
return;
}
int mid=(tl+tr)/2;
build(id*2,tl,mid);
build(id*2+1,mid+1,tr);
lz[id]=0;
st[id]=max(st[id*2],st[id*2+1]);
////cout<<"st "<<tl<<" "<<tr<<" = "<<st[id].first<<" "<<st[id].second<<'\n';
}
void do_lazy(int id, int tl, int tr) {
if(!lz[id]) return;
if(tl!=tr) {
lz[id*2]+=lz[id];
lz[id*2+1]+=lz[id];
}
st[id].first-=lz[id];
lz[id]=0;
return;
}
void upd(int id, int tl, int tr, int l, int r, int sub) {
////cout<<"update "<<id<<" "<<tl<<" "<<tr<<" "<<l<<" "<<r<<endl;
do_lazy(id,tl,tr);
if(tl>r||tr<l) return;
if(l<=tl&&tr<=r) {
lz[id]+=sub;
do_lazy(id,tl,tr);
return;
}
int mid=(tl+tr)/2;
upd(id*2,tl,mid,l,r,sub);
upd(id*2+1,mid+1,tr,l,r,sub);
st[id]=max(st[id*2],st[id*2+1]);
////cout<<"st "<<tl<<" "<<tr<<" = "<<st[id].first<<" "<<st[id].second<<endl;
}
void sub(int v) {
while(v) {
ii tmp=par[v];
int w=tmp.second;
int p=tmp.first;
if(used[v]) break;
used[v]=1;
upd(1,1,n,in[v],out[v],w);
//cout<<"update "<<v<<" "<<w<<endl;
v=p;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i=1 ; i<n ; i++) {
int a,b,c,d;
cin>>a>>b>>c>>d;
AdjList[a].push_back(iii(b,ii(c,d))); // forward then backward
AdjList[b].push_back(iii(a,ii(d,c)));
sum+=c; sum+=d;
}
prep(); dfs();
int root=1;
int mx_cost=cost[1];
for(int i=2 ; i<=n ; i++) {
if(cost[i]>mx_cost) {
root=i;
mx_cost=cost[i];
}
}
dfs2(root);
////cout<<"root = "<<root<<endl;
build();
////cout<<"cost "<<root<<" = "<<cost[root]<<endl;
pair<int,int> mn=st[1];
int id1=mn.second;
int val1=mn.first;
int root1=rev[id1];
curt=0;
par[root]=ii(0,0);
path[root]=0;
dfs2(root);
build();
ans[1]=cost[root];
//cout<<"root = "<<root<<endl;
//cout<<"sum = "<<sum<<endl;
//cout<<"ans "<<1<<" = "<<ans[1]<<endl;
for(int i=1 ; i<=n ; i++) used[i]=0;
for(int i=2 ; i<=n ; i++) {
pair<int,int> mn=st[1];
int id=mn.second;
int val=mn.first;
int v=rev[id];
//cout<<"val "<<val<<endl;
//cout<<"v = "<<v<<endl;
////cout<<endl<<"v = "<<v<<endl;
////cout<<"mn = "<<val<<" "<<id<<endl;
ans[i]=ans[i-1]+val;
//cout<<"ans "<<i<<" = "<<ans[i]<<endl<<endl;
sub(v);
}
curt=0;
par[root1]=ii(0,0);
path[root1]=0;
dfs2(root1);
build();
ans1[1]=cost[root1];
//cout<<"root = "<<root<<endl;
//cout<<"sum = "<<sum<<endl;
//cout<<"ans "<<1<<" = "<<ans[1]<<endl;
for(int i=1 ; i<=n ; i++) used[i]=0;
for(int i=2 ; i<=n ; i++) {
pair<int,int> mn=st[1];
int id=mn.second;
int val=mn.first;
int v=rev[id];
//cout<<"val "<<val<<endl;
//cout<<"v = "<<v<<endl;
////cout<<endl<<"v = "<<v<<endl;
////cout<<"mn = "<<val<<" "<<id<<endl;
ans1[i]=ans1[i-1]+val;
//cout<<"ans "<<i<<" = "<<ans[i]<<endl<<endl;
sub(v);
}
int q;
cin>>q;
while(q--) {
int x;
cin>>x;
ans[x]=max(ans[x],ans1[x]);
cout<<sum-ans[x]<<'\n';
}
}
Compilation message
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:165:9: warning: unused variable 'val1' [-Wunused-variable]
165 | int val1=mn.first;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5072 KB |
Output is correct |
2 |
Correct |
2 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5072 KB |
Output is correct |
2 |
Correct |
707 ms |
82400 KB |
Output is correct |
3 |
Correct |
731 ms |
95068 KB |
Output is correct |
4 |
Correct |
655 ms |
80916 KB |
Output is correct |
5 |
Correct |
680 ms |
82192 KB |
Output is correct |
6 |
Correct |
754 ms |
84092 KB |
Output is correct |
7 |
Correct |
664 ms |
81204 KB |
Output is correct |
8 |
Correct |
803 ms |
94496 KB |
Output is correct |
9 |
Correct |
587 ms |
80340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5072 KB |
Output is correct |
2 |
Correct |
704 ms |
82456 KB |
Output is correct |
3 |
Correct |
718 ms |
94992 KB |
Output is correct |
4 |
Correct |
644 ms |
81012 KB |
Output is correct |
5 |
Correct |
684 ms |
82156 KB |
Output is correct |
6 |
Correct |
772 ms |
84748 KB |
Output is correct |
7 |
Correct |
573 ms |
80324 KB |
Output is correct |
8 |
Correct |
826 ms |
90644 KB |
Output is correct |
9 |
Correct |
547 ms |
80088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5072 KB |
Output is correct |
2 |
Correct |
2 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
2 ms |
5072 KB |
Output is correct |
13 |
Correct |
8 ms |
5840 KB |
Output is correct |
14 |
Correct |
5 ms |
5976 KB |
Output is correct |
15 |
Correct |
6 ms |
5840 KB |
Output is correct |
16 |
Correct |
6 ms |
5840 KB |
Output is correct |
17 |
Correct |
7 ms |
5840 KB |
Output is correct |
18 |
Correct |
6 ms |
5884 KB |
Output is correct |
19 |
Correct |
8 ms |
5872 KB |
Output is correct |
20 |
Correct |
7 ms |
5840 KB |
Output is correct |
21 |
Correct |
6 ms |
5840 KB |
Output is correct |
22 |
Correct |
6 ms |
5840 KB |
Output is correct |
23 |
Correct |
6 ms |
5840 KB |
Output is correct |
24 |
Correct |
5 ms |
5756 KB |
Output is correct |
25 |
Correct |
6 ms |
5968 KB |
Output is correct |
26 |
Correct |
5 ms |
5840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5072 KB |
Output is correct |
2 |
Correct |
707 ms |
82400 KB |
Output is correct |
3 |
Correct |
731 ms |
95068 KB |
Output is correct |
4 |
Correct |
655 ms |
80916 KB |
Output is correct |
5 |
Correct |
680 ms |
82192 KB |
Output is correct |
6 |
Correct |
754 ms |
84092 KB |
Output is correct |
7 |
Correct |
664 ms |
81204 KB |
Output is correct |
8 |
Correct |
803 ms |
94496 KB |
Output is correct |
9 |
Correct |
587 ms |
80340 KB |
Output is correct |
10 |
Correct |
2 ms |
5072 KB |
Output is correct |
11 |
Correct |
704 ms |
82456 KB |
Output is correct |
12 |
Correct |
718 ms |
94992 KB |
Output is correct |
13 |
Correct |
644 ms |
81012 KB |
Output is correct |
14 |
Correct |
684 ms |
82156 KB |
Output is correct |
15 |
Correct |
772 ms |
84748 KB |
Output is correct |
16 |
Correct |
573 ms |
80324 KB |
Output is correct |
17 |
Correct |
826 ms |
90644 KB |
Output is correct |
18 |
Correct |
547 ms |
80088 KB |
Output is correct |
19 |
Correct |
3 ms |
5072 KB |
Output is correct |
20 |
Correct |
707 ms |
82340 KB |
Output is correct |
21 |
Correct |
734 ms |
94924 KB |
Output is correct |
22 |
Correct |
633 ms |
81024 KB |
Output is correct |
23 |
Correct |
727 ms |
82876 KB |
Output is correct |
24 |
Correct |
689 ms |
81412 KB |
Output is correct |
25 |
Correct |
709 ms |
82628 KB |
Output is correct |
26 |
Correct |
716 ms |
81496 KB |
Output is correct |
27 |
Correct |
703 ms |
82216 KB |
Output is correct |
28 |
Correct |
780 ms |
84484 KB |
Output is correct |
29 |
Correct |
704 ms |
83064 KB |
Output is correct |
30 |
Correct |
668 ms |
80992 KB |
Output is correct |
31 |
Correct |
647 ms |
80928 KB |
Output is correct |
32 |
Correct |
898 ms |
91120 KB |
Output is correct |
33 |
Correct |
630 ms |
80360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5072 KB |
Output is correct |
2 |
Correct |
2 ms |
5008 KB |
Output is correct |
3 |
Correct |
3 ms |
5024 KB |
Output is correct |
4 |
Correct |
2 ms |
5072 KB |
Output is correct |
5 |
Correct |
3 ms |
5072 KB |
Output is correct |
6 |
Correct |
2 ms |
5072 KB |
Output is correct |
7 |
Correct |
3 ms |
5072 KB |
Output is correct |
8 |
Correct |
2 ms |
5072 KB |
Output is correct |
9 |
Correct |
2 ms |
5024 KB |
Output is correct |
10 |
Correct |
3 ms |
5072 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
5072 KB |
Output is correct |
13 |
Correct |
707 ms |
82400 KB |
Output is correct |
14 |
Correct |
731 ms |
95068 KB |
Output is correct |
15 |
Correct |
655 ms |
80916 KB |
Output is correct |
16 |
Correct |
680 ms |
82192 KB |
Output is correct |
17 |
Correct |
754 ms |
84092 KB |
Output is correct |
18 |
Correct |
664 ms |
81204 KB |
Output is correct |
19 |
Correct |
803 ms |
94496 KB |
Output is correct |
20 |
Correct |
587 ms |
80340 KB |
Output is correct |
21 |
Correct |
2 ms |
5072 KB |
Output is correct |
22 |
Correct |
704 ms |
82456 KB |
Output is correct |
23 |
Correct |
718 ms |
94992 KB |
Output is correct |
24 |
Correct |
644 ms |
81012 KB |
Output is correct |
25 |
Correct |
684 ms |
82156 KB |
Output is correct |
26 |
Correct |
772 ms |
84748 KB |
Output is correct |
27 |
Correct |
573 ms |
80324 KB |
Output is correct |
28 |
Correct |
826 ms |
90644 KB |
Output is correct |
29 |
Correct |
547 ms |
80088 KB |
Output is correct |
30 |
Correct |
2 ms |
5072 KB |
Output is correct |
31 |
Correct |
8 ms |
5840 KB |
Output is correct |
32 |
Correct |
5 ms |
5976 KB |
Output is correct |
33 |
Correct |
6 ms |
5840 KB |
Output is correct |
34 |
Correct |
6 ms |
5840 KB |
Output is correct |
35 |
Correct |
7 ms |
5840 KB |
Output is correct |
36 |
Correct |
6 ms |
5884 KB |
Output is correct |
37 |
Correct |
8 ms |
5872 KB |
Output is correct |
38 |
Correct |
7 ms |
5840 KB |
Output is correct |
39 |
Correct |
6 ms |
5840 KB |
Output is correct |
40 |
Correct |
6 ms |
5840 KB |
Output is correct |
41 |
Correct |
6 ms |
5840 KB |
Output is correct |
42 |
Correct |
5 ms |
5756 KB |
Output is correct |
43 |
Correct |
6 ms |
5968 KB |
Output is correct |
44 |
Correct |
5 ms |
5840 KB |
Output is correct |
45 |
Correct |
3 ms |
5072 KB |
Output is correct |
46 |
Correct |
707 ms |
82340 KB |
Output is correct |
47 |
Correct |
734 ms |
94924 KB |
Output is correct |
48 |
Correct |
633 ms |
81024 KB |
Output is correct |
49 |
Correct |
727 ms |
82876 KB |
Output is correct |
50 |
Correct |
689 ms |
81412 KB |
Output is correct |
51 |
Correct |
709 ms |
82628 KB |
Output is correct |
52 |
Correct |
716 ms |
81496 KB |
Output is correct |
53 |
Correct |
703 ms |
82216 KB |
Output is correct |
54 |
Correct |
780 ms |
84484 KB |
Output is correct |
55 |
Correct |
704 ms |
83064 KB |
Output is correct |
56 |
Correct |
668 ms |
80992 KB |
Output is correct |
57 |
Correct |
647 ms |
80928 KB |
Output is correct |
58 |
Correct |
898 ms |
91120 KB |
Output is correct |
59 |
Correct |
630 ms |
80360 KB |
Output is correct |
60 |
Correct |
3 ms |
5072 KB |
Output is correct |
61 |
Correct |
783 ms |
85072 KB |
Output is correct |
62 |
Correct |
825 ms |
96600 KB |
Output is correct |
63 |
Correct |
713 ms |
83676 KB |
Output is correct |
64 |
Correct |
761 ms |
85312 KB |
Output is correct |
65 |
Correct |
814 ms |
83852 KB |
Output is correct |
66 |
Correct |
808 ms |
85280 KB |
Output is correct |
67 |
Correct |
776 ms |
83880 KB |
Output is correct |
68 |
Correct |
786 ms |
85060 KB |
Output is correct |
69 |
Correct |
836 ms |
86860 KB |
Output is correct |
70 |
Correct |
830 ms |
85500 KB |
Output is correct |
71 |
Correct |
767 ms |
83800 KB |
Output is correct |
72 |
Correct |
720 ms |
84572 KB |
Output is correct |
73 |
Correct |
919 ms |
93304 KB |
Output is correct |
74 |
Correct |
617 ms |
84508 KB |
Output is correct |