#include "race.h"
#include <algorithm>
#include <vector>
using namespace std;
#define MAX_N 200001
#define MAX_K 1000001
const int non = 0x3fffffff;
vector<int> G[MAX_N];
int ex[MAX_N],ey[MAX_N],l[MAX_N],K;
bool forb[MAX_N];
int ts,chk[MAX_N],cut[MAX_N],Q[MAX_N],depth[MAX_N],len[MAX_N],sz[MAX_N],line[MAX_K];
int gety(int x, int i){
return x == ex[i] ? ey[i] : ex[i];
}
int dfs(int x)
{
int head, tail, res = non;
head = tail = -1;
Q[++head] = x; chk[x] = ts; depth[x] = 0;
while (tail < head){
int p = Q[++tail];
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
int q = gety(p,G[p][i]);
if (chk[q] != ts){
Q[++head] = q;
chk[q] = ts;
depth[q] = depth[p] + 1;
}
}
}
ts++;
int all = head + 1;
if (all > 1){
int c = x;
for (int j=head;j>=0;j--){
int p = Q[j];
sz[p] = 1;
bool good = true;
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
int q = gety(p,G[p][i]);
if (depth[p] < depth[q]){
sz[p] += sz[q];
if (sz[q] > all/2) good = false;
}
}
if (all - sz[p] > all/2) good = false;
if (good){
c = p;
break;
}
}
int child = 0;
head = tail = 0; x = c;
Q[0] = x; chk[x] = ts; depth[x] = len[x] = 0; line[0] = 0;
for (int i=0;i<G[x].size();i++) if (!forb[G[x][i]]){
int y = gety(x,G[x][i]);
cut[child++] = y;
chk[y] = ts;
depth[y] = 1;
len[y] = l[G[x][i]];
}
for (int s=0;s<child;s++){
int last = head;
if (len[cut[s]] <= K) Q[++head] = cut[s];
while (tail < head){
int p = Q[++tail];
res = min(res,line[K-len[p]]+depth[p]);
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
int q = gety(p,G[p][i]);
if (chk[q] != ts){
chk[q] = ts;
depth[q] = depth[p] + 1;
len[q] = len[p] + l[G[p][i]];
if (len[q] <= K) Q[++head] = q;
}
}
}
for (int j=last+1;j<=head;j++){
int p = Q[j];
line[len[p]] = min(line[len[p]],depth[p]);
}
}
for (int j=0;j<=head;j++){
line[len[Q[j]]] = non;
}
ts++;
for (int i=0;i<G[x].size();i++) if (!forb[G[x][i]]){
forb[G[x][i]] = 1;
int small = dfs(gety(c,G[x][i]));
res = min(res,small);
}
}
return res;
}
int best_path(int N, int K_, int H[][2], int L[])
{
K = K_;
for (int i=0;i<N-1;i++){
int x = H[i][0], y = H[i][1];
G[x].push_back(i);
G[y].push_back(i);
ex[i] = x; ey[i] = y; l[i] = L[i];
}
for (int i=0;i<N;i++) chk[i] = -1;
for (int i=0;i<=K;i++) line[i] = non;
int ans = dfs(0);
if (ans == non) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'int dfs(int)':
race.cpp:26:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
~^~~~~~~~~~~~
race.cpp:45:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
~^~~~~~~~~~~~
race.cpp:64:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[x].size();i++) if (!forb[G[x][i]]){
~^~~~~~~~~~~~
race.cpp:78:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[p].size();i++) if (!forb[G[p][i]]){
~^~~~~~~~~~~~
race.cpp:100:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<G[x].size();i++) if (!forb[G[x][i]]){
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5300 KB |
Output is correct |
4 |
Correct |
6 ms |
5300 KB |
Output is correct |
5 |
Correct |
7 ms |
5300 KB |
Output is correct |
6 |
Correct |
5 ms |
5300 KB |
Output is correct |
7 |
Correct |
6 ms |
5300 KB |
Output is correct |
8 |
Correct |
6 ms |
5300 KB |
Output is correct |
9 |
Correct |
6 ms |
5388 KB |
Output is correct |
10 |
Correct |
5 ms |
5388 KB |
Output is correct |
11 |
Correct |
6 ms |
5388 KB |
Output is correct |
12 |
Correct |
6 ms |
5388 KB |
Output is correct |
13 |
Correct |
6 ms |
5388 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5300 KB |
Output is correct |
4 |
Correct |
6 ms |
5300 KB |
Output is correct |
5 |
Correct |
7 ms |
5300 KB |
Output is correct |
6 |
Correct |
5 ms |
5300 KB |
Output is correct |
7 |
Correct |
6 ms |
5300 KB |
Output is correct |
8 |
Correct |
6 ms |
5300 KB |
Output is correct |
9 |
Correct |
6 ms |
5388 KB |
Output is correct |
10 |
Correct |
5 ms |
5388 KB |
Output is correct |
11 |
Correct |
6 ms |
5388 KB |
Output is correct |
12 |
Correct |
6 ms |
5388 KB |
Output is correct |
13 |
Correct |
6 ms |
5388 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
19 |
Correct |
5 ms |
5484 KB |
Output is correct |
20 |
Correct |
6 ms |
5484 KB |
Output is correct |
21 |
Correct |
6 ms |
5484 KB |
Output is correct |
22 |
Correct |
8 ms |
8960 KB |
Output is correct |
23 |
Correct |
9 ms |
8960 KB |
Output is correct |
24 |
Correct |
8 ms |
8960 KB |
Output is correct |
25 |
Correct |
9 ms |
8960 KB |
Output is correct |
26 |
Correct |
7 ms |
8960 KB |
Output is correct |
27 |
Correct |
8 ms |
8960 KB |
Output is correct |
28 |
Correct |
7 ms |
8960 KB |
Output is correct |
29 |
Correct |
8 ms |
8960 KB |
Output is correct |
30 |
Correct |
7 ms |
8960 KB |
Output is correct |
31 |
Correct |
8 ms |
8960 KB |
Output is correct |
32 |
Correct |
8 ms |
8960 KB |
Output is correct |
33 |
Correct |
8 ms |
8960 KB |
Output is correct |
34 |
Correct |
7 ms |
8960 KB |
Output is correct |
35 |
Correct |
8 ms |
8960 KB |
Output is correct |
36 |
Correct |
9 ms |
9084 KB |
Output is correct |
37 |
Correct |
9 ms |
9084 KB |
Output is correct |
38 |
Correct |
9 ms |
9084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5300 KB |
Output is correct |
4 |
Correct |
6 ms |
5300 KB |
Output is correct |
5 |
Correct |
7 ms |
5300 KB |
Output is correct |
6 |
Correct |
5 ms |
5300 KB |
Output is correct |
7 |
Correct |
6 ms |
5300 KB |
Output is correct |
8 |
Correct |
6 ms |
5300 KB |
Output is correct |
9 |
Correct |
6 ms |
5388 KB |
Output is correct |
10 |
Correct |
5 ms |
5388 KB |
Output is correct |
11 |
Correct |
6 ms |
5388 KB |
Output is correct |
12 |
Correct |
6 ms |
5388 KB |
Output is correct |
13 |
Correct |
6 ms |
5388 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
19 |
Correct |
217 ms |
12956 KB |
Output is correct |
20 |
Correct |
220 ms |
13052 KB |
Output is correct |
21 |
Correct |
224 ms |
13052 KB |
Output is correct |
22 |
Correct |
233 ms |
13052 KB |
Output is correct |
23 |
Correct |
146 ms |
13052 KB |
Output is correct |
24 |
Correct |
92 ms |
13064 KB |
Output is correct |
25 |
Correct |
164 ms |
13064 KB |
Output is correct |
26 |
Correct |
92 ms |
13064 KB |
Output is correct |
27 |
Correct |
250 ms |
20988 KB |
Output is correct |
28 |
Correct |
375 ms |
20988 KB |
Output is correct |
29 |
Correct |
461 ms |
20988 KB |
Output is correct |
30 |
Correct |
282 ms |
21048 KB |
Output is correct |
31 |
Correct |
291 ms |
21048 KB |
Output is correct |
32 |
Correct |
325 ms |
21048 KB |
Output is correct |
33 |
Correct |
326 ms |
21048 KB |
Output is correct |
34 |
Correct |
454 ms |
21048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5300 KB |
Output is correct |
4 |
Correct |
6 ms |
5300 KB |
Output is correct |
5 |
Correct |
7 ms |
5300 KB |
Output is correct |
6 |
Correct |
5 ms |
5300 KB |
Output is correct |
7 |
Correct |
6 ms |
5300 KB |
Output is correct |
8 |
Correct |
6 ms |
5300 KB |
Output is correct |
9 |
Correct |
6 ms |
5388 KB |
Output is correct |
10 |
Correct |
5 ms |
5388 KB |
Output is correct |
11 |
Correct |
6 ms |
5388 KB |
Output is correct |
12 |
Correct |
6 ms |
5388 KB |
Output is correct |
13 |
Correct |
6 ms |
5388 KB |
Output is correct |
14 |
Correct |
5 ms |
5484 KB |
Output is correct |
15 |
Correct |
5 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
5 ms |
5484 KB |
Output is correct |
18 |
Correct |
5 ms |
5484 KB |
Output is correct |
19 |
Correct |
5 ms |
5484 KB |
Output is correct |
20 |
Correct |
6 ms |
5484 KB |
Output is correct |
21 |
Correct |
6 ms |
5484 KB |
Output is correct |
22 |
Correct |
8 ms |
8960 KB |
Output is correct |
23 |
Correct |
9 ms |
8960 KB |
Output is correct |
24 |
Correct |
8 ms |
8960 KB |
Output is correct |
25 |
Correct |
9 ms |
8960 KB |
Output is correct |
26 |
Correct |
7 ms |
8960 KB |
Output is correct |
27 |
Correct |
8 ms |
8960 KB |
Output is correct |
28 |
Correct |
7 ms |
8960 KB |
Output is correct |
29 |
Correct |
8 ms |
8960 KB |
Output is correct |
30 |
Correct |
7 ms |
8960 KB |
Output is correct |
31 |
Correct |
8 ms |
8960 KB |
Output is correct |
32 |
Correct |
8 ms |
8960 KB |
Output is correct |
33 |
Correct |
8 ms |
8960 KB |
Output is correct |
34 |
Correct |
7 ms |
8960 KB |
Output is correct |
35 |
Correct |
8 ms |
8960 KB |
Output is correct |
36 |
Correct |
9 ms |
9084 KB |
Output is correct |
37 |
Correct |
9 ms |
9084 KB |
Output is correct |
38 |
Correct |
9 ms |
9084 KB |
Output is correct |
39 |
Correct |
217 ms |
12956 KB |
Output is correct |
40 |
Correct |
220 ms |
13052 KB |
Output is correct |
41 |
Correct |
224 ms |
13052 KB |
Output is correct |
42 |
Correct |
233 ms |
13052 KB |
Output is correct |
43 |
Correct |
146 ms |
13052 KB |
Output is correct |
44 |
Correct |
92 ms |
13064 KB |
Output is correct |
45 |
Correct |
164 ms |
13064 KB |
Output is correct |
46 |
Correct |
92 ms |
13064 KB |
Output is correct |
47 |
Correct |
250 ms |
20988 KB |
Output is correct |
48 |
Correct |
375 ms |
20988 KB |
Output is correct |
49 |
Correct |
461 ms |
20988 KB |
Output is correct |
50 |
Correct |
282 ms |
21048 KB |
Output is correct |
51 |
Correct |
291 ms |
21048 KB |
Output is correct |
52 |
Correct |
325 ms |
21048 KB |
Output is correct |
53 |
Correct |
326 ms |
21048 KB |
Output is correct |
54 |
Correct |
454 ms |
21048 KB |
Output is correct |
55 |
Correct |
17 ms |
21048 KB |
Output is correct |
56 |
Correct |
18 ms |
21048 KB |
Output is correct |
57 |
Correct |
100 ms |
21048 KB |
Output is correct |
58 |
Correct |
57 ms |
21048 KB |
Output is correct |
59 |
Correct |
100 ms |
21048 KB |
Output is correct |
60 |
Correct |
652 ms |
24284 KB |
Output is correct |
61 |
Correct |
342 ms |
24284 KB |
Output is correct |
62 |
Correct |
298 ms |
24828 KB |
Output is correct |
63 |
Correct |
419 ms |
24856 KB |
Output is correct |
64 |
Correct |
801 ms |
24856 KB |
Output is correct |
65 |
Correct |
456 ms |
24856 KB |
Output is correct |
66 |
Correct |
633 ms |
24856 KB |
Output is correct |
67 |
Correct |
242 ms |
25836 KB |
Output is correct |
68 |
Correct |
543 ms |
25836 KB |
Output is correct |
69 |
Correct |
510 ms |
25836 KB |
Output is correct |
70 |
Correct |
480 ms |
25836 KB |
Output is correct |