#include<bits/stdc++.h>
#include "race.h"
using namespace std;
constexpr int MAXN = 2*1e5 + 5, MAXK = 1e6 + 5, INF = 1e8;
bool visited[MAXN];
int c[MAXN], k, mini[MAXK];
vector<int> edges[MAXN], w[MAXN];
int cnt(int id){
visited[id] = 1;
c[id] = 1;
for(int v : edges[id]) if(!visited[v]) c[id] += cnt(v);
visited[id] = 0;
return c[id];
}
int init(int id, int n, int p = -1){
for(int v : edges[id]) if(c[v] > n/2 && !visited[v] && p != v) return init(v, n, id);
return id;
}
int centroid(int id){
return init(id, cnt(id));
}
int calc(int id, int len, int lvl = 1, int p = -1){
int ret = INF;
if(len <= k) ret = min(ret, mini[k-len] + lvl);
for(int i = 0; i < edges[id].size(); i++){
int v = edges[id][i];
if(!visited[v] && v != p) ret = min(ret, calc(v, min(INF, len + w[id][i]), lvl + 1, id));
}
return ret;
}
void add(int id, int len, int lvl = 1, int p = -1){
if(len < MAXK) mini[len] = min(mini[len], lvl);
for(int i = 0; i < edges[id].size(); i++){
int v = edges[id][i];
if(!visited[v] && p != v) add(v, min(INF, len + w[id][i]), lvl + 1, id);
}
}
void res(int id, int len = 0, int p = -1){
if(len < MAXK) mini[len] = INF;
for(int i = 0; i < edges[id].size(); i++){
int v = edges[id][i];
if(!visited[v] && p != v) res(v, min(INF, len + w[id][i]), id);
}
}
int db(int id){
int ret = INF;
mini[0] = 0;
for(int i = 0; i < edges[id].size(); i++){
int v = edges[id][i];
if(visited[v]) continue;
ret = min(ret, calc(v, w[id][i], 1, id));
add(v, w[id][i], 1, id);
}
res(id);
return ret;
}
int process(int id){
int ret = db(id);
visited[id] = 1;
for(int v : edges[id]) if(!visited[v]) ret = min(ret, process(centroid(v)));
return ret;
}
int best_path(int N, int K, int H[][2], int L[])
{
fill(mini, mini + MAXK, INF);
k = K;
for(int i = 0; i < N-1; i++){
edges[H[i][0]].push_back(H[i][1]);
w[H[i][0]].push_back(L[i]);
swap(H[i][0], H[i][1]);
edges[H[i][0]].push_back(H[i][1]);
w[H[i][0]].push_back(L[i]);
}
int ret = process(centroid(0));
return (ret == INF ? -1 : ret);
}
Compilation message
race.cpp: In function 'int calc(int, int, int, int)':
race.cpp:31:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i = 0; i < edges[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
race.cpp: In function 'void add(int, int, int, int)':
race.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < edges[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
race.cpp: In function 'void res(int, int, int)':
race.cpp:48:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i = 0; i < edges[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
race.cpp: In function 'int db(int)':
race.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < edges[id].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13676 KB |
Output is correct |
2 |
Correct |
10 ms |
13628 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13628 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
10 ms |
13676 KB |
Output is correct |
8 |
Correct |
12 ms |
13676 KB |
Output is correct |
9 |
Correct |
11 ms |
13676 KB |
Output is correct |
10 |
Correct |
12 ms |
13676 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
12 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13688 KB |
Output is correct |
16 |
Correct |
9 ms |
13676 KB |
Output is correct |
17 |
Correct |
9 ms |
13676 KB |
Output is correct |
18 |
Correct |
11 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13676 KB |
Output is correct |
2 |
Correct |
10 ms |
13628 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13628 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
10 ms |
13676 KB |
Output is correct |
8 |
Correct |
12 ms |
13676 KB |
Output is correct |
9 |
Correct |
11 ms |
13676 KB |
Output is correct |
10 |
Correct |
12 ms |
13676 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
12 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13688 KB |
Output is correct |
16 |
Correct |
9 ms |
13676 KB |
Output is correct |
17 |
Correct |
9 ms |
13676 KB |
Output is correct |
18 |
Correct |
11 ms |
13676 KB |
Output is correct |
19 |
Correct |
8 ms |
13620 KB |
Output is correct |
20 |
Correct |
11 ms |
13676 KB |
Output is correct |
21 |
Correct |
13 ms |
13684 KB |
Output is correct |
22 |
Correct |
10 ms |
13804 KB |
Output is correct |
23 |
Correct |
10 ms |
13804 KB |
Output is correct |
24 |
Correct |
10 ms |
13804 KB |
Output is correct |
25 |
Correct |
11 ms |
13804 KB |
Output is correct |
26 |
Correct |
14 ms |
13804 KB |
Output is correct |
27 |
Correct |
10 ms |
13804 KB |
Output is correct |
28 |
Correct |
12 ms |
13804 KB |
Output is correct |
29 |
Correct |
10 ms |
13804 KB |
Output is correct |
30 |
Correct |
10 ms |
13804 KB |
Output is correct |
31 |
Correct |
10 ms |
13804 KB |
Output is correct |
32 |
Correct |
13 ms |
13804 KB |
Output is correct |
33 |
Correct |
10 ms |
13804 KB |
Output is correct |
34 |
Correct |
10 ms |
13804 KB |
Output is correct |
35 |
Correct |
10 ms |
13804 KB |
Output is correct |
36 |
Correct |
11 ms |
13804 KB |
Output is correct |
37 |
Correct |
13 ms |
13804 KB |
Output is correct |
38 |
Correct |
11 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13676 KB |
Output is correct |
2 |
Correct |
10 ms |
13628 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13628 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
10 ms |
13676 KB |
Output is correct |
8 |
Correct |
12 ms |
13676 KB |
Output is correct |
9 |
Correct |
11 ms |
13676 KB |
Output is correct |
10 |
Correct |
12 ms |
13676 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
12 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13688 KB |
Output is correct |
16 |
Correct |
9 ms |
13676 KB |
Output is correct |
17 |
Correct |
9 ms |
13676 KB |
Output is correct |
18 |
Correct |
11 ms |
13676 KB |
Output is correct |
19 |
Correct |
356 ms |
22124 KB |
Output is correct |
20 |
Correct |
410 ms |
22124 KB |
Output is correct |
21 |
Correct |
351 ms |
22124 KB |
Output is correct |
22 |
Correct |
316 ms |
22252 KB |
Output is correct |
23 |
Correct |
247 ms |
21976 KB |
Output is correct |
24 |
Correct |
142 ms |
22380 KB |
Output is correct |
25 |
Correct |
322 ms |
25964 KB |
Output is correct |
26 |
Correct |
143 ms |
26732 KB |
Output is correct |
27 |
Correct |
484 ms |
31084 KB |
Output is correct |
28 |
Correct |
1093 ms |
39276 KB |
Output is correct |
29 |
Correct |
880 ms |
38596 KB |
Output is correct |
30 |
Correct |
387 ms |
31084 KB |
Output is correct |
31 |
Correct |
371 ms |
31160 KB |
Output is correct |
32 |
Correct |
510 ms |
31128 KB |
Output is correct |
33 |
Correct |
660 ms |
30044 KB |
Output is correct |
34 |
Correct |
628 ms |
30084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13676 KB |
Output is correct |
2 |
Correct |
10 ms |
13628 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13628 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
10 ms |
13676 KB |
Output is correct |
8 |
Correct |
12 ms |
13676 KB |
Output is correct |
9 |
Correct |
11 ms |
13676 KB |
Output is correct |
10 |
Correct |
12 ms |
13676 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
12 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13688 KB |
Output is correct |
16 |
Correct |
9 ms |
13676 KB |
Output is correct |
17 |
Correct |
9 ms |
13676 KB |
Output is correct |
18 |
Correct |
11 ms |
13676 KB |
Output is correct |
19 |
Correct |
8 ms |
13620 KB |
Output is correct |
20 |
Correct |
11 ms |
13676 KB |
Output is correct |
21 |
Correct |
13 ms |
13684 KB |
Output is correct |
22 |
Correct |
10 ms |
13804 KB |
Output is correct |
23 |
Correct |
10 ms |
13804 KB |
Output is correct |
24 |
Correct |
10 ms |
13804 KB |
Output is correct |
25 |
Correct |
11 ms |
13804 KB |
Output is correct |
26 |
Correct |
14 ms |
13804 KB |
Output is correct |
27 |
Correct |
10 ms |
13804 KB |
Output is correct |
28 |
Correct |
12 ms |
13804 KB |
Output is correct |
29 |
Correct |
10 ms |
13804 KB |
Output is correct |
30 |
Correct |
10 ms |
13804 KB |
Output is correct |
31 |
Correct |
10 ms |
13804 KB |
Output is correct |
32 |
Correct |
13 ms |
13804 KB |
Output is correct |
33 |
Correct |
10 ms |
13804 KB |
Output is correct |
34 |
Correct |
10 ms |
13804 KB |
Output is correct |
35 |
Correct |
10 ms |
13804 KB |
Output is correct |
36 |
Correct |
11 ms |
13804 KB |
Output is correct |
37 |
Correct |
13 ms |
13804 KB |
Output is correct |
38 |
Correct |
11 ms |
13676 KB |
Output is correct |
39 |
Correct |
356 ms |
22124 KB |
Output is correct |
40 |
Correct |
410 ms |
22124 KB |
Output is correct |
41 |
Correct |
351 ms |
22124 KB |
Output is correct |
42 |
Correct |
316 ms |
22252 KB |
Output is correct |
43 |
Correct |
247 ms |
21976 KB |
Output is correct |
44 |
Correct |
142 ms |
22380 KB |
Output is correct |
45 |
Correct |
322 ms |
25964 KB |
Output is correct |
46 |
Correct |
143 ms |
26732 KB |
Output is correct |
47 |
Correct |
484 ms |
31084 KB |
Output is correct |
48 |
Correct |
1093 ms |
39276 KB |
Output is correct |
49 |
Correct |
880 ms |
38596 KB |
Output is correct |
50 |
Correct |
387 ms |
31084 KB |
Output is correct |
51 |
Correct |
371 ms |
31160 KB |
Output is correct |
52 |
Correct |
510 ms |
31128 KB |
Output is correct |
53 |
Correct |
660 ms |
30044 KB |
Output is correct |
54 |
Correct |
628 ms |
30084 KB |
Output is correct |
55 |
Correct |
22 ms |
14572 KB |
Output is correct |
56 |
Correct |
22 ms |
14572 KB |
Output is correct |
57 |
Correct |
133 ms |
22124 KB |
Output is correct |
58 |
Correct |
56 ms |
22748 KB |
Output is correct |
59 |
Correct |
140 ms |
26712 KB |
Output is correct |
60 |
Correct |
875 ms |
38868 KB |
Output is correct |
61 |
Correct |
389 ms |
31084 KB |
Output is correct |
62 |
Correct |
377 ms |
31084 KB |
Output is correct |
63 |
Correct |
518 ms |
31212 KB |
Output is correct |
64 |
Correct |
1025 ms |
30188 KB |
Output is correct |
65 |
Correct |
818 ms |
30416 KB |
Output is correct |
66 |
Correct |
910 ms |
37868 KB |
Output is correct |
67 |
Correct |
216 ms |
31444 KB |
Output is correct |
68 |
Correct |
524 ms |
31212 KB |
Output is correct |
69 |
Correct |
573 ms |
31468 KB |
Output is correct |
70 |
Correct |
500 ms |
30444 KB |
Output is correct |