# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
598327 | acatmeowmeow | Traffic (IOI10_traffic) | C++11 | 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 "grader.h"
#include "traffic.h"
#include <bits/stdc++.h>
using namespace std;
void dfs(int u, int e, vector<vector<int>>&adj, vector<int>&dp, int P[]) {
dp[u] += P[u];
for (auto&v : adj[u]) {
if (v == e) continue;
dfs(v, u, adj, dp, P);
dp[u] += dp[v];
}
}
void reroot(int u, int e, vector<vector<int>>&adj, vector<int>&dp, int P[], int&mn, int&ans) {
int cur = 0;
for (auto&v : adj[u]) cur = max(cur, dp[v]);
if (cur < mn) mn = cur, ans = u;
for (auto&v : adj[u]) {
if (v == e) continue;
dp[u] -= dp[v], dp[v] += dp[u];
reroot(v, u, adj, dp, P, mn, ans);
dp[v] -= dp[u], dp[u] += dp[v];
}
}
int LocateCentre(int N, int P[], int S[], int D[]) {
// build adjacency array
vector<vector<int>> adj(N + 5);
for (int i = 0; i < N - 1; i++) adj[S[i]].push_back(D[i]), adj[D[i]].push_back(S[i]);
// dp
vector<int> dp(N + 5);
dfs(0, -1, adj, dp, P);
// solve for all roots
int mn = 1e18, ans = -1;
reroot(0, -1, adj, dp, P, mn, ans);
return ans;
}