#include <bits/stdc++.h>
using namespace std;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int NMAX = 1e6;
int a[NMAX], dp[NMAX], sum[NMAX], ans;
vector<int> G[NMAX];
void dfs(int u, int parent) {
sum[u] = a[u];
for (int v : G[u])
if (v != parent) {
dfs(v, u);
sum[u] += sum[v];
if (sum[v] > dp[u])
dp[u] = sum[v];
}
}
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 u = 0; u < N; ++u)
a[u] = pp[u];
dfs(0, -1);
for (int u = 0; u < N; ++u) {
if (sum[1] - sum[u] > dp[u])
dp[u] = sum[1] - sum[u];
if (dp[u] < dp[ans])
ans = u;
}
return ans;
}
/*int N, P[NMAX], S[NMAX], D[NMAX];
int main() {
fastIO();
cin >> N;
for (int i = 0; i < N; ++i)
cin >> P[i];
for (int i = 0; i < N - 1; ++i)
cin >> S[i] >> D[i];
cout << LocateCentre(N, P, S, D) << '\n';
return 0;
} */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23828 KB |
Output is correct |
3 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23828 KB |
Output is correct |
3 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23828 KB |
Output is correct |
3 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23828 KB |
Output is correct |
3 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |