#include <bits/stdc++.h>
#include "race.h"
using namespace std;
vector<pair<int, int>> adjlist[200000];
int dist[1000001];
int subsz[200000], level[200000];
vector<pair<int, int>> v;
vector<int> v2;
int k, ans;
void dfs(int x, int p) {
subsz[x] = 1;
for (auto i: adjlist[x]) {
if (i.first == p || level[i.first] != 0) {
continue;
}
dfs(i.first, x);
subsz[x] += subsz[i.first];
}
}
int centroid(int x, int p, int treesz) {
for (auto i: adjlist[x]) {
if (i.first == p || level[i.first] != 0) {
continue;
}
if (treesz < subsz[i.first] * 2) {
return centroid(i.first, x, treesz);
}
}
return x;
}
void dfs2(int x, int p, int depth, int cost) {
if (cost > k) {
return;
}
v.push_back({cost, depth});
for (auto i: adjlist[x]) {
if (i.first == p || level[i.first] != 0) {
continue;
}
dfs2(i.first, x, depth + 1, cost + i.second);
}
}
void decomp(int x) {
dfs(x, -1);
int cent = centroid(x, -1, subsz[x]);
level[cent] = 1;
dist[0] = 0;
for (auto i: adjlist[cent]) {
if (level[i.first] != 0) {
continue;
}
dfs2(i.first, -1, 1, i.second);
for (auto j: v) {
if (dist[k - j.first] == INT_MAX) {
continue;
}
ans = min(ans, dist[k - j.first] + j.second);
}
for (auto j: v) {
v2.push_back(j.first);
dist[j.first] = min(dist[j.first], j.second);
}
v.clear();
}
for (auto i: v2) {
dist[i] = INT_MAX;
}
v2.clear();
for (auto i: adjlist[cent]) {
if (level[i.first] == 0) {
decomp(i.first);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N - 1; i++) {
adjlist[H[i][0]].push_back({H[i][1], L[i]});
adjlist[H[i][1]].push_back({H[i][0], L[i]});
}
k = K;
for (int i = 1; i <= k; i++) {
dist[i] = INT_MAX;
}
ans = N;
decomp(0);
if (ans == N) {
return -1;
} else {
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12196 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12188 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
11992 KB |
Output is correct |
9 |
Correct |
2 ms |
12196 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
2 ms |
12056 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
2 ms |
12124 KB |
Output is correct |
17 |
Correct |
2 ms |
12124 KB |
Output is correct |
18 |
Correct |
2 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12196 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12188 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
11992 KB |
Output is correct |
9 |
Correct |
2 ms |
12196 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
2 ms |
12056 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
2 ms |
12124 KB |
Output is correct |
17 |
Correct |
2 ms |
12124 KB |
Output is correct |
18 |
Correct |
2 ms |
12124 KB |
Output is correct |
19 |
Correct |
2 ms |
12124 KB |
Output is correct |
20 |
Correct |
3 ms |
12124 KB |
Output is correct |
21 |
Correct |
3 ms |
12124 KB |
Output is correct |
22 |
Correct |
3 ms |
14256 KB |
Output is correct |
23 |
Correct |
3 ms |
14172 KB |
Output is correct |
24 |
Correct |
4 ms |
14172 KB |
Output is correct |
25 |
Correct |
4 ms |
14172 KB |
Output is correct |
26 |
Correct |
3 ms |
14172 KB |
Output is correct |
27 |
Correct |
4 ms |
14172 KB |
Output is correct |
28 |
Correct |
3 ms |
12124 KB |
Output is correct |
29 |
Correct |
4 ms |
14172 KB |
Output is correct |
30 |
Correct |
4 ms |
14172 KB |
Output is correct |
31 |
Correct |
3 ms |
14172 KB |
Output is correct |
32 |
Correct |
3 ms |
14252 KB |
Output is correct |
33 |
Correct |
4 ms |
14168 KB |
Output is correct |
34 |
Correct |
4 ms |
14168 KB |
Output is correct |
35 |
Correct |
3 ms |
14172 KB |
Output is correct |
36 |
Correct |
4 ms |
14168 KB |
Output is correct |
37 |
Correct |
4 ms |
14176 KB |
Output is correct |
38 |
Correct |
3 ms |
14256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12196 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12188 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
11992 KB |
Output is correct |
9 |
Correct |
2 ms |
12196 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
2 ms |
12056 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
2 ms |
12124 KB |
Output is correct |
17 |
Correct |
2 ms |
12124 KB |
Output is correct |
18 |
Correct |
2 ms |
12124 KB |
Output is correct |
19 |
Correct |
82 ms |
19628 KB |
Output is correct |
20 |
Correct |
90 ms |
19560 KB |
Output is correct |
21 |
Correct |
90 ms |
19832 KB |
Output is correct |
22 |
Correct |
80 ms |
20412 KB |
Output is correct |
23 |
Correct |
59 ms |
19536 KB |
Output is correct |
24 |
Correct |
46 ms |
19540 KB |
Output is correct |
25 |
Correct |
98 ms |
22852 KB |
Output is correct |
26 |
Correct |
75 ms |
27636 KB |
Output is correct |
27 |
Correct |
112 ms |
24916 KB |
Output is correct |
28 |
Correct |
203 ms |
39248 KB |
Output is correct |
29 |
Correct |
179 ms |
38204 KB |
Output is correct |
30 |
Correct |
108 ms |
24768 KB |
Output is correct |
31 |
Correct |
149 ms |
24964 KB |
Output is correct |
32 |
Correct |
131 ms |
24896 KB |
Output is correct |
33 |
Correct |
151 ms |
23796 KB |
Output is correct |
34 |
Correct |
118 ms |
24660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12124 KB |
Output is correct |
2 |
Correct |
3 ms |
12196 KB |
Output is correct |
3 |
Correct |
3 ms |
12124 KB |
Output is correct |
4 |
Correct |
3 ms |
12188 KB |
Output is correct |
5 |
Correct |
3 ms |
12124 KB |
Output is correct |
6 |
Correct |
3 ms |
12124 KB |
Output is correct |
7 |
Correct |
3 ms |
12124 KB |
Output is correct |
8 |
Correct |
3 ms |
11992 KB |
Output is correct |
9 |
Correct |
2 ms |
12196 KB |
Output is correct |
10 |
Correct |
3 ms |
12124 KB |
Output is correct |
11 |
Correct |
3 ms |
12124 KB |
Output is correct |
12 |
Correct |
2 ms |
12124 KB |
Output is correct |
13 |
Correct |
2 ms |
12056 KB |
Output is correct |
14 |
Correct |
3 ms |
12124 KB |
Output is correct |
15 |
Correct |
3 ms |
12124 KB |
Output is correct |
16 |
Correct |
2 ms |
12124 KB |
Output is correct |
17 |
Correct |
2 ms |
12124 KB |
Output is correct |
18 |
Correct |
2 ms |
12124 KB |
Output is correct |
19 |
Correct |
2 ms |
12124 KB |
Output is correct |
20 |
Correct |
3 ms |
12124 KB |
Output is correct |
21 |
Correct |
3 ms |
12124 KB |
Output is correct |
22 |
Correct |
3 ms |
14256 KB |
Output is correct |
23 |
Correct |
3 ms |
14172 KB |
Output is correct |
24 |
Correct |
4 ms |
14172 KB |
Output is correct |
25 |
Correct |
4 ms |
14172 KB |
Output is correct |
26 |
Correct |
3 ms |
14172 KB |
Output is correct |
27 |
Correct |
4 ms |
14172 KB |
Output is correct |
28 |
Correct |
3 ms |
12124 KB |
Output is correct |
29 |
Correct |
4 ms |
14172 KB |
Output is correct |
30 |
Correct |
4 ms |
14172 KB |
Output is correct |
31 |
Correct |
3 ms |
14172 KB |
Output is correct |
32 |
Correct |
3 ms |
14252 KB |
Output is correct |
33 |
Correct |
4 ms |
14168 KB |
Output is correct |
34 |
Correct |
4 ms |
14168 KB |
Output is correct |
35 |
Correct |
3 ms |
14172 KB |
Output is correct |
36 |
Correct |
4 ms |
14168 KB |
Output is correct |
37 |
Correct |
4 ms |
14176 KB |
Output is correct |
38 |
Correct |
3 ms |
14256 KB |
Output is correct |
39 |
Correct |
82 ms |
19628 KB |
Output is correct |
40 |
Correct |
90 ms |
19560 KB |
Output is correct |
41 |
Correct |
90 ms |
19832 KB |
Output is correct |
42 |
Correct |
80 ms |
20412 KB |
Output is correct |
43 |
Correct |
59 ms |
19536 KB |
Output is correct |
44 |
Correct |
46 ms |
19540 KB |
Output is correct |
45 |
Correct |
98 ms |
22852 KB |
Output is correct |
46 |
Correct |
75 ms |
27636 KB |
Output is correct |
47 |
Correct |
112 ms |
24916 KB |
Output is correct |
48 |
Correct |
203 ms |
39248 KB |
Output is correct |
49 |
Correct |
179 ms |
38204 KB |
Output is correct |
50 |
Correct |
108 ms |
24768 KB |
Output is correct |
51 |
Correct |
149 ms |
24964 KB |
Output is correct |
52 |
Correct |
131 ms |
24896 KB |
Output is correct |
53 |
Correct |
151 ms |
23796 KB |
Output is correct |
54 |
Correct |
118 ms |
24660 KB |
Output is correct |
55 |
Correct |
7 ms |
12636 KB |
Output is correct |
56 |
Correct |
11 ms |
12636 KB |
Output is correct |
57 |
Correct |
58 ms |
20684 KB |
Output is correct |
58 |
Correct |
28 ms |
20172 KB |
Output is correct |
59 |
Correct |
71 ms |
27860 KB |
Output is correct |
60 |
Correct |
332 ms |
42812 KB |
Output is correct |
61 |
Correct |
104 ms |
25376 KB |
Output is correct |
62 |
Correct |
116 ms |
28116 KB |
Output is correct |
63 |
Correct |
141 ms |
28224 KB |
Output is correct |
64 |
Correct |
220 ms |
29384 KB |
Output is correct |
65 |
Correct |
154 ms |
25680 KB |
Output is correct |
66 |
Correct |
205 ms |
38124 KB |
Output is correct |
67 |
Correct |
71 ms |
29236 KB |
Output is correct |
68 |
Correct |
134 ms |
28364 KB |
Output is correct |
69 |
Correct |
149 ms |
28508 KB |
Output is correct |
70 |
Correct |
122 ms |
27976 KB |
Output is correct |