# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
495539 | PiejanVDC | Traffic (IOI10_traffic) | C++17 | 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;
#include "traffic.h"
vector<vector<int>>adj;
int n;
vector<int>v;
vector<int>mx,total;
int root_dfs(int u, int e = -1) {
int w = v[u];
int m = v[u];
for(auto z : adj[u]) {
if(z == e) continue;
int curr = root_dfs(z,u);
w+=curr;
mx[u] = max(mx[u],curr);
}
return total[u] = w;
}
int best, mn = INT_MAX;
void dfs(int u, int sum, int e = -1) {
if(max(mx[u],sum) < mn) {
mn = max(mx[u],sum);
best = u+1;
}
for(auto z : adj[u]) {
if(z == e) continue;
int nw = total[u] - total[z];
dfs(z,nw,u);
}
}
int LocateCentre(int nn, vector<int>p, vector<int>s, vector<int>d) {
n=nn;
adj.resize(n);
v = p;
mx.resize(n,0);
total.resize(n,0);
for(int i = 0 ; i < n-1 ; i++) {
adj[s[i]-1].push_back(d[i]-1);
adj[d[i]-1].push_back(s[i]-1);
}
root_dfs(0);
dfs(0,0);
return best;
}