# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
287911 | Bill_00 | 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 <traffic.h>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<long long>adj[1000001];
vector<long long>leaf;
long long p[1000001];
long long sum[1000001];
long long dp[1000001],vis[1000001];
ll b[1000001];
void dfs(ll j){
if(adj[j].size()==1 && p[j]==adj[j][0]){
leaf.push_back(j);
return;
}
for(ll i=0;i<adj[j].size();i++){
if(adj[j][i]!=p[j]){
p[adj[j][i]]=j;
dfs(adj[j][i]);
}
}
}
long long calc(ll j){
sum[j]=(long long)b[j];
for(ll i=0;i<adj[j].size();i++){
if(adj[j][i]!=p[j]){
sum[j]+=calc(adj[j][i]);
}
}
return sum[j];
}
void check(ll j){
for(ll i=0;i<adj[j].size();i++){
if(vis[adj[j][i]]==0 && adj[j][i]!=p[j]){
dp[adj[j][i]]=dp[j]-(2*sum[adj[j][i]])+sum[0];
vis[adj[j][i]]=1;
check(adj[j][i]);
}
}
if(j!=0){
dp[p[j]]=dp[j]+2*sum[j]-sum[0];
check(p[j]);
}
}
ll LocateCentre(int n,int a[],int s[],int d[]){
for(ll i=0;i<(ll)n;i++){
b[i]=(ll)a[i];
}
memset(vis,0,sizeof(vis));
for(ll i=0;i<(ll)(n-1);i++){
adj[(ll)s[i]].push_back((ll)d[i]);
adj[(ll)d[i]].push_back((ll)s[i]);
}
p[0]=-1;
dfs(0);
calc(0);
dp[leaf[0]]=0;
vis[leaf[0]]=1;
long long ans=2e9,id;
check(leaf[0]);
for(ll i=0;i<(ll)n;i++){
//cout << dp[i] << endl;
if(dp[i]<ans){
id=i;
ans=dp[i];
}
}
return id;
}