# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
895176 | Irate | Traffic (IOI10_traffic) | C++14 | 0 ms | 0 KiB |
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>
using namespace std;
vector<vector<int>>G;
vector<long long>sz, val, res;
int n;
long long sum = 0;
void dfs(int node, int par){
sz[node] = val[node];
for(int &v : G[node]){
if(v != par){
dfs(v, node);
sz[node] += sz[v];
}
}
long long sub_sum = 0;
for(int &v : G[node]){
res[node] = max(res[node], sz[v]);
sub_sum += sz[v];
}
res[node] = max(res[node], sum - sub_sum);
}
int LocateCentre(int N, vector<int>&P, vector<int>&S, vector<int>&D){
int n = N;
G.resize(n);
sz.resize(n);
res.resize(n);
val.resize(n);
for(int i = 0;i < n;++i){
val[i] = P[i];
sum += P[i];
}
for(int i = 0;i < n - 1;++i){
G[S[i]].push_back(D[i]);
G[D[i]].push_back(S[i]);
}
dfs(0, 0);
long long mn = 1e18, id = -1;
for(int i = 0;i < n;++i){
if(res[i] < mn){
mn = res[i];
id = i;
}
}
return id;
}
// int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// int n;
// cin >> n;
// vector<int>P(n), S(n - 1), D(n - 1);
// 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);
// }