# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
524846 | antonioqbab | 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;
using ll = long long;
int LocateCentre(int n, int p[], int s[], int d[]){
vector<vector<int>> G(n);
for(int i=0;i<n-1;++i){
G[s[i]].emplace_back(d[i]);
G[d[i]].emplace_back(s[i]);
}
vector<ll> dp(n), dp2(n);
// dp[node][edge] (max excluding edge)
function<void(int,int)> dfs=[&](int node, int last){
ll cur = 0;
for(auto x:G[node])
if(x!=last){
dfs(x,node);
cur=max(cur,dp[x]);
}
dp[node]=cur+p[node];
};
dfs(0,-1);
ll ans = dp[0] - p[0];
dp2[0]=dp[0];
//cout << ans << '\n';
function<void(int,int)> dfs2=[&](int node, int last){
ans = min(ans, dp2[node]-p[node]);
//cout << node << ' ' << dp[node] << ' ' << dp2[node] << '\n';
for(auto x:G[node])
if(x!=last){
dp2[x]=max(dp[x],dp2[node]+p[x]);
dfs2(x,node);
}
};
dfs2(0,-1);
return ans;
}
int main(){
int n = 5;
int *p=new int[n], *s=new int[n], *d=new int [n];
p[0]=p[1]=p[2]=10;
p[3]=p[4]=20;
s[0]=0,d[0]=2;
s[1]=1,d[1]=2;
s[2]=2,d[2]=3;
s[3]=3,d[3]=4;
cout << LocateCentre(n,p,s,d);
}