#include <bits/stdc++.h>
#include "race.h"
#include <vector>
#define maxn 2000005
#define mod 1000000007
#define INF 1000000005
#define X first
#define Y second
using namespace std;
vector <pair <int, int>> v[maxn];
int sz[maxn];
int used[maxn];
int find_sz(int node, int parent)
{
sz[node] = 1;
for(pair <int, int> e : v[node])
{
if(e.X == parent || used[e.X] == true)
continue;
sz[node] += find_sz(e.X , node);
}
return sz[node];
}
int find_centroid(int node, int parent, int _sz)
{
for(pair <int, int> e : v[node])
{
if(e.X == parent || used[e.X] == true)
continue;
if(sz[e.X] * 2 > _sz)
return find_centroid(e.X, node, _sz);
}
return node;
}
int c = 1e8;
int ans = INF;
int help[maxn];
void dfs1(int node, int parent, int depth, int weight, int k)
{
if(weight > 0 && weight <= k)
help[weight] = c;
if(k > weight)
help[k - weight] = c;
for(pair <int, int> e : v[node])
{
if(e.X == parent || used[e.X] == true)
continue;
dfs1(e.X, node, depth + 1, weight + e.Y, k);
}
}
void dfs2(int node, int parent, int depth, int weight, int k)
{
if(weight <= k && parent != -1)
ans = min(ans, help[k - weight] + depth);
for(pair <int, int> e : v[node])
{
if(e.X == parent || used[e.X] == true)
continue;
dfs2(e.X, node, depth + 1, weight + e.Y, k);
}
}
void dfs3(int node, int parent, int depth, int weight, int k)
{
if (k >= weight && parent != -1)
help[weight] = min(help[weight], depth);
for (pair <int, int> e : v[node])
{
if (e.X == parent || used[e.X])
continue;
dfs3(e.X, node, depth + 1, weight + e.Y , k);
}
}
void decompose(int node , int k)
{
int cur_sz = find_sz(node , -1);
int centroid = find_centroid(node , -1 , cur_sz);
used[centroid] = true;
for(pair <int , int> e : v[centroid])
{
if(used[e.X] == true)
continue;
dfs1(e.X , centroid , 1 , e.Y , k);
}
for(pair <int , int> e : v[centroid])
{
if(used[e.X] == true)
continue;
dfs2(e.X, centroid , 1 , e.Y , k);
dfs3(e.X , centroid , 1 , e.Y , k);
}
for(pair <int , int> e : v[centroid])
{
if(used[e.X] == true)
continue;
decompose(e.X , k);
}
}
int best_path(int n , int k , int h[][2] , int l[])
{
for(int i = 0; i < n - 1; i++)
{
v[h[i][0]].push_back({h[i][1] , l[i]});
v[h[i][1]].push_back({h[i][0] , l[i]});
}
help[0] = 0;
decompose(0 , k);
if(ans > n)
return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
53848 KB |
Output is correct |
2 |
Correct |
13 ms |
53848 KB |
Output is correct |
3 |
Correct |
13 ms |
53996 KB |
Output is correct |
4 |
Correct |
12 ms |
53852 KB |
Output is correct |
5 |
Correct |
12 ms |
53852 KB |
Output is correct |
6 |
Correct |
12 ms |
53852 KB |
Output is correct |
7 |
Correct |
12 ms |
53852 KB |
Output is correct |
8 |
Correct |
13 ms |
54020 KB |
Output is correct |
9 |
Correct |
12 ms |
53848 KB |
Output is correct |
10 |
Correct |
12 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53852 KB |
Output is correct |
12 |
Correct |
12 ms |
53852 KB |
Output is correct |
13 |
Correct |
12 ms |
53936 KB |
Output is correct |
14 |
Correct |
12 ms |
53852 KB |
Output is correct |
15 |
Correct |
12 ms |
53852 KB |
Output is correct |
16 |
Correct |
12 ms |
53852 KB |
Output is correct |
17 |
Correct |
13 ms |
53852 KB |
Output is correct |
18 |
Correct |
12 ms |
53852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
53848 KB |
Output is correct |
2 |
Correct |
13 ms |
53848 KB |
Output is correct |
3 |
Correct |
13 ms |
53996 KB |
Output is correct |
4 |
Correct |
12 ms |
53852 KB |
Output is correct |
5 |
Correct |
12 ms |
53852 KB |
Output is correct |
6 |
Correct |
12 ms |
53852 KB |
Output is correct |
7 |
Correct |
12 ms |
53852 KB |
Output is correct |
8 |
Correct |
13 ms |
54020 KB |
Output is correct |
9 |
Correct |
12 ms |
53848 KB |
Output is correct |
10 |
Correct |
12 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53852 KB |
Output is correct |
12 |
Correct |
12 ms |
53852 KB |
Output is correct |
13 |
Correct |
12 ms |
53936 KB |
Output is correct |
14 |
Correct |
12 ms |
53852 KB |
Output is correct |
15 |
Correct |
12 ms |
53852 KB |
Output is correct |
16 |
Correct |
12 ms |
53852 KB |
Output is correct |
17 |
Correct |
13 ms |
53852 KB |
Output is correct |
18 |
Correct |
12 ms |
53852 KB |
Output is correct |
19 |
Correct |
12 ms |
53852 KB |
Output is correct |
20 |
Correct |
14 ms |
53852 KB |
Output is correct |
21 |
Correct |
14 ms |
53852 KB |
Output is correct |
22 |
Correct |
13 ms |
58080 KB |
Output is correct |
23 |
Correct |
13 ms |
57944 KB |
Output is correct |
24 |
Correct |
16 ms |
58204 KB |
Output is correct |
25 |
Correct |
14 ms |
57948 KB |
Output is correct |
26 |
Correct |
13 ms |
55896 KB |
Output is correct |
27 |
Correct |
12 ms |
53852 KB |
Output is correct |
28 |
Correct |
12 ms |
56000 KB |
Output is correct |
29 |
Correct |
13 ms |
56116 KB |
Output is correct |
30 |
Correct |
13 ms |
55896 KB |
Output is correct |
31 |
Correct |
13 ms |
57948 KB |
Output is correct |
32 |
Correct |
13 ms |
57944 KB |
Output is correct |
33 |
Correct |
14 ms |
57948 KB |
Output is correct |
34 |
Correct |
13 ms |
56116 KB |
Output is correct |
35 |
Correct |
15 ms |
58176 KB |
Output is correct |
36 |
Correct |
14 ms |
57948 KB |
Output is correct |
37 |
Correct |
15 ms |
58160 KB |
Output is correct |
38 |
Correct |
13 ms |
55900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
53848 KB |
Output is correct |
2 |
Correct |
13 ms |
53848 KB |
Output is correct |
3 |
Correct |
13 ms |
53996 KB |
Output is correct |
4 |
Correct |
12 ms |
53852 KB |
Output is correct |
5 |
Correct |
12 ms |
53852 KB |
Output is correct |
6 |
Correct |
12 ms |
53852 KB |
Output is correct |
7 |
Correct |
12 ms |
53852 KB |
Output is correct |
8 |
Correct |
13 ms |
54020 KB |
Output is correct |
9 |
Correct |
12 ms |
53848 KB |
Output is correct |
10 |
Correct |
12 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53852 KB |
Output is correct |
12 |
Correct |
12 ms |
53852 KB |
Output is correct |
13 |
Correct |
12 ms |
53936 KB |
Output is correct |
14 |
Correct |
12 ms |
53852 KB |
Output is correct |
15 |
Correct |
12 ms |
53852 KB |
Output is correct |
16 |
Correct |
12 ms |
53852 KB |
Output is correct |
17 |
Correct |
13 ms |
53852 KB |
Output is correct |
18 |
Correct |
12 ms |
53852 KB |
Output is correct |
19 |
Correct |
123 ms |
60244 KB |
Output is correct |
20 |
Correct |
116 ms |
60240 KB |
Output is correct |
21 |
Correct |
116 ms |
60224 KB |
Output is correct |
22 |
Correct |
97 ms |
60244 KB |
Output is correct |
23 |
Correct |
87 ms |
60496 KB |
Output is correct |
24 |
Correct |
57 ms |
60324 KB |
Output is correct |
25 |
Correct |
125 ms |
63648 KB |
Output is correct |
26 |
Correct |
94 ms |
65776 KB |
Output is correct |
27 |
Correct |
145 ms |
64432 KB |
Output is correct |
28 |
Correct |
288 ms |
75720 KB |
Output is correct |
29 |
Correct |
297 ms |
74852 KB |
Output is correct |
30 |
Correct |
145 ms |
64520 KB |
Output is correct |
31 |
Correct |
145 ms |
64564 KB |
Output is correct |
32 |
Correct |
172 ms |
64672 KB |
Output is correct |
33 |
Correct |
205 ms |
63412 KB |
Output is correct |
34 |
Correct |
199 ms |
63252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
53848 KB |
Output is correct |
2 |
Correct |
13 ms |
53848 KB |
Output is correct |
3 |
Correct |
13 ms |
53996 KB |
Output is correct |
4 |
Correct |
12 ms |
53852 KB |
Output is correct |
5 |
Correct |
12 ms |
53852 KB |
Output is correct |
6 |
Correct |
12 ms |
53852 KB |
Output is correct |
7 |
Correct |
12 ms |
53852 KB |
Output is correct |
8 |
Correct |
13 ms |
54020 KB |
Output is correct |
9 |
Correct |
12 ms |
53848 KB |
Output is correct |
10 |
Correct |
12 ms |
53852 KB |
Output is correct |
11 |
Correct |
12 ms |
53852 KB |
Output is correct |
12 |
Correct |
12 ms |
53852 KB |
Output is correct |
13 |
Correct |
12 ms |
53936 KB |
Output is correct |
14 |
Correct |
12 ms |
53852 KB |
Output is correct |
15 |
Correct |
12 ms |
53852 KB |
Output is correct |
16 |
Correct |
12 ms |
53852 KB |
Output is correct |
17 |
Correct |
13 ms |
53852 KB |
Output is correct |
18 |
Correct |
12 ms |
53852 KB |
Output is correct |
19 |
Correct |
12 ms |
53852 KB |
Output is correct |
20 |
Correct |
14 ms |
53852 KB |
Output is correct |
21 |
Correct |
14 ms |
53852 KB |
Output is correct |
22 |
Correct |
13 ms |
58080 KB |
Output is correct |
23 |
Correct |
13 ms |
57944 KB |
Output is correct |
24 |
Correct |
16 ms |
58204 KB |
Output is correct |
25 |
Correct |
14 ms |
57948 KB |
Output is correct |
26 |
Correct |
13 ms |
55896 KB |
Output is correct |
27 |
Correct |
12 ms |
53852 KB |
Output is correct |
28 |
Correct |
12 ms |
56000 KB |
Output is correct |
29 |
Correct |
13 ms |
56116 KB |
Output is correct |
30 |
Correct |
13 ms |
55896 KB |
Output is correct |
31 |
Correct |
13 ms |
57948 KB |
Output is correct |
32 |
Correct |
13 ms |
57944 KB |
Output is correct |
33 |
Correct |
14 ms |
57948 KB |
Output is correct |
34 |
Correct |
13 ms |
56116 KB |
Output is correct |
35 |
Correct |
15 ms |
58176 KB |
Output is correct |
36 |
Correct |
14 ms |
57948 KB |
Output is correct |
37 |
Correct |
15 ms |
58160 KB |
Output is correct |
38 |
Correct |
13 ms |
55900 KB |
Output is correct |
39 |
Correct |
123 ms |
60244 KB |
Output is correct |
40 |
Correct |
116 ms |
60240 KB |
Output is correct |
41 |
Correct |
116 ms |
60224 KB |
Output is correct |
42 |
Correct |
97 ms |
60244 KB |
Output is correct |
43 |
Correct |
87 ms |
60496 KB |
Output is correct |
44 |
Correct |
57 ms |
60324 KB |
Output is correct |
45 |
Correct |
125 ms |
63648 KB |
Output is correct |
46 |
Correct |
94 ms |
65776 KB |
Output is correct |
47 |
Correct |
145 ms |
64432 KB |
Output is correct |
48 |
Correct |
288 ms |
75720 KB |
Output is correct |
49 |
Correct |
297 ms |
74852 KB |
Output is correct |
50 |
Correct |
145 ms |
64520 KB |
Output is correct |
51 |
Correct |
145 ms |
64564 KB |
Output is correct |
52 |
Correct |
172 ms |
64672 KB |
Output is correct |
53 |
Correct |
205 ms |
63412 KB |
Output is correct |
54 |
Correct |
199 ms |
63252 KB |
Output is correct |
55 |
Correct |
19 ms |
54364 KB |
Output is correct |
56 |
Correct |
19 ms |
54364 KB |
Output is correct |
57 |
Correct |
69 ms |
60628 KB |
Output is correct |
58 |
Correct |
40 ms |
61380 KB |
Output is correct |
59 |
Correct |
86 ms |
68900 KB |
Output is correct |
60 |
Correct |
305 ms |
81128 KB |
Output is correct |
61 |
Correct |
145 ms |
65976 KB |
Output is correct |
62 |
Correct |
145 ms |
68692 KB |
Output is correct |
63 |
Correct |
178 ms |
68680 KB |
Output is correct |
64 |
Correct |
296 ms |
67372 KB |
Output is correct |
65 |
Correct |
225 ms |
67668 KB |
Output is correct |
66 |
Correct |
300 ms |
77140 KB |
Output is correct |
67 |
Correct |
84 ms |
70248 KB |
Output is correct |
68 |
Correct |
176 ms |
71024 KB |
Output is correct |
69 |
Correct |
213 ms |
69396 KB |
Output is correct |
70 |
Correct |
165 ms |
71148 KB |
Output is correct |