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 "race.h"
#include <algorithm>
#include <vector>
#include <queue>
#define INF 0x7fffffff
using namespace std;
typedef pair <int,int> ppair;
static vector <ppair> A[200000];
static vector <ppair> Length;
static vector <int> Eraser;
static int N, K, Die[200000], Check[200000], D[1000001];
static int Num, Center, Ans=INF;
int HowManyNode(int Now) {
int i, j=A[Now].size(), Temp=1;
Check[Now]=1;
for(i=0 ; i<j ; i++)
if(!Die[A[Now][i].first] && !Check[A[Now][i].first])
Temp+=HowManyNode(A[Now][i].first);
Check[Now]=0;
return Temp;
}
int FindCenter(int Now) {
int i, j=A[Now].size(), OK=1, Sum=1, Temp;
Check[Now]=1;
for(i=0 ; i<j ; i++)
if(!Die[A[Now][i].first] && !Check[A[Now][i].first]) {
Temp=FindCenter(A[Now][i].first);
Sum+=Temp;
if(Temp>Num/2)
OK=0;
}
Check[Now]=0;
if(Num-Sum>Num/2)
OK=0;
if(OK)
Center=Now;
return Sum;
}
void FindLength(int Now, int Len, int Lev) {
int i, j=A[Now].size();
Check[Now]=1;
if(Len<=K)
Length.push_back(make_pair(Len,Lev));
for(i=0 ; i<j ; i++)
if(!Die[A[Now][i].first] && !Check[A[Now][i].first])
FindLength(A[Now][i].first,Len+A[Now][i].second,Lev+1);
Check[Now]=0;
}
void DivideConquer(int Route) {
int i, j=A[Route].size(), k, l, Temp;
Eraser.clear();
for(i=0 ; i<j ; i++)
if(!Die[A[Route][i].first]) {
Length.clear();
FindLength(A[Route][i].first,A[Route][i].second,1);
l=Length.size();
for(k=0 ; k<l ; k++)
if(D[K-Length[k].first] || Length[k].first==K)
if(Ans>Length[k].second+D[K-Length[k].first])
Ans=Length[k].second+D[K-Length[k].first];
for(k=0 ; k<l ; k++) {
if(!D[Length[k].first]) {
Eraser.push_back(Length[k].first);
D[Length[k].first]=Length[k].second;
}
else if(D[Length[k].first]>Length[k].second)
D[Length[k].first]=Length[k].second;
}
}
l=Eraser.size();
for(k=0 ; k<l ; k++)
D[Eraser[k]]=0;
for(i=0 ; i<j ; i++)
if(!Die[A[Route][i].first]) {
Num=HowManyNode(A[Route][i].first);
Temp=FindCenter(A[Route][i].first);
Die[Center]=1;
DivideConquer(Center);
}
}
int best_path(int N_, int K_, int H[][2], int L[]) {
int i, Temp;
N=N_;
K=K_;
for(i=0 ; i<N-1 ; i++) {
A[H[i][0]].push_back(make_pair(H[i][1],L[i]));
A[H[i][1]].push_back(make_pair(H[i][0],L[i]));
}
Num=HowManyNode(0);
Temp=FindCenter(0);
Die[Center]=1;
DivideConquer(Center);
if(Ans==INF)
return -1;
return Ans;
}
Compilation message (stderr)
race.cpp: In function 'void DivideConquer(int)':
race.cpp:63:37: warning: variable 'Temp' set but not used [-Wunused-but-set-variable]
int i, j=A[Route].size(), k, l, Temp;
^~~~
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:98:12: warning: variable 'Temp' set but not used [-Wunused-but-set-variable]
int i, Temp;
^~~~
# | 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... |