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>
using namespace std;
#define ll int64_t
#define all(x) x.begin(), x.end()
int LocateCentre(int N, int pp[], int S[], int D[]) {
vector<vector<int>> adj(N);
vector<ll> ans(N);
vector<ll> subtree(N, 0);
ll sum = 0;
for(int i = 0; i < N-1; ++i){
adj[S[i]].push_back(D[i]);
adj[D[i]].push_back(S[i]);
}
for(int i = 0; i < N; ++i){
subtree[i] += pp[i];
sum += pp[i];
}
auto dfs = [&](auto self, int edge, int prev) -> void{
for(auto& e : adj[edge]){
if(e == prev) continue;
self(self, e, edge);
subtree[edge] += subtree[e];
ans[edge] = max(ans[edge], subtree[e]);
}
ans[edge] = max(ans[edge], sum - subtree[edge]);
};
dfs(dfs, 0, 0);
return min_element(all(ans)) - ans.begin();
}
// subtree[i] originally == node weight
// Increment it each time by weights of its subnodes
// Answer could be max(ans, sum(of all weights) - ans)
# | 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... |