# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
373484 | eulerdesoja | 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>
#include<fstream>
using namespace std;
#define int long long
#define pb push_back
#define sz(x) int(x.size())
typedef pair<int,int>ii;
typedef vector<int> vi;
const int mxn=1e6+6;
vi g[mxn];
int n,a[mxn],ma[mxn],sum[mxn];//maximum of congestion in a road
void dfs(int i,int p){
for(int j:g[i])if(j!=p){
dfs(j,i);
sum[i]+=sum[j];
ma[i]=max(ma[i],max(ma[j],sum[j]));
}
sum[i]+=a[i];
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n-1;i++){
int x,y;cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
int ans=1e18,res;
for(int i=0;i<n;i++){
memset(sum,0,sizeof(sum));
memset(ma,0,sizeof(ma));
dfs(i,-1);
if(ma[i]<ans){
ans=ma[i];
res=i;
}
}
cout<<res<<"\n";
return 0;
}
/*
5
10 10 10 20 20
0 2
1 2
2 3
3 4
*/