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;
struct diam{
long long int max1=0;
long long int max2=0;
};
vector<vector<int>> listaAdy;
vector<diam>dp;
int n;
ll res=1e18;
int answer=-1;
void dfs1(int nodo, int padre, int P[]){
for(int v: listaAdy[nodo]){
if (v==padre) continue;
dfs1(v, nodo, P);
if (dp[nodo].max1<dp[v].max1){
dp[nodo].max2=dp[nodo].max1;
dp[nodo].max1=dp[v].max1;
}else if (dp[nodo].max2<dp[v].max1){
dp[nodo].max2=dp[v].max1;
}
}dp[nodo].max1+=P[nodo];
dp[nodo].max2+=P[nodo];
return;
}
void dfs2(int nodo, int padre, ll maxi, int P[]){
ll maxini=maxi;
if (res>max(maxi, dp[nodo].max1-P[nodo])){
res=max(maxi, dp[nodo].max1-P[nodo]);
answer=nodo;
}
for(int v: listaAdy[nodo]){
if(v==padre) continue;
if(dp[nodo].max1==dp[v].max1+P[nodo]){
maxi=max(maxini+P[nodo], dp[nodo].max2);
}else{
maxi=max(maxini+P[nodo], dp[nodo].max1);
}
dfs2(v, nodo, maxi, P);
}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-1; i++){
listaAdy[S[i]].push_back(D[i]);
listaAdy[D[i]].push_back(S[i]);
}dfs1(0,0,P);
dfs2(0,0,0,P);
return answer;
}
/*int main(){
int N, P[MAXN], S[MAXN], D[MAXN];
cin>>N;
for (int i=0; i<N; i++) cin>>P[i];
for (int i=0; i<N-1; i++){
cin>>S[i]>>D[i];
}
cout<<LocateCentre(N, P, S, D)<<endl;
cout<<res<<endl;
return 0;
}*/
# | 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... |