# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
502537 | Fake4Fun | 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.
// source: https://oj.uz/problem/view/IOI10_traffic
#include<iostream>
#include<vector>
using namespace std;
const int N=1e6+5;
int n,p[N];
int t1[N],t2[N],dad[N];
vector<int> adj[N];
void DFS(int pre,int u){
t1[u]=p[u];
dad[u]=pre;
for(auto i:adj[u]){
if(i==pre) continue;
DFS(u,i);
t1[u]+=t1[i];
}
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>p[i];
for(int i=1;i<n;i++){
int u,v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
DFS(0,1);
int all=t1[1];
int mi, pos=-1;
for(int u=1;u<=n;u++){
int val=all-t1[u];
for(auto i:adj[u]){
if(i==dad[u]) continue;
val=max(val,t1[i]);
}
if(pos==-1||mi>val) pos=u, mi=val;
}
cout<<pos;
return 0;
}