#include <bits/stdc++.h>
using namespace std ;
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if(x > y) return x = y, true ;
return false ;
}
const int inf = 1e9 ;
const int lim = 1e6 + 5 ;
struct E {
int u, v, w ;
E() {}
E(int a, int b, int c): u(a), v(b), w(c) {}
};
int g[lim], child[lim], n, k, res = inf ;
vector<int> adj[lim] ;
bitset<lim> del ;
E e[lim] ;
int other(int i, int u) {
return e[i].u ^ e[i].v ^ u ;
}
void dfs(int u, int par) {
child[u] = 1 ;
for(int i : adj[u]) {
int v = other(i, u) ;
if(!del[v] && v != par) {
dfs(v, u) ;
child[u] += child[v] ;
}
}
}
int find_centroid(int u, int par, int cur) {
for(int i : adj[u]) {
int v = other(i, u) ;
if(!del[v] && v != par && child[v] > (cur >> 1)) {
return find_centroid(v, u, cur) ;
}
}
return u ;
}
void dfs_calc(int u, int par, int h, int len) {
if(len > k) return ;
minimize(res, h + g[k - len]) ;
for(int i : adj[u]) {
int v = other(i, u) ;
if(!del[v] && v != par) {
dfs_calc(v, u, h + 1, len + e[i].w) ;
}
}
}
void dfs_up(int u, int par, int h, int len, vector<int> &tmp) {
if(len > k) return ;
tmp.push_back(len) ;
minimize(g[len], h) ;
for(int i : adj[u]) {
int v = other(i, u) ;
if(!del[v] && v != par) {
dfs_up(v, u, h + 1, len + e[i].w, tmp) ;
}
}
}
void decomp(int root) {
// cerr << root << ' ' ;
dfs(root, 0) ;
root = find_centroid(root, 0, child[root]) ;
vector<int> tmp ;
for(int i : adj[root]) {
int v = other(i, root) ;
if(!del[v]) {
dfs_calc(v, root, 1, e[i].w) ;
dfs_up(v, root, 1, e[i].w, tmp) ;
}
}
del.set(root) ;
for(int k : tmp) {
g[k] = inf ;
}
for(int i : adj[root]) {
int v = other(i, root) ;
if(!del[v]) {
decomp(v) ;
}
}
}
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, v = H[i][1] + 1 ;
e[i] = E(u, v, L[i]) ;
adj[u].push_back(i) ;
adj[v].push_back(i) ;
}
for(int i = 1; i <= lim; i ++) {
g[i] = inf ;
}
decomp(1) ;
return res == inf ? -1 : res ;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:107:14: warning: iteration 1000004 invokes undefined behavior [-Waggressive-loop-optimizations]
107 | g[i] = inf ;
| ~~~~~^~~~~
race.cpp:106:22: note: within this loop
106 | for(int i = 1; i <= lim; i ++) {
| ~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28764 KB |
Output is correct |
2 |
Correct |
11 ms |
28764 KB |
Output is correct |
3 |
Correct |
13 ms |
28760 KB |
Output is correct |
4 |
Correct |
8 ms |
28908 KB |
Output is correct |
5 |
Correct |
10 ms |
29012 KB |
Output is correct |
6 |
Correct |
7 ms |
28764 KB |
Output is correct |
7 |
Correct |
8 ms |
28760 KB |
Output is correct |
8 |
Correct |
8 ms |
28760 KB |
Output is correct |
9 |
Correct |
8 ms |
28972 KB |
Output is correct |
10 |
Correct |
8 ms |
28764 KB |
Output is correct |
11 |
Correct |
8 ms |
28764 KB |
Output is correct |
12 |
Correct |
7 ms |
28764 KB |
Output is correct |
13 |
Correct |
11 ms |
28760 KB |
Output is correct |
14 |
Correct |
8 ms |
28880 KB |
Output is correct |
15 |
Correct |
10 ms |
28760 KB |
Output is correct |
16 |
Correct |
8 ms |
28760 KB |
Output is correct |
17 |
Correct |
8 ms |
28764 KB |
Output is correct |
18 |
Correct |
8 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28764 KB |
Output is correct |
2 |
Correct |
11 ms |
28764 KB |
Output is correct |
3 |
Correct |
13 ms |
28760 KB |
Output is correct |
4 |
Correct |
8 ms |
28908 KB |
Output is correct |
5 |
Correct |
10 ms |
29012 KB |
Output is correct |
6 |
Correct |
7 ms |
28764 KB |
Output is correct |
7 |
Correct |
8 ms |
28760 KB |
Output is correct |
8 |
Correct |
8 ms |
28760 KB |
Output is correct |
9 |
Correct |
8 ms |
28972 KB |
Output is correct |
10 |
Correct |
8 ms |
28764 KB |
Output is correct |
11 |
Correct |
8 ms |
28764 KB |
Output is correct |
12 |
Correct |
7 ms |
28764 KB |
Output is correct |
13 |
Correct |
11 ms |
28760 KB |
Output is correct |
14 |
Correct |
8 ms |
28880 KB |
Output is correct |
15 |
Correct |
10 ms |
28760 KB |
Output is correct |
16 |
Correct |
8 ms |
28760 KB |
Output is correct |
17 |
Correct |
8 ms |
28764 KB |
Output is correct |
18 |
Correct |
8 ms |
29016 KB |
Output is correct |
19 |
Correct |
9 ms |
29008 KB |
Output is correct |
20 |
Correct |
9 ms |
28760 KB |
Output is correct |
21 |
Correct |
9 ms |
29020 KB |
Output is correct |
22 |
Correct |
8 ms |
29020 KB |
Output is correct |
23 |
Correct |
9 ms |
29020 KB |
Output is correct |
24 |
Correct |
11 ms |
31032 KB |
Output is correct |
25 |
Correct |
9 ms |
29020 KB |
Output is correct |
26 |
Correct |
7 ms |
28924 KB |
Output is correct |
27 |
Correct |
11 ms |
29048 KB |
Output is correct |
28 |
Correct |
10 ms |
30812 KB |
Output is correct |
29 |
Correct |
9 ms |
28924 KB |
Output is correct |
30 |
Correct |
12 ms |
28964 KB |
Output is correct |
31 |
Correct |
10 ms |
28872 KB |
Output is correct |
32 |
Correct |
12 ms |
31068 KB |
Output is correct |
33 |
Correct |
12 ms |
29020 KB |
Output is correct |
34 |
Correct |
10 ms |
28924 KB |
Output is correct |
35 |
Correct |
11 ms |
27936 KB |
Output is correct |
36 |
Correct |
11 ms |
27796 KB |
Output is correct |
37 |
Correct |
14 ms |
27756 KB |
Output is correct |
38 |
Correct |
11 ms |
27748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28764 KB |
Output is correct |
2 |
Correct |
11 ms |
28764 KB |
Output is correct |
3 |
Correct |
13 ms |
28760 KB |
Output is correct |
4 |
Correct |
8 ms |
28908 KB |
Output is correct |
5 |
Correct |
10 ms |
29012 KB |
Output is correct |
6 |
Correct |
7 ms |
28764 KB |
Output is correct |
7 |
Correct |
8 ms |
28760 KB |
Output is correct |
8 |
Correct |
8 ms |
28760 KB |
Output is correct |
9 |
Correct |
8 ms |
28972 KB |
Output is correct |
10 |
Correct |
8 ms |
28764 KB |
Output is correct |
11 |
Correct |
8 ms |
28764 KB |
Output is correct |
12 |
Correct |
7 ms |
28764 KB |
Output is correct |
13 |
Correct |
11 ms |
28760 KB |
Output is correct |
14 |
Correct |
8 ms |
28880 KB |
Output is correct |
15 |
Correct |
10 ms |
28760 KB |
Output is correct |
16 |
Correct |
8 ms |
28760 KB |
Output is correct |
17 |
Correct |
8 ms |
28764 KB |
Output is correct |
18 |
Correct |
8 ms |
29016 KB |
Output is correct |
19 |
Correct |
116 ms |
35156 KB |
Output is correct |
20 |
Correct |
108 ms |
35236 KB |
Output is correct |
21 |
Correct |
116 ms |
36660 KB |
Output is correct |
22 |
Correct |
89 ms |
37076 KB |
Output is correct |
23 |
Correct |
59 ms |
42676 KB |
Output is correct |
24 |
Correct |
66 ms |
36320 KB |
Output is correct |
25 |
Correct |
88 ms |
43088 KB |
Output is correct |
26 |
Correct |
84 ms |
43760 KB |
Output is correct |
27 |
Correct |
119 ms |
43096 KB |
Output is correct |
28 |
Correct |
176 ms |
59200 KB |
Output is correct |
29 |
Correct |
196 ms |
61524 KB |
Output is correct |
30 |
Correct |
131 ms |
44116 KB |
Output is correct |
31 |
Correct |
119 ms |
44088 KB |
Output is correct |
32 |
Correct |
142 ms |
44268 KB |
Output is correct |
33 |
Correct |
131 ms |
43736 KB |
Output is correct |
34 |
Correct |
107 ms |
44640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
28764 KB |
Output is correct |
2 |
Correct |
11 ms |
28764 KB |
Output is correct |
3 |
Correct |
13 ms |
28760 KB |
Output is correct |
4 |
Correct |
8 ms |
28908 KB |
Output is correct |
5 |
Correct |
10 ms |
29012 KB |
Output is correct |
6 |
Correct |
7 ms |
28764 KB |
Output is correct |
7 |
Correct |
8 ms |
28760 KB |
Output is correct |
8 |
Correct |
8 ms |
28760 KB |
Output is correct |
9 |
Correct |
8 ms |
28972 KB |
Output is correct |
10 |
Correct |
8 ms |
28764 KB |
Output is correct |
11 |
Correct |
8 ms |
28764 KB |
Output is correct |
12 |
Correct |
7 ms |
28764 KB |
Output is correct |
13 |
Correct |
11 ms |
28760 KB |
Output is correct |
14 |
Correct |
8 ms |
28880 KB |
Output is correct |
15 |
Correct |
10 ms |
28760 KB |
Output is correct |
16 |
Correct |
8 ms |
28760 KB |
Output is correct |
17 |
Correct |
8 ms |
28764 KB |
Output is correct |
18 |
Correct |
8 ms |
29016 KB |
Output is correct |
19 |
Correct |
9 ms |
29008 KB |
Output is correct |
20 |
Correct |
9 ms |
28760 KB |
Output is correct |
21 |
Correct |
9 ms |
29020 KB |
Output is correct |
22 |
Correct |
8 ms |
29020 KB |
Output is correct |
23 |
Correct |
9 ms |
29020 KB |
Output is correct |
24 |
Correct |
11 ms |
31032 KB |
Output is correct |
25 |
Correct |
9 ms |
29020 KB |
Output is correct |
26 |
Correct |
7 ms |
28924 KB |
Output is correct |
27 |
Correct |
11 ms |
29048 KB |
Output is correct |
28 |
Correct |
10 ms |
30812 KB |
Output is correct |
29 |
Correct |
9 ms |
28924 KB |
Output is correct |
30 |
Correct |
12 ms |
28964 KB |
Output is correct |
31 |
Correct |
10 ms |
28872 KB |
Output is correct |
32 |
Correct |
12 ms |
31068 KB |
Output is correct |
33 |
Correct |
12 ms |
29020 KB |
Output is correct |
34 |
Correct |
10 ms |
28924 KB |
Output is correct |
35 |
Correct |
11 ms |
27936 KB |
Output is correct |
36 |
Correct |
11 ms |
27796 KB |
Output is correct |
37 |
Correct |
14 ms |
27756 KB |
Output is correct |
38 |
Correct |
11 ms |
27748 KB |
Output is correct |
39 |
Correct |
116 ms |
35156 KB |
Output is correct |
40 |
Correct |
108 ms |
35236 KB |
Output is correct |
41 |
Correct |
116 ms |
36660 KB |
Output is correct |
42 |
Correct |
89 ms |
37076 KB |
Output is correct |
43 |
Correct |
59 ms |
42676 KB |
Output is correct |
44 |
Correct |
66 ms |
36320 KB |
Output is correct |
45 |
Correct |
88 ms |
43088 KB |
Output is correct |
46 |
Correct |
84 ms |
43760 KB |
Output is correct |
47 |
Correct |
119 ms |
43096 KB |
Output is correct |
48 |
Correct |
176 ms |
59200 KB |
Output is correct |
49 |
Correct |
196 ms |
61524 KB |
Output is correct |
50 |
Correct |
131 ms |
44116 KB |
Output is correct |
51 |
Correct |
119 ms |
44088 KB |
Output is correct |
52 |
Correct |
142 ms |
44268 KB |
Output is correct |
53 |
Correct |
131 ms |
43736 KB |
Output is correct |
54 |
Correct |
107 ms |
44640 KB |
Output is correct |
55 |
Correct |
17 ms |
29788 KB |
Output is correct |
56 |
Correct |
14 ms |
29812 KB |
Output is correct |
57 |
Correct |
88 ms |
35844 KB |
Output is correct |
58 |
Correct |
27 ms |
35784 KB |
Output is correct |
59 |
Correct |
82 ms |
43704 KB |
Output is correct |
60 |
Correct |
294 ms |
62396 KB |
Output is correct |
61 |
Correct |
123 ms |
43604 KB |
Output is correct |
62 |
Correct |
157 ms |
48588 KB |
Output is correct |
63 |
Correct |
193 ms |
44252 KB |
Output is correct |
64 |
Correct |
312 ms |
44744 KB |
Output is correct |
65 |
Correct |
141 ms |
44656 KB |
Output is correct |
66 |
Correct |
197 ms |
55748 KB |
Output is correct |
67 |
Correct |
69 ms |
46276 KB |
Output is correct |
68 |
Correct |
162 ms |
46080 KB |
Output is correct |
69 |
Correct |
170 ms |
45124 KB |
Output is correct |
70 |
Correct |
195 ms |
44236 KB |
Output is correct |