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;
using ll = long long;
const ll INF = 1e18;
const int N = 1001000;
ll dp[2][N];
vector<int> adj[N];
int a[N], ans = -1;
ll mn = INF;
void dfs(int v, int p = -1) {
dp[0][v] = dp[1][v] = 0;
for (auto& x : adj[v]) {
if (x == p) continue;
dfs(x, v);
dp[0][v] += dp[0][x] + a[x];
dp[1][v] = max(dp[1][v], dp[0][x] + a[x]);
}
}
void dfs2(int v, int p = -1, ll w = 0) {
if (max(w, dp[1][v]) < mn) {
mn = max(w, dp[1][v]);
ans = v;
}
for (auto& x : adj[v]) {
if (x == p) continue;
dfs2(x, v, dp[0][v] - dp[0][x] - a[x] + w);
}
}
int LocateCentre(int n, int p[], int s[], int d[]) {
for (int i = 0;i < n;i++) {
a[i] = p[i];
}
for (int i = 0;i < n - 1;i++) {
adj[s[i]].push_back(d[i]);
adj[d[i]].push_back(s[i]);
}
dfs(0);
dfs2(0);
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... |