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>
#include "traffic.h"
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
vector<int> g[1000003];
int sz[1000003], ppl[1000003];
bool visited[1000003];
void dfs(int n) {
visited[n] = 1;
sz[n] = ppl[n];
for (auto x : g[n]) {
if (visited[x]) continue;
dfs(x);
sz[n] += sz[x];
}
}
int LocateCentre(int N, int pp[], int S[], int D[]) {
for (int i = 0; i < N - 1; i++) g[S[i]].push_back(D[i]);
for (int i = 0; i < N; i++) ppl[i] = pp[i];
dfs(0);
queue<pair<int, int>> q;
q.push({0, 0});
memset(visited, 0, sizeof(visited));
visited[0] = 1;
ll best = LLONG_MAX, id = 0;
while (q.size() > 0) {
int place = q.front().first, sum = q.front().second, cnt = 0, mx = q.front().second;
q.pop();
for (auto x : g[place]) {
if (visited[x]) continue;
mx = max(mx, sz[x]);
cnt += sz[x];
}
if (mx < best) {
best = mx;
id = place;
}
for (auto x : g[place]) {
if (visited[x]) continue;
visited[x] = 1;
q.push({x, sz[place] + sum + cnt - sz[x]});
}
}
return id;
}
# | 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... |