#include "race.h"
#include <bits/stdc++.h>
#define ad push_back
#define MP make_pair
#define fr first
#define sc second
using namespace std;
const int N = 1e6 + 20;
const int mod = 1e9 + 7;
int n, k;
vector<pair<int, int> > g[N];
int sz[N], col[N], ans = mod, cl[N];
queue <int> q;
void dfs0(int v, int par)
{
sz[v] = 1;
for(auto p : g[v])
{
if(col[p.fr] || p.fr == par) continue;
dfs0(p.fr, v);
sz[v] += sz[p.fr];
}
}
int dfs1(int v, int par, int s)
{
for(auto p : g[v])
{
if(col[p.fr] || p.fr == par) continue;
if(sz[p.fr] > s / 2) return dfs1(p.fr, v, s);
}
return v;
}
void dfs(int v, int par, int l, int d, int bl)
{
if(bl == 0)
{
cl[d] = mod;
if(d <= k) cl[k - d] = mod;
}
if(bl == 1) if(d <= k) ans = min(ans, l + cl[k - d]);
if(bl == 2) cl[d] = min(cl[d], l);
for(auto p : g[v])
{
if(p.fr == par || col[p.fr]) continue;
dfs(p.fr, v, l + 1, min(k + 1, d + p.sc), bl);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k = K;
for (int i = 0; i < n - 1; i++)
{
g[H[i][0]].ad(MP(H[i][1], L[i]));
g[H[i][1]].ad(MP(H[i][0], L[i]));
}
q.push(1);
while(!q.empty())
{
int c = q.front();
dfs0(c, c);
c = dfs1(c, c, sz[c]);
q.pop();
col[c] = 1;
for(auto p : g[c]) if(!col[p.fr]) q.push(p.fr);
dfs(c, c, 0, 0, 0);
cl[0] = 0;
for(auto p : g[c])
{
if(!col[p.fr])
{
dfs(p.fr, c, 1, p.sc, 1);
dfs(p.fr, c, 1, p.sc, 2);
}
}
}
if(ans == mod) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23712 KB |
Output is correct |
3 |
Correct |
14 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
20 ms |
23820 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23800 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
16 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23712 KB |
Output is correct |
3 |
Correct |
14 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
20 ms |
23820 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23800 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
16 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23756 KB |
Output is correct |
19 |
Correct |
14 ms |
23756 KB |
Output is correct |
20 |
Correct |
17 ms |
23792 KB |
Output is correct |
21 |
Correct |
16 ms |
23884 KB |
Output is correct |
22 |
Correct |
16 ms |
27376 KB |
Output is correct |
23 |
Correct |
16 ms |
27252 KB |
Output is correct |
24 |
Correct |
18 ms |
27132 KB |
Output is correct |
25 |
Correct |
16 ms |
27000 KB |
Output is correct |
26 |
Correct |
17 ms |
25212 KB |
Output is correct |
27 |
Correct |
16 ms |
23804 KB |
Output is correct |
28 |
Correct |
15 ms |
24648 KB |
Output is correct |
29 |
Correct |
15 ms |
25084 KB |
Output is correct |
30 |
Correct |
18 ms |
25292 KB |
Output is correct |
31 |
Correct |
19 ms |
26356 KB |
Output is correct |
32 |
Correct |
17 ms |
26572 KB |
Output is correct |
33 |
Correct |
16 ms |
26872 KB |
Output is correct |
34 |
Correct |
19 ms |
26188 KB |
Output is correct |
35 |
Correct |
16 ms |
26956 KB |
Output is correct |
36 |
Correct |
17 ms |
27464 KB |
Output is correct |
37 |
Correct |
21 ms |
26872 KB |
Output is correct |
38 |
Correct |
17 ms |
25804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23712 KB |
Output is correct |
3 |
Correct |
14 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
20 ms |
23820 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23800 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
16 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23756 KB |
Output is correct |
19 |
Correct |
287 ms |
30820 KB |
Output is correct |
20 |
Correct |
293 ms |
30764 KB |
Output is correct |
21 |
Correct |
273 ms |
30848 KB |
Output is correct |
22 |
Correct |
242 ms |
30936 KB |
Output is correct |
23 |
Correct |
167 ms |
31252 KB |
Output is correct |
24 |
Correct |
110 ms |
31256 KB |
Output is correct |
25 |
Correct |
240 ms |
37408 KB |
Output is correct |
26 |
Correct |
139 ms |
38184 KB |
Output is correct |
27 |
Correct |
348 ms |
38296 KB |
Output is correct |
28 |
Correct |
735 ms |
51864 KB |
Output is correct |
29 |
Correct |
848 ms |
46684 KB |
Output is correct |
30 |
Correct |
338 ms |
38392 KB |
Output is correct |
31 |
Correct |
326 ms |
38476 KB |
Output is correct |
32 |
Correct |
509 ms |
38500 KB |
Output is correct |
33 |
Correct |
533 ms |
37140 KB |
Output is correct |
34 |
Correct |
580 ms |
41936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23712 KB |
Output is correct |
3 |
Correct |
14 ms |
23800 KB |
Output is correct |
4 |
Correct |
15 ms |
23756 KB |
Output is correct |
5 |
Correct |
14 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23792 KB |
Output is correct |
7 |
Correct |
16 ms |
23756 KB |
Output is correct |
8 |
Correct |
14 ms |
23784 KB |
Output is correct |
9 |
Correct |
20 ms |
23820 KB |
Output is correct |
10 |
Correct |
14 ms |
23756 KB |
Output is correct |
11 |
Correct |
16 ms |
23800 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
14 ms |
23824 KB |
Output is correct |
14 |
Correct |
14 ms |
23788 KB |
Output is correct |
15 |
Correct |
14 ms |
23788 KB |
Output is correct |
16 |
Correct |
14 ms |
23756 KB |
Output is correct |
17 |
Correct |
16 ms |
23760 KB |
Output is correct |
18 |
Correct |
14 ms |
23756 KB |
Output is correct |
19 |
Correct |
14 ms |
23756 KB |
Output is correct |
20 |
Correct |
17 ms |
23792 KB |
Output is correct |
21 |
Correct |
16 ms |
23884 KB |
Output is correct |
22 |
Correct |
16 ms |
27376 KB |
Output is correct |
23 |
Correct |
16 ms |
27252 KB |
Output is correct |
24 |
Correct |
18 ms |
27132 KB |
Output is correct |
25 |
Correct |
16 ms |
27000 KB |
Output is correct |
26 |
Correct |
17 ms |
25212 KB |
Output is correct |
27 |
Correct |
16 ms |
23804 KB |
Output is correct |
28 |
Correct |
15 ms |
24648 KB |
Output is correct |
29 |
Correct |
15 ms |
25084 KB |
Output is correct |
30 |
Correct |
18 ms |
25292 KB |
Output is correct |
31 |
Correct |
19 ms |
26356 KB |
Output is correct |
32 |
Correct |
17 ms |
26572 KB |
Output is correct |
33 |
Correct |
16 ms |
26872 KB |
Output is correct |
34 |
Correct |
19 ms |
26188 KB |
Output is correct |
35 |
Correct |
16 ms |
26956 KB |
Output is correct |
36 |
Correct |
17 ms |
27464 KB |
Output is correct |
37 |
Correct |
21 ms |
26872 KB |
Output is correct |
38 |
Correct |
17 ms |
25804 KB |
Output is correct |
39 |
Correct |
287 ms |
30820 KB |
Output is correct |
40 |
Correct |
293 ms |
30764 KB |
Output is correct |
41 |
Correct |
273 ms |
30848 KB |
Output is correct |
42 |
Correct |
242 ms |
30936 KB |
Output is correct |
43 |
Correct |
167 ms |
31252 KB |
Output is correct |
44 |
Correct |
110 ms |
31256 KB |
Output is correct |
45 |
Correct |
240 ms |
37408 KB |
Output is correct |
46 |
Correct |
139 ms |
38184 KB |
Output is correct |
47 |
Correct |
348 ms |
38296 KB |
Output is correct |
48 |
Correct |
735 ms |
51864 KB |
Output is correct |
49 |
Correct |
848 ms |
46684 KB |
Output is correct |
50 |
Correct |
338 ms |
38392 KB |
Output is correct |
51 |
Correct |
326 ms |
38476 KB |
Output is correct |
52 |
Correct |
509 ms |
38500 KB |
Output is correct |
53 |
Correct |
533 ms |
37140 KB |
Output is correct |
54 |
Correct |
580 ms |
41936 KB |
Output is correct |
55 |
Correct |
25 ms |
24432 KB |
Output is correct |
56 |
Correct |
26 ms |
24440 KB |
Output is correct |
57 |
Correct |
140 ms |
31180 KB |
Output is correct |
58 |
Correct |
54 ms |
31032 KB |
Output is correct |
59 |
Correct |
140 ms |
38808 KB |
Output is correct |
60 |
Correct |
763 ms |
49160 KB |
Output is correct |
61 |
Correct |
315 ms |
38556 KB |
Output is correct |
62 |
Correct |
380 ms |
38576 KB |
Output is correct |
63 |
Correct |
563 ms |
38600 KB |
Output is correct |
64 |
Correct |
1039 ms |
40656 KB |
Output is correct |
65 |
Correct |
777 ms |
43080 KB |
Output is correct |
66 |
Correct |
1016 ms |
54552 KB |
Output is correct |
67 |
Correct |
177 ms |
39704 KB |
Output is correct |
68 |
Correct |
483 ms |
42100 KB |
Output is correct |
69 |
Correct |
514 ms |
42556 KB |
Output is correct |
70 |
Correct |
455 ms |
41588 KB |
Output is correct |