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 int64 = long long;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int NMAX = 1e6 + 6;
int a[NMAX], ans;
int64 dp[NMAX], sum[NMAX];
vector<int> G[NMAX];
void dfs1(int u, int parent) {
sum[u] = a[u];
for (int v : G[u])
if (v != parent) {
dfs1(v, u);
sum[u] += sum[v];
if (sum[v] > dp[u])
dp[u] = sum[v];
}
}
void dfs2(int u, int parent, int64 s) {
if (dp[u] < dp[ans])
ans = u;
for (int v : G[u])
if (v != parent) {
int64 aux = dp[v];
if (s + a[u] > dp[v])
dp[v] = s + a[u];
dfs2(v, u, s + a[u]);
dp[v] = aux;
}
}
int LocateCentre(int N, int pp[], int S[], int D[]) {
for (int i = 0; i < N - 1; ++i) {
G[S[i]].emplace_back(D[i]);
G[D[i]].emplace_back(S[i]);
}
for (int i = 0; i < N; ++i)
a[i] = pp[i];
dfs1(0, -1);
dfs2(0, -1, 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... |