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;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int LocateCentre(int n, int* a, int* e1, int* e2) {
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
g[e1[i]].emplace_back(e2[i]);
g[e2[i]].emplace_back(e1[i]);
}
function<void(int, int)> dfs = [&](int v, int p) {
for (int to : g[v]) {
if (to == p) {
continue;
}
dfs(to, v);
a[v] += a[to];
}
};
dfs(0, -1);
int ans = -1;
int sum = (int) 2e9 + 10;
function<void(int, int)> reroot = [&](int v, int p) {
int mx = -1;
for (int to : g[v]) {
mx = max(mx, a[to]);
}
if (mx < sum) {
sum = mx;
ans = v;
}
for (int to : g[v]) {
if (to == p) {
continue;
}
int a_v = a[v];
int a_to = a[to];
a[v] = a_v - a_to;
a[to] = a_v;
reroot(to, v);
a[v] = a_v;
a[to] = a_to;
}
};
reroot(0, -1);
ans++;
return ans;
}
# | 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... |