이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
#define ll long long
const int maxN = 1e6;
vector<int> adj[maxN];
int people[maxN];
int children[maxN];
int total;
// use DP on trees to calculate the max congestion starting from src => people[src]
// count number of children in postorder DFS
void DFS(int P[], int src, int par) {
for (int x : adj[src]) {
if (x != par) {
DFS(P, x, src);
children[src] += children[x];
people[src] = max(people[src], children[x]);
}
}
children[src] += P[src];
people[src] = max(people[src], total - children[src]);
}
int LocateCentre(int N, int P[], int S[], int D[]) {
ll minCongestion = 2e9;
int ans = -1;
total = accumulate(P, P + N, 0);
for (int i = 0; i < N - 1; i++) {
adj[S[i]].push_back(D[i]);
adj[D[i]].push_back(S[i]);
}
DFS(P, 0, -1);
for (int i = 0; i < N; i++) {
if (people[i] < minCongestion) {
minCongestion = people[i];
ans = i;
}
}
cout << "people: ";
for (int i = 0; i < N; i++) {
cout << people[i] << " ";
}
cout << endl;
cout << "children: ";
for (int i = 0; i < N; i++) {
cout << children[i] << " ";
}
cout << endl;
return ans;
}
// int main() {
// int a1[5] = {10, 10, 10, 20, 20};
// int a2[4] = {0, 1, 2, 3};
// int a3[4] = {2, 2, 3, 4};
// cout << LocateCentre(5, a1, a2, a3) << endl;
// }
# | 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... |