#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
#define MAX_N 200000
#define MAX_K 1000000
struct edge{int u,c;};
typedef vector<edge> vec;
typedef vector<vec> mat;
mat path;
int n,comet,SubSz[MAX_N],MaxSz[MAX_N];
bool killed[MAX_N];
bool chk[MAX_N];
vector<int> s;
int dfs(int v){
chk[v]=1;
s.push_back(v);
int ret=0;
SubSz[v]=MaxSz[v]=1;
for(int i=0;i<path[v].size();i++){
int u=path[v][i].u;
if(killed[u]||chk[u])continue;
ret=dfs(u);
SubSz[v]+=ret;
MaxSz[v]=max(MaxSz[v],ret+1);
}
chk[v]=0;
return SubSz[v];
}
int centroid(int v){
s.clear();
dfs(v);
int x,ret=v,Max=0;
for(int i=0;i<s.size();i++){
x=s[i];
if(Max<min(SubSz[v]-MaxSz[x],MaxSz[x])){
Max=min(SubSz[v]-MaxSz[x],MaxSz[x]);
ret=x;
}
}
return ret;
}
int stamp[MAX_K+1],a[MAX_K+1];
struct state{int v,c,len;};
int f(int v){
int root=centroid(v);
killed[root]=1;
int ret=1e9;
for(int i=0;i<path[root].size();i++){
int u=path[root][i].u;
if(killed[u])continue;
queue<state> Q;
Q.push(state{u,path[root][i].c,1});
chk[u]=1;
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c==comet)ret=min(ret,h.len);
if(h.c>=comet)continue;
if(stamp[comet-h.c]==root)ret=min(ret,a[comet-h.c]+h.len);
for(int j=0;j<path[h.v].size();j++){
int uu=path[h.v][j].u;
if(killed[uu]||chk[uu])continue;
chk[uu]=1;
Q.push(state{uu,h.c+path[h.v][j].c,h.len+1});
}
}
Q.push(state{u,path[root][i].c,1});
chk[u]=0;
while(!Q.empty()){
state h=Q.front();
Q.pop();
if(h.c>=comet)continue;
if(stamp[h.c]==root)
a[h.c]=min(a[h.c],h.len);
else
stamp[h.c]=root,a[h.c]=h.len;
for(int j=0;j<path[h.v].size();j++){
int uu=path[h.v][j].u;
if(killed[uu]||chk[uu]==0)continue;
chk[uu]=0;
Q.push(state{uu,h.c+path[h.v][j].c,h.len+1});
}
}
}
for(int i=0;i<path[root].size();i++){
int u=path[root][i].u;
if(killed[u])continue;
ret=min(ret,f(u));
}
killed[root]=0;
return ret;
}
int best_path(int N, int K, int H[][2], int L[]){
memset(stamp,-1,sizeof(stamp));
n=N;
comet=K;
path.assign(n,vec());
for(int i=0;i<n;i++){
path[H[i][0]].push_back(edge{H[i][1],L[i]});
path[H[i][1]].push_back(edge{H[i][0],L[i]});
}
int ret=f(0);
return ret==1e9?-1:ret;
}
Compilation message
race.cpp: In function 'int dfs(int)':
race.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[v].size();i++){
~^~~~~~~~~~~~~~~
race.cpp: In function 'int centroid(int)':
race.cpp:43:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<s.size();i++){
~^~~~~~~~~
race.cpp: In function 'int f(int)':
race.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[root].size();i++){
~^~~~~~~~~~~~~~~~~~
race.cpp:78:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp:95:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<path[h.v].size();j++){
~^~~~~~~~~~~~~~~~~
race.cpp:104:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<path[root].size();i++){
~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Correct |
5 ms |
4400 KB |
Output is correct |
4 |
Correct |
5 ms |
4476 KB |
Output is correct |
5 |
Correct |
5 ms |
4512 KB |
Output is correct |
6 |
Correct |
5 ms |
4512 KB |
Output is correct |
7 |
Correct |
4 ms |
4512 KB |
Output is correct |
8 |
Correct |
5 ms |
4512 KB |
Output is correct |
9 |
Correct |
5 ms |
4512 KB |
Output is correct |
10 |
Correct |
5 ms |
4512 KB |
Output is correct |
11 |
Correct |
5 ms |
4512 KB |
Output is correct |
12 |
Correct |
5 ms |
4512 KB |
Output is correct |
13 |
Correct |
5 ms |
4512 KB |
Output is correct |
14 |
Correct |
5 ms |
4512 KB |
Output is correct |
15 |
Correct |
5 ms |
4512 KB |
Output is correct |
16 |
Correct |
5 ms |
4512 KB |
Output is correct |
17 |
Correct |
5 ms |
4588 KB |
Output is correct |
18 |
Correct |
5 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Correct |
5 ms |
4400 KB |
Output is correct |
4 |
Correct |
5 ms |
4476 KB |
Output is correct |
5 |
Correct |
5 ms |
4512 KB |
Output is correct |
6 |
Correct |
5 ms |
4512 KB |
Output is correct |
7 |
Correct |
4 ms |
4512 KB |
Output is correct |
8 |
Correct |
5 ms |
4512 KB |
Output is correct |
9 |
Correct |
5 ms |
4512 KB |
Output is correct |
10 |
Correct |
5 ms |
4512 KB |
Output is correct |
11 |
Correct |
5 ms |
4512 KB |
Output is correct |
12 |
Correct |
5 ms |
4512 KB |
Output is correct |
13 |
Correct |
5 ms |
4512 KB |
Output is correct |
14 |
Correct |
5 ms |
4512 KB |
Output is correct |
15 |
Correct |
5 ms |
4512 KB |
Output is correct |
16 |
Correct |
5 ms |
4512 KB |
Output is correct |
17 |
Correct |
5 ms |
4588 KB |
Output is correct |
18 |
Correct |
5 ms |
4588 KB |
Output is correct |
19 |
Correct |
5 ms |
4588 KB |
Output is correct |
20 |
Correct |
5 ms |
4588 KB |
Output is correct |
21 |
Correct |
6 ms |
4604 KB |
Output is correct |
22 |
Correct |
8 ms |
7036 KB |
Output is correct |
23 |
Correct |
7 ms |
7036 KB |
Output is correct |
24 |
Correct |
8 ms |
7420 KB |
Output is correct |
25 |
Correct |
7 ms |
7420 KB |
Output is correct |
26 |
Correct |
7 ms |
7420 KB |
Output is correct |
27 |
Correct |
5 ms |
7420 KB |
Output is correct |
28 |
Correct |
6 ms |
7420 KB |
Output is correct |
29 |
Correct |
6 ms |
7420 KB |
Output is correct |
30 |
Correct |
7 ms |
7420 KB |
Output is correct |
31 |
Correct |
8 ms |
7420 KB |
Output is correct |
32 |
Correct |
7 ms |
7420 KB |
Output is correct |
33 |
Correct |
10 ms |
7420 KB |
Output is correct |
34 |
Correct |
9 ms |
7420 KB |
Output is correct |
35 |
Correct |
9 ms |
7548 KB |
Output is correct |
36 |
Correct |
8 ms |
7932 KB |
Output is correct |
37 |
Correct |
7 ms |
7932 KB |
Output is correct |
38 |
Correct |
7 ms |
7932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Correct |
5 ms |
4400 KB |
Output is correct |
4 |
Correct |
5 ms |
4476 KB |
Output is correct |
5 |
Correct |
5 ms |
4512 KB |
Output is correct |
6 |
Correct |
5 ms |
4512 KB |
Output is correct |
7 |
Correct |
4 ms |
4512 KB |
Output is correct |
8 |
Correct |
5 ms |
4512 KB |
Output is correct |
9 |
Correct |
5 ms |
4512 KB |
Output is correct |
10 |
Correct |
5 ms |
4512 KB |
Output is correct |
11 |
Correct |
5 ms |
4512 KB |
Output is correct |
12 |
Correct |
5 ms |
4512 KB |
Output is correct |
13 |
Correct |
5 ms |
4512 KB |
Output is correct |
14 |
Correct |
5 ms |
4512 KB |
Output is correct |
15 |
Correct |
5 ms |
4512 KB |
Output is correct |
16 |
Correct |
5 ms |
4512 KB |
Output is correct |
17 |
Correct |
5 ms |
4588 KB |
Output is correct |
18 |
Correct |
5 ms |
4588 KB |
Output is correct |
19 |
Correct |
222 ms |
13180 KB |
Output is correct |
20 |
Correct |
262 ms |
13180 KB |
Output is correct |
21 |
Correct |
214 ms |
13308 KB |
Output is correct |
22 |
Correct |
184 ms |
13320 KB |
Output is correct |
23 |
Correct |
199 ms |
13436 KB |
Output is correct |
24 |
Correct |
145 ms |
13436 KB |
Output is correct |
25 |
Correct |
247 ms |
14456 KB |
Output is correct |
26 |
Correct |
183 ms |
15736 KB |
Output is correct |
27 |
Correct |
307 ms |
21868 KB |
Output is correct |
28 |
Correct |
532 ms |
26864 KB |
Output is correct |
29 |
Correct |
387 ms |
26864 KB |
Output is correct |
30 |
Correct |
300 ms |
26864 KB |
Output is correct |
31 |
Correct |
245 ms |
26864 KB |
Output is correct |
32 |
Correct |
315 ms |
26864 KB |
Output is correct |
33 |
Correct |
434 ms |
26864 KB |
Output is correct |
34 |
Correct |
341 ms |
26864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4216 KB |
Output is correct |
2 |
Correct |
5 ms |
4324 KB |
Output is correct |
3 |
Correct |
5 ms |
4400 KB |
Output is correct |
4 |
Correct |
5 ms |
4476 KB |
Output is correct |
5 |
Correct |
5 ms |
4512 KB |
Output is correct |
6 |
Correct |
5 ms |
4512 KB |
Output is correct |
7 |
Correct |
4 ms |
4512 KB |
Output is correct |
8 |
Correct |
5 ms |
4512 KB |
Output is correct |
9 |
Correct |
5 ms |
4512 KB |
Output is correct |
10 |
Correct |
5 ms |
4512 KB |
Output is correct |
11 |
Correct |
5 ms |
4512 KB |
Output is correct |
12 |
Correct |
5 ms |
4512 KB |
Output is correct |
13 |
Correct |
5 ms |
4512 KB |
Output is correct |
14 |
Correct |
5 ms |
4512 KB |
Output is correct |
15 |
Correct |
5 ms |
4512 KB |
Output is correct |
16 |
Correct |
5 ms |
4512 KB |
Output is correct |
17 |
Correct |
5 ms |
4588 KB |
Output is correct |
18 |
Correct |
5 ms |
4588 KB |
Output is correct |
19 |
Correct |
5 ms |
4588 KB |
Output is correct |
20 |
Correct |
5 ms |
4588 KB |
Output is correct |
21 |
Correct |
6 ms |
4604 KB |
Output is correct |
22 |
Correct |
8 ms |
7036 KB |
Output is correct |
23 |
Correct |
7 ms |
7036 KB |
Output is correct |
24 |
Correct |
8 ms |
7420 KB |
Output is correct |
25 |
Correct |
7 ms |
7420 KB |
Output is correct |
26 |
Correct |
7 ms |
7420 KB |
Output is correct |
27 |
Correct |
5 ms |
7420 KB |
Output is correct |
28 |
Correct |
6 ms |
7420 KB |
Output is correct |
29 |
Correct |
6 ms |
7420 KB |
Output is correct |
30 |
Correct |
7 ms |
7420 KB |
Output is correct |
31 |
Correct |
8 ms |
7420 KB |
Output is correct |
32 |
Correct |
7 ms |
7420 KB |
Output is correct |
33 |
Correct |
10 ms |
7420 KB |
Output is correct |
34 |
Correct |
9 ms |
7420 KB |
Output is correct |
35 |
Correct |
9 ms |
7548 KB |
Output is correct |
36 |
Correct |
8 ms |
7932 KB |
Output is correct |
37 |
Correct |
7 ms |
7932 KB |
Output is correct |
38 |
Correct |
7 ms |
7932 KB |
Output is correct |
39 |
Correct |
222 ms |
13180 KB |
Output is correct |
40 |
Correct |
262 ms |
13180 KB |
Output is correct |
41 |
Correct |
214 ms |
13308 KB |
Output is correct |
42 |
Correct |
184 ms |
13320 KB |
Output is correct |
43 |
Correct |
199 ms |
13436 KB |
Output is correct |
44 |
Correct |
145 ms |
13436 KB |
Output is correct |
45 |
Correct |
247 ms |
14456 KB |
Output is correct |
46 |
Correct |
183 ms |
15736 KB |
Output is correct |
47 |
Correct |
307 ms |
21868 KB |
Output is correct |
48 |
Correct |
532 ms |
26864 KB |
Output is correct |
49 |
Correct |
387 ms |
26864 KB |
Output is correct |
50 |
Correct |
300 ms |
26864 KB |
Output is correct |
51 |
Correct |
245 ms |
26864 KB |
Output is correct |
52 |
Correct |
315 ms |
26864 KB |
Output is correct |
53 |
Correct |
434 ms |
26864 KB |
Output is correct |
54 |
Correct |
341 ms |
26864 KB |
Output is correct |
55 |
Correct |
19 ms |
26864 KB |
Output is correct |
56 |
Correct |
21 ms |
26864 KB |
Output is correct |
57 |
Correct |
144 ms |
26864 KB |
Output is correct |
58 |
Correct |
70 ms |
26864 KB |
Output is correct |
59 |
Correct |
183 ms |
26864 KB |
Output is correct |
60 |
Correct |
740 ms |
28908 KB |
Output is correct |
61 |
Correct |
260 ms |
28908 KB |
Output is correct |
62 |
Correct |
313 ms |
28908 KB |
Output is correct |
63 |
Correct |
393 ms |
28908 KB |
Output is correct |
64 |
Correct |
693 ms |
28908 KB |
Output is correct |
65 |
Correct |
370 ms |
28908 KB |
Output is correct |
66 |
Correct |
1030 ms |
29296 KB |
Output is correct |
67 |
Correct |
225 ms |
29296 KB |
Output is correct |
68 |
Correct |
525 ms |
29296 KB |
Output is correct |
69 |
Correct |
573 ms |
29296 KB |
Output is correct |
70 |
Correct |
484 ms |
29296 KB |
Output is correct |