#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
set < int > G[N];
int sz[N];
//int a[N * 10];
map < int , int > a;
int H[N][2] , L[N];
int K;
void getsize(int node , int par)
{
sz[node] = 1;
for(auto e : G[node])
{
int to = node ^ H[e][0] ^ H[e][1];
int c = L[e];
if(to != par)
{
getsize(to , node);
sz[node] += sz[to];
}
}
}
int find(int node , int par , int n)
{
for(auto e : G[node])
{
int to = node ^ H[e][0] ^ H[e][1];
int c = L[e];
if(to != par)
{
if(sz[to] > n / 2)
return find(to , node , n);
}
}
return node;
}
int ans = INT_MAX;
void get(int node , int par , int len , int h)
{
if(len <= K && a[K - len])
ans = min(ans , a[K - len] + h - 1);
if(len == K)
ans = min(ans , h - 1);
for(auto e : G[node])
{
int to = node ^ H[e][0] ^ H[e][1];
int c = L[e];
if(to != par)
get(to , node , len + c , h + 1);
}
}
void add(int node , int par , int len , int h)
{
if(len <= K)
{
if(a[len] == 0) a[len] = h - 1;
else a[len] = min(a[len] , h - 1);
}
for(auto e : G[node])
{
int to = node ^ H[e][0] ^ H[e][1];
int c = L[e];
if(to != par)
add(to , node , len + c , h + 1);
}
}
void solve(int root)
{
a.clear();
for(auto e : G[root])
{
int to = root ^ H[e][0] ^ H[e][1];
int c = L[e];
get(to , root , c , 2);
add(to , root , c , 2);
}
}
void decomp(int root)
{
getsize(root , 0);
int c = find(root , 0 , sz[root]);
solve(c);
for(auto e : G[c])
{
int to = c ^ H[e][0] ^ H[e][1];
G[to].erase(e);
decomp(to);
}
}
int best_path(int n , int k , int h[][2] , int l[])
{
K = k;
for(int i = 0 ; i < n - 1 ; i++)
{
H[i][0] = h[i][0];
H[i][1] = h[i][1];
L[i] = l[i];
}
for(int i = 0 ; i < n - 1 ; i++)
{
int x = H[i][0];
int y = H[i][1];
G[x].insert(i);
G[y].insert(i);
}
decomp(0);
if(ans == INT_MAX) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void getsize(int, int)':
race.cpp:22:17: warning: unused variable 'c' [-Wunused-variable]
22 | int c = L[e];
| ^
race.cpp: In function 'int find(int, int, int)':
race.cpp:37:17: warning: unused variable 'c' [-Wunused-variable]
37 | int c = L[e];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9740 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9660 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9740 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9660 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9804 KB |
Output is correct |
23 |
Correct |
6 ms |
9812 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9812 KB |
Output is correct |
27 |
Correct |
7 ms |
9812 KB |
Output is correct |
28 |
Correct |
8 ms |
9872 KB |
Output is correct |
29 |
Correct |
7 ms |
9812 KB |
Output is correct |
30 |
Correct |
7 ms |
9812 KB |
Output is correct |
31 |
Correct |
7 ms |
9940 KB |
Output is correct |
32 |
Correct |
7 ms |
9816 KB |
Output is correct |
33 |
Correct |
8 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9812 KB |
Output is correct |
35 |
Correct |
8 ms |
9812 KB |
Output is correct |
36 |
Correct |
7 ms |
9812 KB |
Output is correct |
37 |
Correct |
8 ms |
9824 KB |
Output is correct |
38 |
Correct |
7 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9740 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9660 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
387 ms |
21836 KB |
Output is correct |
20 |
Correct |
377 ms |
21832 KB |
Output is correct |
21 |
Correct |
401 ms |
21860 KB |
Output is correct |
22 |
Correct |
387 ms |
21880 KB |
Output is correct |
23 |
Correct |
204 ms |
21824 KB |
Output is correct |
24 |
Correct |
128 ms |
21844 KB |
Output is correct |
25 |
Correct |
284 ms |
27316 KB |
Output is correct |
26 |
Correct |
146 ms |
29644 KB |
Output is correct |
27 |
Correct |
442 ms |
33824 KB |
Output is correct |
28 |
Correct |
679 ms |
49600 KB |
Output is correct |
29 |
Correct |
664 ms |
48400 KB |
Output is correct |
30 |
Correct |
437 ms |
33884 KB |
Output is correct |
31 |
Correct |
428 ms |
33848 KB |
Output is correct |
32 |
Correct |
481 ms |
33864 KB |
Output is correct |
33 |
Correct |
597 ms |
34084 KB |
Output is correct |
34 |
Correct |
437 ms |
33992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9740 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9660 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
5 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9812 KB |
Output is correct |
22 |
Correct |
6 ms |
9804 KB |
Output is correct |
23 |
Correct |
6 ms |
9812 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
7 ms |
9940 KB |
Output is correct |
26 |
Correct |
7 ms |
9812 KB |
Output is correct |
27 |
Correct |
7 ms |
9812 KB |
Output is correct |
28 |
Correct |
8 ms |
9872 KB |
Output is correct |
29 |
Correct |
7 ms |
9812 KB |
Output is correct |
30 |
Correct |
7 ms |
9812 KB |
Output is correct |
31 |
Correct |
7 ms |
9940 KB |
Output is correct |
32 |
Correct |
7 ms |
9816 KB |
Output is correct |
33 |
Correct |
8 ms |
9940 KB |
Output is correct |
34 |
Correct |
7 ms |
9812 KB |
Output is correct |
35 |
Correct |
8 ms |
9812 KB |
Output is correct |
36 |
Correct |
7 ms |
9812 KB |
Output is correct |
37 |
Correct |
8 ms |
9824 KB |
Output is correct |
38 |
Correct |
7 ms |
9812 KB |
Output is correct |
39 |
Correct |
387 ms |
21836 KB |
Output is correct |
40 |
Correct |
377 ms |
21832 KB |
Output is correct |
41 |
Correct |
401 ms |
21860 KB |
Output is correct |
42 |
Correct |
387 ms |
21880 KB |
Output is correct |
43 |
Correct |
204 ms |
21824 KB |
Output is correct |
44 |
Correct |
128 ms |
21844 KB |
Output is correct |
45 |
Correct |
284 ms |
27316 KB |
Output is correct |
46 |
Correct |
146 ms |
29644 KB |
Output is correct |
47 |
Correct |
442 ms |
33824 KB |
Output is correct |
48 |
Correct |
679 ms |
49600 KB |
Output is correct |
49 |
Correct |
664 ms |
48400 KB |
Output is correct |
50 |
Correct |
437 ms |
33884 KB |
Output is correct |
51 |
Correct |
428 ms |
33848 KB |
Output is correct |
52 |
Correct |
481 ms |
33864 KB |
Output is correct |
53 |
Correct |
597 ms |
34084 KB |
Output is correct |
54 |
Correct |
437 ms |
33992 KB |
Output is correct |
55 |
Correct |
32 ms |
11092 KB |
Output is correct |
56 |
Correct |
25 ms |
10836 KB |
Output is correct |
57 |
Correct |
216 ms |
21708 KB |
Output is correct |
58 |
Correct |
73 ms |
21836 KB |
Output is correct |
59 |
Correct |
467 ms |
36308 KB |
Output is correct |
60 |
Correct |
1479 ms |
65168 KB |
Output is correct |
61 |
Correct |
563 ms |
37164 KB |
Output is correct |
62 |
Correct |
589 ms |
37216 KB |
Output is correct |
63 |
Correct |
662 ms |
37180 KB |
Output is correct |
64 |
Correct |
2054 ms |
46628 KB |
Output is correct |
65 |
Correct |
651 ms |
37844 KB |
Output is correct |
66 |
Correct |
1247 ms |
49432 KB |
Output is correct |
67 |
Correct |
239 ms |
37600 KB |
Output is correct |
68 |
Correct |
856 ms |
44264 KB |
Output is correct |
69 |
Correct |
851 ms |
44328 KB |
Output is correct |
70 |
Correct |
753 ms |
42744 KB |
Output is correct |