#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] = 1e8;
if(k > weight)
help[k - weight] = 1e8;
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 |
23 ms |
47196 KB |
Output is correct |
2 |
Correct |
23 ms |
47196 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
23 ms |
47452 KB |
Output is correct |
5 |
Correct |
24 ms |
47424 KB |
Output is correct |
6 |
Correct |
23 ms |
47392 KB |
Output is correct |
7 |
Correct |
27 ms |
47184 KB |
Output is correct |
8 |
Correct |
22 ms |
47332 KB |
Output is correct |
9 |
Correct |
26 ms |
47404 KB |
Output is correct |
10 |
Correct |
23 ms |
47448 KB |
Output is correct |
11 |
Correct |
23 ms |
47412 KB |
Output is correct |
12 |
Correct |
23 ms |
47704 KB |
Output is correct |
13 |
Correct |
23 ms |
47196 KB |
Output is correct |
14 |
Correct |
22 ms |
47196 KB |
Output is correct |
15 |
Correct |
22 ms |
47196 KB |
Output is correct |
16 |
Correct |
23 ms |
47448 KB |
Output is correct |
17 |
Correct |
21 ms |
47448 KB |
Output is correct |
18 |
Correct |
23 ms |
47452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47196 KB |
Output is correct |
2 |
Correct |
23 ms |
47196 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
23 ms |
47452 KB |
Output is correct |
5 |
Correct |
24 ms |
47424 KB |
Output is correct |
6 |
Correct |
23 ms |
47392 KB |
Output is correct |
7 |
Correct |
27 ms |
47184 KB |
Output is correct |
8 |
Correct |
22 ms |
47332 KB |
Output is correct |
9 |
Correct |
26 ms |
47404 KB |
Output is correct |
10 |
Correct |
23 ms |
47448 KB |
Output is correct |
11 |
Correct |
23 ms |
47412 KB |
Output is correct |
12 |
Correct |
23 ms |
47704 KB |
Output is correct |
13 |
Correct |
23 ms |
47196 KB |
Output is correct |
14 |
Correct |
22 ms |
47196 KB |
Output is correct |
15 |
Correct |
22 ms |
47196 KB |
Output is correct |
16 |
Correct |
23 ms |
47448 KB |
Output is correct |
17 |
Correct |
21 ms |
47448 KB |
Output is correct |
18 |
Correct |
23 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47192 KB |
Output is correct |
20 |
Correct |
26 ms |
47452 KB |
Output is correct |
21 |
Correct |
23 ms |
47376 KB |
Output is correct |
22 |
Correct |
23 ms |
50780 KB |
Output is correct |
23 |
Correct |
24 ms |
50268 KB |
Output is correct |
24 |
Correct |
26 ms |
50772 KB |
Output is correct |
25 |
Correct |
26 ms |
50516 KB |
Output is correct |
26 |
Correct |
25 ms |
48732 KB |
Output is correct |
27 |
Correct |
23 ms |
47424 KB |
Output is correct |
28 |
Correct |
24 ms |
48216 KB |
Output is correct |
29 |
Correct |
24 ms |
48732 KB |
Output is correct |
30 |
Correct |
22 ms |
48728 KB |
Output is correct |
31 |
Correct |
23 ms |
50012 KB |
Output is correct |
32 |
Correct |
22 ms |
50268 KB |
Output is correct |
33 |
Correct |
22 ms |
50520 KB |
Output is correct |
34 |
Correct |
23 ms |
49704 KB |
Output is correct |
35 |
Correct |
23 ms |
50524 KB |
Output is correct |
36 |
Correct |
24 ms |
51036 KB |
Output is correct |
37 |
Correct |
25 ms |
50520 KB |
Output is correct |
38 |
Correct |
31 ms |
49244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47196 KB |
Output is correct |
2 |
Correct |
23 ms |
47196 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
23 ms |
47452 KB |
Output is correct |
5 |
Correct |
24 ms |
47424 KB |
Output is correct |
6 |
Correct |
23 ms |
47392 KB |
Output is correct |
7 |
Correct |
27 ms |
47184 KB |
Output is correct |
8 |
Correct |
22 ms |
47332 KB |
Output is correct |
9 |
Correct |
26 ms |
47404 KB |
Output is correct |
10 |
Correct |
23 ms |
47448 KB |
Output is correct |
11 |
Correct |
23 ms |
47412 KB |
Output is correct |
12 |
Correct |
23 ms |
47704 KB |
Output is correct |
13 |
Correct |
23 ms |
47196 KB |
Output is correct |
14 |
Correct |
22 ms |
47196 KB |
Output is correct |
15 |
Correct |
22 ms |
47196 KB |
Output is correct |
16 |
Correct |
23 ms |
47448 KB |
Output is correct |
17 |
Correct |
21 ms |
47448 KB |
Output is correct |
18 |
Correct |
23 ms |
47452 KB |
Output is correct |
19 |
Correct |
132 ms |
54352 KB |
Output is correct |
20 |
Correct |
131 ms |
54212 KB |
Output is correct |
21 |
Correct |
125 ms |
54408 KB |
Output is correct |
22 |
Correct |
107 ms |
54304 KB |
Output is correct |
23 |
Correct |
98 ms |
54608 KB |
Output is correct |
24 |
Correct |
65 ms |
54608 KB |
Output is correct |
25 |
Correct |
151 ms |
57628 KB |
Output is correct |
26 |
Correct |
92 ms |
60072 KB |
Output is correct |
27 |
Correct |
143 ms |
61780 KB |
Output is correct |
28 |
Correct |
367 ms |
72908 KB |
Output is correct |
29 |
Correct |
334 ms |
72012 KB |
Output is correct |
30 |
Correct |
147 ms |
61776 KB |
Output is correct |
31 |
Correct |
142 ms |
61780 KB |
Output is correct |
32 |
Correct |
197 ms |
61656 KB |
Output is correct |
33 |
Correct |
239 ms |
60496 KB |
Output is correct |
34 |
Correct |
242 ms |
61320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47196 KB |
Output is correct |
2 |
Correct |
23 ms |
47196 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
23 ms |
47452 KB |
Output is correct |
5 |
Correct |
24 ms |
47424 KB |
Output is correct |
6 |
Correct |
23 ms |
47392 KB |
Output is correct |
7 |
Correct |
27 ms |
47184 KB |
Output is correct |
8 |
Correct |
22 ms |
47332 KB |
Output is correct |
9 |
Correct |
26 ms |
47404 KB |
Output is correct |
10 |
Correct |
23 ms |
47448 KB |
Output is correct |
11 |
Correct |
23 ms |
47412 KB |
Output is correct |
12 |
Correct |
23 ms |
47704 KB |
Output is correct |
13 |
Correct |
23 ms |
47196 KB |
Output is correct |
14 |
Correct |
22 ms |
47196 KB |
Output is correct |
15 |
Correct |
22 ms |
47196 KB |
Output is correct |
16 |
Correct |
23 ms |
47448 KB |
Output is correct |
17 |
Correct |
21 ms |
47448 KB |
Output is correct |
18 |
Correct |
23 ms |
47452 KB |
Output is correct |
19 |
Correct |
23 ms |
47192 KB |
Output is correct |
20 |
Correct |
26 ms |
47452 KB |
Output is correct |
21 |
Correct |
23 ms |
47376 KB |
Output is correct |
22 |
Correct |
23 ms |
50780 KB |
Output is correct |
23 |
Correct |
24 ms |
50268 KB |
Output is correct |
24 |
Correct |
26 ms |
50772 KB |
Output is correct |
25 |
Correct |
26 ms |
50516 KB |
Output is correct |
26 |
Correct |
25 ms |
48732 KB |
Output is correct |
27 |
Correct |
23 ms |
47424 KB |
Output is correct |
28 |
Correct |
24 ms |
48216 KB |
Output is correct |
29 |
Correct |
24 ms |
48732 KB |
Output is correct |
30 |
Correct |
22 ms |
48728 KB |
Output is correct |
31 |
Correct |
23 ms |
50012 KB |
Output is correct |
32 |
Correct |
22 ms |
50268 KB |
Output is correct |
33 |
Correct |
22 ms |
50520 KB |
Output is correct |
34 |
Correct |
23 ms |
49704 KB |
Output is correct |
35 |
Correct |
23 ms |
50524 KB |
Output is correct |
36 |
Correct |
24 ms |
51036 KB |
Output is correct |
37 |
Correct |
25 ms |
50520 KB |
Output is correct |
38 |
Correct |
31 ms |
49244 KB |
Output is correct |
39 |
Correct |
132 ms |
54352 KB |
Output is correct |
40 |
Correct |
131 ms |
54212 KB |
Output is correct |
41 |
Correct |
125 ms |
54408 KB |
Output is correct |
42 |
Correct |
107 ms |
54304 KB |
Output is correct |
43 |
Correct |
98 ms |
54608 KB |
Output is correct |
44 |
Correct |
65 ms |
54608 KB |
Output is correct |
45 |
Correct |
151 ms |
57628 KB |
Output is correct |
46 |
Correct |
92 ms |
60072 KB |
Output is correct |
47 |
Correct |
143 ms |
61780 KB |
Output is correct |
48 |
Correct |
367 ms |
72908 KB |
Output is correct |
49 |
Correct |
334 ms |
72012 KB |
Output is correct |
50 |
Correct |
147 ms |
61776 KB |
Output is correct |
51 |
Correct |
142 ms |
61780 KB |
Output is correct |
52 |
Correct |
197 ms |
61656 KB |
Output is correct |
53 |
Correct |
239 ms |
60496 KB |
Output is correct |
54 |
Correct |
242 ms |
61320 KB |
Output is correct |
55 |
Correct |
37 ms |
47960 KB |
Output is correct |
56 |
Correct |
28 ms |
47964 KB |
Output is correct |
57 |
Correct |
69 ms |
54612 KB |
Output is correct |
58 |
Correct |
42 ms |
54224 KB |
Output is correct |
59 |
Correct |
88 ms |
60748 KB |
Output is correct |
60 |
Correct |
327 ms |
76276 KB |
Output is correct |
61 |
Correct |
146 ms |
61780 KB |
Output is correct |
62 |
Correct |
143 ms |
61780 KB |
Output is correct |
63 |
Correct |
194 ms |
61780 KB |
Output is correct |
64 |
Correct |
332 ms |
64096 KB |
Output is correct |
65 |
Correct |
228 ms |
62544 KB |
Output is correct |
66 |
Correct |
366 ms |
74068 KB |
Output is correct |
67 |
Correct |
106 ms |
62524 KB |
Output is correct |
68 |
Correct |
182 ms |
65116 KB |
Output is correct |
69 |
Correct |
187 ms |
65460 KB |
Output is correct |
70 |
Correct |
181 ms |
64564 KB |
Output is correct |