# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
365441 | alexyd88 | 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>
using namespace std;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define vvb vector<vector<bool>>
#define piii pair<int, pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define inf INT_MAX
#define pb push_back
#define p push
#define ll long long
#define ull unsigned long long
#define g get
#define dbl double
#define mn 1000005
int N, P[mn];
vi adj[mn];
int fans;
int ans[mn];
int dfs (int loc, int par)
{
int sb = 0, mb = 0;
for (auto i : adj[loc])
if (i != par)
sb += dfs(i, loc), mb = max(mb, dfs(i, loc));
ans[loc] = max(mb, fans - sb - P[loc]);
return sb + P[loc];
}
int LocateCentre(int fN, int fP[mn], int S[mn], int D[mn])
{
N = fN;
for (int i = 0; i < N; i++)
P[i] = fP[i], fans += P[i];
for (int i = 0; i < N - 1; i++)
adj[S[i]].pb({D[i]}), adj[D[i]].pb({S[i]});
dfs(0, -1);
int bc = inf, bl;
for (int i = 0; i < N; i++)
if (bc > ans[i])
bc = ans[i], bl = i;
return bl;
}
int main(void)
{
cout << "TR" << endl;
int N, P[mn], S[mn], D[mn];
cin >> N;
for (int i = 0; i < N; i++)
cin >> P[i];
for (int i = 0; i < N - 1; i++)
cin >> S[i];
for (int i = 0; i < N - 1; i++)
cin >> D[i];
cout << LocateCentre(N, P, S, D);
}