#include<bits/stdc++.h>
#include "race.h"
using namespace std;
const int MAXN = 200010;
const int MAXK = 1000010;
int n, k, n_cur, sub[MAXN], best[MAXK], ans = MAXK;
vector<int> graph[MAXN], cost[MAXN];
bool marc[MAXN];
void find_sub(int v, int p)
{
sub[v] = 1;
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
if(u == p || marc[u]) continue;
find_sub(u, v), sub[v] += sub[u];
}
}
int find_centroid(int v, int p)
{
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
if(u == p || marc[u]) continue;
if(2 * sub[u] >= n_cur) return find_centroid(u, v);
}
return v;
}
void dfs(int v, int p, int dist, int depth, bool flag)
{
if(k < dist) return;
if(flag)
{
if(best[dist] == 0) best[dist] = depth;
else best[dist] = min(best[dist], depth);
}
else best[dist] = 0;
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
int c = cost[v][i];
if(u != p && !marc[u]) dfs(u, v, dist + c, depth + 1, flag);
}
}
void dfs2(int v, int p, int dist, int depth)
{
if(k < dist) return;
if(best[k - dist]) ans = min(ans, best[k - dist] + depth);
if(dist == k) ans = min(ans, depth);
for(int i = 0; i < (int) graph[v].size(); i++)
{
int u = graph[v][i];
int c = cost[v][i];
if(u != p && !marc[u]) dfs2(u, v, dist + c, depth + 1);
}
}
void decompose(int v, int p)
{
find_sub(v, p);
n_cur = sub[v];
int c = find_centroid(v, p);
// printf("\nc = %d\n", c - 1);
marc[c] = true;
for(int i = 0; i < (int) graph[c].size(); i++)
{
int u = graph[c][i];
if(u == p || marc[u]) continue;
dfs2(u, c, cost[c][i], 1);
dfs(u, c, cost[c][i], 1, true);
}
// for(int i = 0; i <= k; i++) printf("best[%d] = %d\n", i, best[i]);
dfs(c, c, 0, 0, false);
for(int i = 0; i < (int) graph[c].size(); i++)
{
int u = graph[c][i];
if(!marc[u]) decompose(u, c);
}
}
int best_path(int N, int K, int h[][2], int l[])
{
n = N, k = K;
for(int i = 0; i < n - 1; i++)
{
int u = h[i][0] + 1;
int v = h[i][1] + 1;
int w = l[i];
graph[u].push_back(v);
graph[v].push_back(u);
cost[u].push_back(w);
cost[v].push_back(w);
}
decompose(1, 1);
return (ans == MAXK) ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9784 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
6 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9784 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
6 ms |
9708 KB |
Output is correct |
19 |
Correct |
6 ms |
9708 KB |
Output is correct |
20 |
Correct |
6 ms |
9708 KB |
Output is correct |
21 |
Correct |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
10 ms |
12268 KB |
Output is correct |
23 |
Correct |
9 ms |
12012 KB |
Output is correct |
24 |
Correct |
9 ms |
12780 KB |
Output is correct |
25 |
Correct |
9 ms |
11628 KB |
Output is correct |
26 |
Correct |
8 ms |
11116 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
9 ms |
10604 KB |
Output is correct |
29 |
Correct |
8 ms |
11116 KB |
Output is correct |
30 |
Correct |
9 ms |
11244 KB |
Output is correct |
31 |
Correct |
9 ms |
11628 KB |
Output is correct |
32 |
Correct |
10 ms |
11756 KB |
Output is correct |
33 |
Correct |
11 ms |
12524 KB |
Output is correct |
34 |
Correct |
9 ms |
12140 KB |
Output is correct |
35 |
Correct |
10 ms |
12780 KB |
Output is correct |
36 |
Correct |
12 ms |
13056 KB |
Output is correct |
37 |
Correct |
9 ms |
11756 KB |
Output is correct |
38 |
Correct |
8 ms |
10988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9784 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
6 ms |
9708 KB |
Output is correct |
19 |
Correct |
251 ms |
19180 KB |
Output is correct |
20 |
Correct |
240 ms |
19180 KB |
Output is correct |
21 |
Correct |
291 ms |
19308 KB |
Output is correct |
22 |
Correct |
278 ms |
19308 KB |
Output is correct |
23 |
Correct |
154 ms |
19052 KB |
Output is correct |
24 |
Correct |
115 ms |
19564 KB |
Output is correct |
25 |
Correct |
210 ms |
23148 KB |
Output is correct |
26 |
Correct |
137 ms |
26988 KB |
Output is correct |
27 |
Correct |
329 ms |
29804 KB |
Output is correct |
28 |
Correct |
405 ms |
44268 KB |
Output is correct |
29 |
Correct |
416 ms |
43136 KB |
Output is correct |
30 |
Correct |
343 ms |
29676 KB |
Output is correct |
31 |
Correct |
326 ms |
29924 KB |
Output is correct |
32 |
Correct |
394 ms |
29804 KB |
Output is correct |
33 |
Correct |
402 ms |
28652 KB |
Output is correct |
34 |
Correct |
333 ms |
29548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9708 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
6 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
7 ms |
9836 KB |
Output is correct |
12 |
Correct |
7 ms |
9784 KB |
Output is correct |
13 |
Correct |
8 ms |
9708 KB |
Output is correct |
14 |
Correct |
7 ms |
9836 KB |
Output is correct |
15 |
Correct |
8 ms |
9708 KB |
Output is correct |
16 |
Correct |
6 ms |
9708 KB |
Output is correct |
17 |
Correct |
6 ms |
9708 KB |
Output is correct |
18 |
Correct |
6 ms |
9708 KB |
Output is correct |
19 |
Correct |
6 ms |
9708 KB |
Output is correct |
20 |
Correct |
6 ms |
9708 KB |
Output is correct |
21 |
Correct |
8 ms |
9836 KB |
Output is correct |
22 |
Correct |
10 ms |
12268 KB |
Output is correct |
23 |
Correct |
9 ms |
12012 KB |
Output is correct |
24 |
Correct |
9 ms |
12780 KB |
Output is correct |
25 |
Correct |
9 ms |
11628 KB |
Output is correct |
26 |
Correct |
8 ms |
11116 KB |
Output is correct |
27 |
Correct |
7 ms |
9836 KB |
Output is correct |
28 |
Correct |
9 ms |
10604 KB |
Output is correct |
29 |
Correct |
8 ms |
11116 KB |
Output is correct |
30 |
Correct |
9 ms |
11244 KB |
Output is correct |
31 |
Correct |
9 ms |
11628 KB |
Output is correct |
32 |
Correct |
10 ms |
11756 KB |
Output is correct |
33 |
Correct |
11 ms |
12524 KB |
Output is correct |
34 |
Correct |
9 ms |
12140 KB |
Output is correct |
35 |
Correct |
10 ms |
12780 KB |
Output is correct |
36 |
Correct |
12 ms |
13056 KB |
Output is correct |
37 |
Correct |
9 ms |
11756 KB |
Output is correct |
38 |
Correct |
8 ms |
10988 KB |
Output is correct |
39 |
Correct |
251 ms |
19180 KB |
Output is correct |
40 |
Correct |
240 ms |
19180 KB |
Output is correct |
41 |
Correct |
291 ms |
19308 KB |
Output is correct |
42 |
Correct |
278 ms |
19308 KB |
Output is correct |
43 |
Correct |
154 ms |
19052 KB |
Output is correct |
44 |
Correct |
115 ms |
19564 KB |
Output is correct |
45 |
Correct |
210 ms |
23148 KB |
Output is correct |
46 |
Correct |
137 ms |
26988 KB |
Output is correct |
47 |
Correct |
329 ms |
29804 KB |
Output is correct |
48 |
Correct |
405 ms |
44268 KB |
Output is correct |
49 |
Correct |
416 ms |
43136 KB |
Output is correct |
50 |
Correct |
343 ms |
29676 KB |
Output is correct |
51 |
Correct |
326 ms |
29924 KB |
Output is correct |
52 |
Correct |
394 ms |
29804 KB |
Output is correct |
53 |
Correct |
402 ms |
28652 KB |
Output is correct |
54 |
Correct |
333 ms |
29548 KB |
Output is correct |
55 |
Correct |
18 ms |
10732 KB |
Output is correct |
56 |
Correct |
20 ms |
10604 KB |
Output is correct |
57 |
Correct |
127 ms |
19052 KB |
Output is correct |
58 |
Correct |
53 ms |
19548 KB |
Output is correct |
59 |
Correct |
140 ms |
27244 KB |
Output is correct |
60 |
Correct |
861 ms |
45676 KB |
Output is correct |
61 |
Correct |
389 ms |
29968 KB |
Output is correct |
62 |
Correct |
430 ms |
29932 KB |
Output is correct |
63 |
Correct |
508 ms |
30060 KB |
Output is correct |
64 |
Correct |
951 ms |
30564 KB |
Output is correct |
65 |
Correct |
406 ms |
29952 KB |
Output is correct |
66 |
Correct |
646 ms |
44772 KB |
Output is correct |
67 |
Correct |
250 ms |
30532 KB |
Output is correct |
68 |
Correct |
506 ms |
33260 KB |
Output is correct |
69 |
Correct |
491 ms |
33356 KB |
Output is correct |
70 |
Correct |
467 ms |
32364 KB |
Output is correct |