#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
int K, N, res, curr_maks, curr_node, cnt, ans;
vector<vii> adjList(200001);
vi subtree, gotov, mini, moze;
void cal_subtree(int curr, int prev){
int D = 0;
for (int i = 0; i < adjList[curr].size(); i++){
int t = adjList[curr][i].first;
if (t != prev && !gotov[t]){
cal_subtree(t, curr);
D += 1 + subtree[t];
}
}
subtree[curr] = D;
}
void find_centr(int curr, int prev, int uk){
int x = uk - subtree[curr] - 1;
for (int i = 0; i < adjList[curr].size(); i++){
int t = adjList[curr][i].first;
if (t != prev && !gotov[t]){
find_centr(t, curr, uk);
x = max(x, 1 + subtree[t]);
}
}
if (curr_maks > x){
curr_node = curr;
curr_maks = x;
}
}
void dfs1(int curr, int prev, int dist, int depth){
if (dist > K)
return;
if (moze[dist] != cnt || depth < mini[dist]){
moze[dist] = cnt;
mini[dist] = depth;
}
for (int i = 0; i < adjList[curr].size(); i++){
int t = adjList[curr][i].first;
if (t != prev && !gotov[t])
dfs1(t, curr, dist + adjList[curr][i].second, depth + 1);
}
}
void dfs2(int curr, int prev, int dist, int depth){
if (dist > K)
return;
//cout << curr << " " << prev << " " << dist << endl;
if (moze[K - dist] == cnt){
ans = min(ans, depth + mini[K - dist]);
//cout << curr << " " << prev << " " << dist << endl;
}
if (K == dist){
ans = min(ans, depth);
//cout << curr << " " << prev << " " << dist << endl;
}
for (int i = 0; i < adjList[curr].size(); i++){
int t = adjList[curr][i].first;
if (t != prev && !gotov[t])
dfs2(t, curr, dist + adjList[curr][i].second, depth + 1);
}
}
void solve(int curr){
cal_subtree(curr, -1);
if (subtree[curr] <= 1)
return;
curr_node = -1; curr_maks = INT_MAX;
find_centr(curr, -1, subtree[curr] + 1);
int centr = curr_node;
//cout << centr << endl;
cnt++;
for (int i = 0; i < adjList[centr].size(); i++){
int t = adjList[centr][i].first;
if (!gotov[t]){
dfs2(t, centr, adjList[centr][i].second, 1);
dfs1(t, centr, adjList[centr][i].second, 1);
}
}
//1. i 2. dfs
gotov[centr] = true;
for (int i = 0; i < adjList[centr].size(); i++){
int t = adjList[centr][i].first;
if (!gotov[t])
solve(t);
}
}
int best_path(int n, int k, int h[][2], int l[]){
res = INT_MAX;
N = n; K = k;
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]});
}
gotov.assign(n +1 , false);
mini.assign(k + 1, 0);
subtree.assign(n + 1, 0);
moze.assign(k + 1, -1);
cnt = 0;
ans = INT_MAX;
solve(0);
if (ans == INT_MAX)
return -1;
return ans;
}
Compilation message
race.cpp: In function 'void cal_subtree(int, int)':
race.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < adjList[curr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void find_centr(int, int, int)':
race.cpp:35:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 0; i < adjList[curr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:58:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < adjList[curr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:68:3: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
68 | if (dist > K)
| ^~
race.cpp:71:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
71 | if (moze[K - dist] == cnt){
| ^~
race.cpp:80:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | for (int i = 0; i < adjList[curr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:97:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < adjList[centr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~
race.cpp:109:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i = 0; i < adjList[centr].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
10 ms |
12288 KB |
Output is correct |
23 |
Correct |
8 ms |
11008 KB |
Output is correct |
24 |
Correct |
10 ms |
11904 KB |
Output is correct |
25 |
Correct |
9 ms |
11648 KB |
Output is correct |
26 |
Correct |
6 ms |
7808 KB |
Output is correct |
27 |
Correct |
8 ms |
11392 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
10240 KB |
Output is correct |
32 |
Correct |
11 ms |
10624 KB |
Output is correct |
33 |
Correct |
9 ms |
11264 KB |
Output is correct |
34 |
Correct |
8 ms |
9728 KB |
Output is correct |
35 |
Correct |
8 ms |
11520 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
8 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
241 ms |
10700 KB |
Output is correct |
20 |
Correct |
239 ms |
10616 KB |
Output is correct |
21 |
Correct |
262 ms |
10616 KB |
Output is correct |
22 |
Correct |
227 ms |
10744 KB |
Output is correct |
23 |
Correct |
158 ms |
10872 KB |
Output is correct |
24 |
Correct |
96 ms |
10852 KB |
Output is correct |
25 |
Correct |
183 ms |
14204 KB |
Output is correct |
26 |
Correct |
117 ms |
17912 KB |
Output is correct |
27 |
Correct |
279 ms |
16504 KB |
Output is correct |
28 |
Correct |
473 ms |
30816 KB |
Output is correct |
29 |
Correct |
434 ms |
29688 KB |
Output is correct |
30 |
Correct |
274 ms |
16632 KB |
Output is correct |
31 |
Correct |
228 ms |
16644 KB |
Output is correct |
32 |
Correct |
385 ms |
16504 KB |
Output is correct |
33 |
Correct |
405 ms |
15480 KB |
Output is correct |
34 |
Correct |
305 ms |
15288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
5 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
5 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
5 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
5120 KB |
Output is correct |
18 |
Correct |
5 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
10 ms |
12288 KB |
Output is correct |
23 |
Correct |
8 ms |
11008 KB |
Output is correct |
24 |
Correct |
10 ms |
11904 KB |
Output is correct |
25 |
Correct |
9 ms |
11648 KB |
Output is correct |
26 |
Correct |
6 ms |
7808 KB |
Output is correct |
27 |
Correct |
8 ms |
11392 KB |
Output is correct |
28 |
Correct |
5 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
6 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
10240 KB |
Output is correct |
32 |
Correct |
11 ms |
10624 KB |
Output is correct |
33 |
Correct |
9 ms |
11264 KB |
Output is correct |
34 |
Correct |
8 ms |
9728 KB |
Output is correct |
35 |
Correct |
8 ms |
11520 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
8 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
39 |
Correct |
241 ms |
10700 KB |
Output is correct |
40 |
Correct |
239 ms |
10616 KB |
Output is correct |
41 |
Correct |
262 ms |
10616 KB |
Output is correct |
42 |
Correct |
227 ms |
10744 KB |
Output is correct |
43 |
Correct |
158 ms |
10872 KB |
Output is correct |
44 |
Correct |
96 ms |
10852 KB |
Output is correct |
45 |
Correct |
183 ms |
14204 KB |
Output is correct |
46 |
Correct |
117 ms |
17912 KB |
Output is correct |
47 |
Correct |
279 ms |
16504 KB |
Output is correct |
48 |
Correct |
473 ms |
30816 KB |
Output is correct |
49 |
Correct |
434 ms |
29688 KB |
Output is correct |
50 |
Correct |
274 ms |
16632 KB |
Output is correct |
51 |
Correct |
228 ms |
16644 KB |
Output is correct |
52 |
Correct |
385 ms |
16504 KB |
Output is correct |
53 |
Correct |
405 ms |
15480 KB |
Output is correct |
54 |
Correct |
305 ms |
15288 KB |
Output is correct |
55 |
Correct |
15 ms |
5760 KB |
Output is correct |
56 |
Correct |
15 ms |
5760 KB |
Output is correct |
57 |
Correct |
111 ms |
12396 KB |
Output is correct |
58 |
Correct |
48 ms |
12012 KB |
Output is correct |
59 |
Correct |
123 ms |
20804 KB |
Output is correct |
60 |
Correct |
624 ms |
40888 KB |
Output is correct |
61 |
Correct |
242 ms |
19576 KB |
Output is correct |
62 |
Correct |
279 ms |
27384 KB |
Output is correct |
63 |
Correct |
399 ms |
27388 KB |
Output is correct |
64 |
Correct |
884 ms |
23816 KB |
Output is correct |
65 |
Correct |
469 ms |
20460 KB |
Output is correct |
66 |
Correct |
587 ms |
38140 KB |
Output is correct |
67 |
Correct |
147 ms |
28008 KB |
Output is correct |
68 |
Correct |
386 ms |
28156 KB |
Output is correct |
69 |
Correct |
383 ms |
28152 KB |
Output is correct |
70 |
Correct |
358 ms |
27512 KB |
Output is correct |