#include "race.h"
#include <bits/stdc++.h>
#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif
using namespace std;
vector<pair<int,int>> adj[200000];
int M, D[1000001], P[1000001], W[200000], dist[200000], parent[200000];
void reset(int c)
{
W[c]=0;
for(auto[n,w]: adj[c]) if(W[n]>0) reset(n);
}
int dfs(int c)
{
W[c]=1; dist[c]=0x7fffffff;
for(auto[n,w]: adj[c]) if(W[n]==0) W[c]+=dfs(n);
return W[c];
}
int centroid(int c, int v)
{
for(auto[n,w]: adj[c]) if(W[n]<W[c] && 2*W[n]>v) return centroid(n,v);
return c;
}
int solve(int C)
{
int ret=0x7fffffff;
queue<pair<int,int>> Q;
reset(C);
dist[C=centroid(C,dfs(C))]=0;
D[0]=0;
P[0]=C;
parent[C]=C;
for(auto[n,w]: adj[C]) if(W[n]>0) {
if((dist[n]=w)>M) continue;
parent[n]=C;
Q.emplace(1,n);
while(!Q.empty()) {
auto[v,c]=Q.front();
Q.pop();
if(P[M-dist[c]]==C) ret=min(ret,v+D[M-dist[c]]);
for(auto[n,w]: adj[c]) if(dist[n]>dist[c]+w && dist[c]+w<=M) {
dist[n]=dist[c]+w;
parent[n]=c;
Q.emplace(v+1,n);
}
}
Q.emplace(1,n);
while(!Q.empty()) {
auto[v,c]=Q.front();
Q.pop();
if(P[dist[c]]!=C) {
P[dist[c]]=C;
D[dist[c]]=v;
}
else D[dist[c]]=min(D[dist[c]],v);
for(auto[n,w]: adj[c]) if(dist[n]<0x7fffffff && parent[n]==c) Q.emplace(v+1,n);
}
}
W[C]=-1;
for(auto[n,w]: adj[C]) if(W[n]>0) ret=min(ret,solve(n));
return ret;
}
int best_path(int N, int K, int H[][2], int L[])
{
for(int i=0;i<N-1;i++) {
adj[H[i][0]].emplace_back(H[i][1],L[i]);
adj[H[i][1]].emplace_back(H[i][0],L[i]);
}
memset(P,-1,sizeof(P));
M=K;
N=solve(0);
return N==0x7fffffff ? -1:N;
}
Compilation message
race.cpp: In function 'void reset(int)':
race.cpp:20:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) if(W[n]>0) reset(n);
^
race.cpp: In function 'int dfs(int)':
race.cpp:26:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) if(W[n]==0) W[c]+=dfs(n);
^
race.cpp: In function 'int centroid(int, int)':
race.cpp:32:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) if(W[n]<W[c] && 2*W[n]>v) return centroid(n,v);
^
race.cpp: In function 'int solve(int)':
race.cpp:68:16: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[c]) if(dist[n]<0x7fffffff && parent[n]==c) Q.emplace(v+1,n);
^
race.cpp:72:14: warning: unused variable 'w' [-Wunused-variable]
for(auto[n,w]: adj[C]) if(W[n]>0) ret=min(ret,solve(n));
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8956 KB |
Output is correct |
3 |
Correct |
10 ms |
8956 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8900 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
9 ms |
8952 KB |
Output is correct |
14 |
Correct |
10 ms |
8952 KB |
Output is correct |
15 |
Correct |
10 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8924 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8956 KB |
Output is correct |
3 |
Correct |
10 ms |
8956 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8900 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
9 ms |
8952 KB |
Output is correct |
14 |
Correct |
10 ms |
8952 KB |
Output is correct |
15 |
Correct |
10 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8924 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8952 KB |
Output is correct |
19 |
Correct |
10 ms |
8952 KB |
Output is correct |
20 |
Correct |
10 ms |
8952 KB |
Output is correct |
21 |
Correct |
11 ms |
9080 KB |
Output is correct |
22 |
Correct |
12 ms |
11512 KB |
Output is correct |
23 |
Correct |
12 ms |
11128 KB |
Output is correct |
24 |
Correct |
13 ms |
11896 KB |
Output is correct |
25 |
Correct |
12 ms |
10744 KB |
Output is correct |
26 |
Correct |
12 ms |
10332 KB |
Output is correct |
27 |
Correct |
10 ms |
9080 KB |
Output is correct |
28 |
Correct |
11 ms |
9852 KB |
Output is correct |
29 |
Correct |
12 ms |
10360 KB |
Output is correct |
30 |
Correct |
12 ms |
10492 KB |
Output is correct |
31 |
Correct |
12 ms |
10872 KB |
Output is correct |
32 |
Correct |
13 ms |
10876 KB |
Output is correct |
33 |
Correct |
13 ms |
11640 KB |
Output is correct |
34 |
Correct |
13 ms |
11272 KB |
Output is correct |
35 |
Correct |
14 ms |
12024 KB |
Output is correct |
36 |
Correct |
14 ms |
12280 KB |
Output is correct |
37 |
Correct |
13 ms |
10872 KB |
Output is correct |
38 |
Correct |
12 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8956 KB |
Output is correct |
3 |
Correct |
10 ms |
8956 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8900 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
9 ms |
8952 KB |
Output is correct |
14 |
Correct |
10 ms |
8952 KB |
Output is correct |
15 |
Correct |
10 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8924 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8952 KB |
Output is correct |
19 |
Correct |
248 ms |
16192 KB |
Output is correct |
20 |
Correct |
256 ms |
16120 KB |
Output is correct |
21 |
Correct |
255 ms |
16120 KB |
Output is correct |
22 |
Correct |
233 ms |
16284 KB |
Output is correct |
23 |
Correct |
161 ms |
16376 KB |
Output is correct |
24 |
Correct |
118 ms |
16248 KB |
Output is correct |
25 |
Correct |
277 ms |
17292 KB |
Output is correct |
26 |
Correct |
169 ms |
18764 KB |
Output is correct |
27 |
Correct |
267 ms |
22300 KB |
Output is correct |
28 |
Correct |
526 ms |
27336 KB |
Output is correct |
29 |
Correct |
601 ms |
26892 KB |
Output is correct |
30 |
Correct |
257 ms |
22240 KB |
Output is correct |
31 |
Correct |
273 ms |
22484 KB |
Output is correct |
32 |
Correct |
355 ms |
22396 KB |
Output is correct |
33 |
Correct |
480 ms |
21240 KB |
Output is correct |
34 |
Correct |
423 ms |
21240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8956 KB |
Output is correct |
3 |
Correct |
10 ms |
8956 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8900 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
9080 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
9 ms |
8952 KB |
Output is correct |
14 |
Correct |
10 ms |
8952 KB |
Output is correct |
15 |
Correct |
10 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8924 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8952 KB |
Output is correct |
19 |
Correct |
10 ms |
8952 KB |
Output is correct |
20 |
Correct |
10 ms |
8952 KB |
Output is correct |
21 |
Correct |
11 ms |
9080 KB |
Output is correct |
22 |
Correct |
12 ms |
11512 KB |
Output is correct |
23 |
Correct |
12 ms |
11128 KB |
Output is correct |
24 |
Correct |
13 ms |
11896 KB |
Output is correct |
25 |
Correct |
12 ms |
10744 KB |
Output is correct |
26 |
Correct |
12 ms |
10332 KB |
Output is correct |
27 |
Correct |
10 ms |
9080 KB |
Output is correct |
28 |
Correct |
11 ms |
9852 KB |
Output is correct |
29 |
Correct |
12 ms |
10360 KB |
Output is correct |
30 |
Correct |
12 ms |
10492 KB |
Output is correct |
31 |
Correct |
12 ms |
10872 KB |
Output is correct |
32 |
Correct |
13 ms |
10876 KB |
Output is correct |
33 |
Correct |
13 ms |
11640 KB |
Output is correct |
34 |
Correct |
13 ms |
11272 KB |
Output is correct |
35 |
Correct |
14 ms |
12024 KB |
Output is correct |
36 |
Correct |
14 ms |
12280 KB |
Output is correct |
37 |
Correct |
13 ms |
10872 KB |
Output is correct |
38 |
Correct |
12 ms |
10232 KB |
Output is correct |
39 |
Correct |
248 ms |
16192 KB |
Output is correct |
40 |
Correct |
256 ms |
16120 KB |
Output is correct |
41 |
Correct |
255 ms |
16120 KB |
Output is correct |
42 |
Correct |
233 ms |
16284 KB |
Output is correct |
43 |
Correct |
161 ms |
16376 KB |
Output is correct |
44 |
Correct |
118 ms |
16248 KB |
Output is correct |
45 |
Correct |
277 ms |
17292 KB |
Output is correct |
46 |
Correct |
169 ms |
18764 KB |
Output is correct |
47 |
Correct |
267 ms |
22300 KB |
Output is correct |
48 |
Correct |
526 ms |
27336 KB |
Output is correct |
49 |
Correct |
601 ms |
26892 KB |
Output is correct |
50 |
Correct |
257 ms |
22240 KB |
Output is correct |
51 |
Correct |
273 ms |
22484 KB |
Output is correct |
52 |
Correct |
355 ms |
22396 KB |
Output is correct |
53 |
Correct |
480 ms |
21240 KB |
Output is correct |
54 |
Correct |
423 ms |
21240 KB |
Output is correct |
55 |
Correct |
26 ms |
9772 KB |
Output is correct |
56 |
Correct |
25 ms |
9720 KB |
Output is correct |
57 |
Correct |
147 ms |
16564 KB |
Output is correct |
58 |
Correct |
69 ms |
16332 KB |
Output is correct |
59 |
Correct |
177 ms |
19164 KB |
Output is correct |
60 |
Correct |
998 ms |
29416 KB |
Output is correct |
61 |
Correct |
294 ms |
22392 KB |
Output is correct |
62 |
Correct |
313 ms |
22264 KB |
Output is correct |
63 |
Correct |
430 ms |
22392 KB |
Output is correct |
64 |
Correct |
787 ms |
23416 KB |
Output is correct |
65 |
Correct |
377 ms |
22268 KB |
Output is correct |
66 |
Correct |
870 ms |
29764 KB |
Output is correct |
67 |
Correct |
209 ms |
22600 KB |
Output is correct |
68 |
Correct |
457 ms |
25208 KB |
Output is correct |
69 |
Correct |
452 ms |
25336 KB |
Output is correct |
70 |
Correct |
426 ms |
24668 KB |
Output is correct |