# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
377140 | kevinxiehk | Traffic (IOI10_traffic) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ick cout<<"ickbmi32.9\n"
using namespace std;
int ans=0,arr[1000005],wt[1000005],n,t,tt;
vector<int>conn[1000005];
void dfs1(int now){
wt[now]=arr[now];
for(auto x:conn[now]){
if(wt[x]!=-1)continue;
dfs1(x);
wt[now]+=wt[x];
}
return;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=0;i<n;i++)cin>>arr[i];
for(int i=1;i<n;i++){
cin>>t>>tt;
conn[t].pb(tt);conn[tt].pb(t);
}
memset(wt,-1,sizeof wt);
dfs1(0);
while(1){
//for(int i=0;i<n;i++)cout<<wt[i]<<' ';
//cout<<ans<<'\n';
int tm=-1,tmid=-1;
for(auto x:conn[ans]){
if(wt[x]>tm){
tm=wt[x];
tmid=x;
}
}
if(wt[ans]-wt[tmid]>=tm){
cout<<ans<<'\n';
return 0;
}
wt[tmid]=wt[ans];
wt[ans]=-1;
ans=tmid;
}
return 0;
}