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>
#define pb push_back
#define ll long long
#include "traffic.h"
using namespace std;
ll tot,dp[1000005],ansval;
int ans;
vector <int> v[1000005];
void dfs(int x,int par){
ll r=0,sum=0;
for (int to: v[x]){
if (to==par) continue;
dfs(to,x);
r=max(r,dp[to]);
sum += dp[to];
}
r=max(r,tot - dp[x] - sum);
dp[x]+= sum;
if (r < ansval) ansval = r,ans = x;
}
int LocateCentre(int n, int p[], int S[], int D[]) {
ans=0;
ansval = 1e18;
for (int i=0;i<n;i++)
tot += p[i],dp[i] = p[i];
for (int i = 0; i < n - 1; i++){
v[S[i]].pb(D[i]);
v[D[i]].pb(S[i]);
}
dfs(0,0);
for (int i=0;i<n;i++) v[i].clear(),dp[i]=0;
return 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... |