Submission #376770

#TimeUsernameProblemLanguageResultExecution timeMemory
376770jass921026Designated Cities (JOI19_designated_cities)C++14
16 / 100
327 ms40172 KiB
#include<bits/stdc++.h> using namespace std; #define jizz ios_base::sync_with_stdio(false);cin.tie(NULL); typedef long long ll; typedef pair<int,int> pii; #define F first #define S second const int MAXN=2E5+10; vector<pii> G[MAXN]; ll sum[MAXN], sum2[MAXN], path[MAXN], ans[MAXN]; void init(int x, int f){ for(auto i:G[x]){ if(i.F!=f){ init(i.F,x); } else{ sum[1]+=i.S; } } } void getSum(int x, int f, ll s){ ll maxpath=0, secpath=0; for(auto i:G[x]){ if(i.F==f){ sum[x]=s-i.S; break; } } for(auto i:G[x]){ if(i.F!=f){ getSum(i.F,x,sum[x]+i.S); ll cp=path[i.F]+i.S; if(cp>maxpath){ secpath=maxpath; maxpath=cp; } else{ secpath=max(secpath,cp); } } } path[x]=maxpath; sum2[x]=sum[x]+maxpath+secpath; } int main(){ jizz int n; cin>>n; ll all=0; for(int i=0;i<n-1;i++){ int a, b, c, d; cin>>a>>b>>c>>d; G[a].push_back({b,c}); G[b].push_back({a,d}); all=all+c+d; } init(1,0); getSum(1,0,sum[1]); for(int i=1;i<=n;i++){ ans[1]=max(ans[1],sum[i]); ans[2]=max(ans[2],sum2[i]); } int q; cin>>q; for(int i=0;i<q;i++){ int e; cin>>e; cout<<all-ans[e]<<"\n"; } return 0; } /* 4 1 2 1 2 1 3 3 4 1 4 5 6 2 1 2 */ /* 5 1 3 13 6 5 1 17 8 5 2 6 10 1 4 16 11 1 1 */ /* 6 1 6 6 12 6 2 5 16 1 4 13 4 5 1 19 3 3 1 9 13 1 2 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...