# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
503246 | Hi_Im_Not_Meo_Meo | Traffic (IOI10_traffic) | C++14 | 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 f(i,j,k) for(int i=j;i<=k;i++)
#define pb push_back
const int N = 1e6+6;
using namespace std;
int n, vt, ans = 2e9;
int a[N], b[N];
vector <int> adj[N];
void dfs(int u,int pre){
b[u] = a[u];
for(auto v: adj[u])
if(v != pre){
dfs(v,u);
b[u] += b[v];
}
}
void dff(int u,int pre,int val){
int mx = val;
for(auto v: adj[u])
if(v != pre){
dff(v,u,val+b[u]-b[v]);
mx = max(mx,b[v]);
}
if(mx < ans){
vt = u;
ans = mx;
}
}
main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
f(i,1,n) cin>>a[i];
f(i,2,n){
int u, v;
cin>>u>>v;
u++; v++;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,0);
dff(1,0,0);
cout<<vt-1;
}