# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1018293 | gutzzy | Traffic (IOI10_traffic) | C++17 | 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>
#include "traffic.h"
using namespace std;
vector<int> dp;
vector<vector<int>> lst;
pair<int,int> ans;
int calc(int nodo, int padre, vector<int> &p){
if(dp[nodo]!=-1) return dp[nodo];
int res = p[nodo];
for(auto vecino:lst[nodo]){
if(vecino!=padre) res += calc(vecino,nodo,p);
}
dp[nodo] = res;
return dp[nodo];
}
void dfs(int nodo, int padre, vector<int> &p){
dp[padre] = -1;
int res = 0;
for(auto vecino:lst[nodo]){
res = max(res, calc(vecino,nodo,p));
}
//cout << "arena: " << nodo << " res: " << res << endl;
if(res<ans.second){
//cout << "update! new arena: " << nodo << " " << res << endl;
ans.second = res;
ans.first = nodo;
}
for(auto vecino:lst[nodo]){
if(vecino!=padre) dfs(vecino,nodo,p);
}
return;
}
int LocateCentre(int n, int *p, int *s, int *d){
dp = vector<int>(n,-1);
ans.first = 0;
ans.second = 2e9;
lst = vector<vector<int>>(n,vector<int>());
for(int i=0;i<n-1;i++){
lst[s[i]].push_back(d[i]);
lst[d[i]].push_back(s[i]);
}
dfs(0,0,p);
return ans.first;
}
/*
int main(){
cout << LocateCentre(5,{10,10,10,20,20},{0,1,2,3},{2,2,3,4}) << endl;
return 0;
}
*/