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>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MX = 1e6+5;
int sum, sz[MX];
ii ans(2e9, 0);
vector<int> G[MX];
void dfs(int u, int p) {
for (int v : G[u]) if (v != p)
dfs(v, u), sz[u] += sz[v];
}
void solve(int u, int p, int P[]) {
int up = sum-P[u], mx = 0;
for (int v : G[u]) if (v != p)
up -= sz[v], mx = max(sz[v], mx);
mx = max(up, mx);
if (mx < ans.f) ans = ii(mx, u);
for (int v : G[u]) if (v != p)
solve(v, u, P);
}
int LocateCentre(int N, int P[], int S[], int D[]) {
for (int i = 0; i < N-1; ++i)
G[S[i]].push_back(D[i]),
G[D[i]].push_back(S[i]);
for (int i = 0; i < N; ++i)
sum += P[i], sz[i] = P[i];
dfs(0, -1);
solve(0, -1, P);
return ans.s;
}
# | 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... |