# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
312722 | ronnith | 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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define boost_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define int long long
#define num(a) a-'0'
#define charr(a) (a - 'A' + 1)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define maxs(a,b) if(b>a)a=b
#define mins(a,b) if(b<a)a=b
#define bin1(a) __builtin_popcount(a)
#define debug(x) cerr<<"["<<#x<<":"<<x<<"] "
#define debug2(a,b) debug(a);debug(b)
#define debug3(a,b,c) debug2(a,b);debug(c)
#define de cerr
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/(__gcd(a,b))
#define print(arr) for(auto it = arr.begin();it < arr.end();it ++){cout << *it << " ";}cout << ln;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(a) (a).begin(), (a).end()
#define vi vector<long long>
#define v vector
#define p pair
#define pii p<int,int>
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define ln "\n"
typedef long double ld;
typedef double d;
using namespace std;
using namespace __gnu_pbds;
int modF=1e9+7;
int inf=1e18+5;
int nax = 1e6 + 5;
vector<int> a(nax);
vector<int> size(nax);
vector<vector<int>> adj(nax);
int ans = inf;
int node = -1;
void dfs1(int x,int pr){
// debug(x);
size[x] = a[x];
for(auto e:adj[x]){
if(e != pr){
dfs1(e,x);
size[x] += size[e];
}
}
}
void dfs2(int x,int pr){
int mx = 0;
for(auto e:adj[x]){
if(e != pr){
maxs(mx,size[e]);
dfs2(e,x);
}
else{
maxs(mx,size[0] - size[x]);
}
}
if(mx < ans){
ans = mx;
node = x;
}
}
int LocateCentre(int n,int p[],int s[],int d[]){
int res;
rep(i,0,n){
a[i] = p[i];
}
rep(i,0,n - 1){
adj[s[i]].pb(d[i]);
adj[d[i]].pb(s[i]);
}
// debug("yes");
dfs1(0,-1);
dfs2(0,-1);
// debug("yes");
res = node;
return res;
}
signed main(){
int n;
cin >> n;
int h[n];
rep(i,0,n){
cin >> h[i];
}
int s[n - 1];
int d[n - 1];
rep(i,0,n - 1){
cin >> s[i];
}
rep(i,0,n - 1){
cin >> d[i];
}
cout << LocateCentre(5,h,s,d) << ln;
// int n;
// cin >> n;
// cout << n << ln;
return 0;
}