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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
const int MAXN=1e5+5;
vector<vector<int>> listaAdy;
vector<ll>dp;
int n;
ll res=1e18;
int answer=-1;
ll total=0;
void dfs1(int nodo, int padre, int P[]){
ll maxi=0;
for(int v: listaAdy[nodo]){
if (v==padre) continue;
dfs1(v, nodo, P);
dp[nodo]+=dp[v];
maxi=max(maxi, dp[v]);
}dp[nodo]+=P[nodo];
maxi=max(maxi, total-dp[nodo]);
if (maxi<res){
res=maxi;
answer=nodo;
}return;
}
int LocateCentre(int N, int P[], int S[], int D[]) {
listaAdy.clear();
listaAdy.resize(N);
dp.clear();
dp.resize(N);
for (int i=0; i<N; i++) total+=P[i];
for (int i=0; i<N-1; i++){
listaAdy[S[i]].push_back(D[i]);
listaAdy[D[i]].push_back(S[i]);
}dfs1(0,0,P);
return answer;
}
# | 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... |