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;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pi;
vector<int> adj[1000000];
ll p[1000000];
ll sub[1000000];
ll res;
ll mt[1000000];
void dfs(int u, int par, ll pc) {
ll tsum = 0, mv = 0;
for (auto v : adj[u]) {
if (v == par) continue;
tsum += sub[v];
mv = max(mv, sub[v]);
}
mt[u] = max(mv, pc);
for (auto v : adj[u]) {
if (v == par) continue;
dfs(v, u, pc+tsum-sub[v]+p[u]);
}
}
ll gsum(int u, int par) {
ll tsum = p[u];
for (auto v : adj[u]) {
if (v == par) continue;
tsum += gsum(v, u);
}
sub[u] = tsum;
return sub[u];
}
int LocateCentre(int N, int P[], int S[], int D[]) {
for (int i = 0; i < N; i++) adj[i].clear();
for (int i = 0; i < N; i++) p[i] = P[i];
res = 1e18;
//fill(mt, mt+N, 0);
for (int i = 0; i < N-1; i++) {
int a = S[i], b = D[i];
adj[a].push_back(b);
adj[b].push_back(a);
}
gsum(0, -1);
dfs(0, -1, 0);
//cout << "Debugging array" << endl;
//for (int i = 0; i < N; i++) cout << mt[i] << " "; cout << endl;
//for (int i = 0; i < N; i++) cout << sub[i] << " "; cout << endl;
int mi = 0;
for (int i = 1; i < N; i++) {
if (mt[i] < mt[mi]) mi = i;
}
return mi;
}
# | 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... |