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>
int LocateCentre(int n, int p[], int s[], int d[]) {
using namespace std;
vector<vector<int>> adj(n);
for (int i = 0; i + 1 < n; ++i) {
adj[s[i]].emplace_back(d[i]);
adj[d[i]].emplace_back(s[i]);
}
auto dfs_rev_order = [&](int root) {
vector<int> stk = {root}, ord(n);
for (auto& u : ord) {
u = stk.back();
stk.pop_back();
for (const auto& v : adj[u]) {
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
stk.emplace_back(v);
}
}
reverse(ord.begin(), ord.end());
return ord;
}(0);
const auto total = accumulate(p, p + n, 0);
vector<int> subtree_size(n);
vector<int64_t> dp(n);
for (const auto& u : dfs_rev_order) {
for (const auto& v : adj[u]) {
subtree_size[u] += subtree_size[v];
dp[u] += subtree_size[v];
}
}
int u = 0;
for (bool update = true; update;) {
update = false;
for (const auto& v : adj[u]) {
if (dp[u] > dp[v] + (total - subtree_size[v])) {
u = v;
update = true;
}
}
}
return u;
}
# | 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... |