# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
377760 | AlexRex0 | 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 <stdio.h>
#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000002];
long long int sub[1000002];
bool visitado[1000002];
bool visitado2[1000002];
int v[1000002];
long long int dfs(int nodo, int padre){
sub[nodo]+= v[nodo];
for(int i = 0 ; i < g[nodo].size(); ++i){
if(g[nodo][i] != padre){
sub[nodo]+= dfs(g[nodo][i], nodo);
}
}
return sub[nodo];
}
long long int indRes, indMax = -1, Max, aux, Res, ayuda;
static int N,P[1000000],S[1000000],D[1000000];
int LocateCentre(int N, int pp[], int S[], int D[]) {
for(int i = 0; i < N - 1; ++i){
g[S[i]].push_back(D[i]);
g[D[i]].push_back(S[i]);
v[i] = pp[i];
}
v[N - 1] = pp[N - 1];
dfs(0, -1);
Max = -1;
for(int i = 0; i < g[0].size(); ++i){
ayuda = sub[g[0][i]];
if(ayuda > Max){
Max = ayuda;
}
}
Res = Max;
visitado2[0] = true;
while(true){
Max = -1;
for(int i = 0; i < g[aux].size(); ++i){
ayuda = sub[g[aux][i]];
if(ayuda > Max){
Max = ayuda;
indMax = g[aux][i];
}
}
if(visitado2[indMax]){
if(Max < Res){
indRes = aux;
Res = Max;
}
return indRes;
}
visitado2[indMax] = true;
if(Max < Res){
indRes = aux;
Res = Max;
}
sub[aux]-= sub[indMax];
sub[indMax]+= sub[aux];
if(indRes == indMax){
break;
}
aux = indMax;
}
return indRes;
}
int main(){
int i;
scanf("%d",&N);
for (i=0;i<N;i++) scanf("%d",&P[i]);
for (i=0;i<N-1;i++) scanf("%d%d",&S[i],&D[i]);
int r = LocateCentre(N,P,S,D);
printf("%d\n",r);
return 0;
}