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 "traffic.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 69;
int dp[N], a[N], ans = 2e9 + 69, gas = -1;
vector<int> g[N];
void dfs(int v, int p) {
dp[v] = a[v];
for (auto u : g[v]) {
if (u == p)
continue;
dfs(u, v);
dp[v] += dp[u];
}
}
void solve(int v, int p) {
int mx = 0, s = 0;
for (auto u : g[v]) {
if (u == p)
continue;
s += dp[u];
mx = max(mx, dp[u]);
}
mx = max(mx, dp[1] - s);
if (mx <= ans) {
gas = v;
ans = mx;
}
for (auto u : g[v]) {
if (u == p)
continue;
solve(u, v);
}
}
int LocateCentre(int n, int p[], int u[], int v[]) {
for (int i = 0;i < n;i++)
a[i] = p[i];
for (int i = 0;i < n - 1;i++) {
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
}
dfs(0, -1);
solve(0, -1);
return gas;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |