#include <bits/stdc++.h>
#include "race.h"
typedef long long ll;
#define F first
#define S second
using namespace std;
using pii = pair<int,int>;
const int N = 2e5 + 5, M = 1e6 + 5, inf = 1e8;
int k, mn[M], ans = inf, sz[N];
set<int> s;
vector<pii> g[N];
bool ded[N];
void dfs(int u, int p, int d, ll w){
if(w == k) ans = min(ans, d);
if(w < k) ans = min(ans, d + mn[k - w]);
for(pii it : g[u]) if(it.S != p && !ded[it.S]) dfs(it.S, u, d + 1, w + it.F);
}
void efs(int u, int p, int d, ll w){
if(w <= k) mn[w] = min(mn[w], d), s.insert(w);
for(pii it : g[u]) if(it.S != p && !ded[it.S]) efs(it.S, u, d + 1, w + it.F);
}
int fsz(int u, int p){
sz[u] = 1;
for(pii it: g[u]) if(it.S != p && !ded[it.S]) sz[u] += fsz(it.S, u);
return sz[u];
}
int fcen(int u, int p, int tsz){
for(pii it: g[u]) if(it.S != p && !ded[it.S] && sz[it.S] > tsz/2) return fcen(it.S, u, tsz); return u;
}
void cd(int u){
ded[u = fcen(u, -1, fsz(u, -1))] = true; s.clear();
for(pii it : g[u]) if(!ded[it.S]) dfs(it.S, u, 1, it.F), efs(it.S, u, 1, it.F);
for(int w : s) mn[w] = inf;
for(pii it: g[u]) if(!ded[it.S]) cd(it.S);
}
int best_path(int n, int K, int e[][2], int l[]){
for(int i = 0; i<n; ++i) g[i].clear();
k = K;
for(int i = 0; i<n-1; ++i){
int u = e[i][0], v = e[i][1], w = l[i];
g[u].emplace_back(w, v); g[v].emplace_back(w, u);
}
fill(sz, sz+n, 0); fill(ded, ded+n, false); fill(mn, mn+k+1, ans = inf);
cd(0);
return (ans >= inf)? -1 : ans;
}
/*
signed main(){
int n, K; scanf("%d %d", &n, &K);
int e[n][2], l[n];
for(int i = 0, u, v; i<n-1; ++i) scanf("%d %d", &e[i][0], &e[i][1]);
for(int i = 0; i<n-1; ++i) scanf("%d", l+i);
printf("%d\n", best_path(n, K, e, l));
}
*/
Compilation message
race.cpp: In function 'int fcen(int, int, int)':
race.cpp:29:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
29 | for(pii it: g[u]) if(it.S != p && !ded[it.S] && sz[it.S] > tsz/2) return fcen(it.S, u, tsz); return u;
| ^~~
race.cpp:29:95: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
29 | for(pii it: g[u]) if(it.S != p && !ded[it.S] && sz[it.S] > tsz/2) return fcen(it.S, u, tsz); return u;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5148 KB |
Output is correct |
5 |
Correct |
2 ms |
5152 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
5180 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5148 KB |
Output is correct |
5 |
Correct |
2 ms |
5152 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
5180 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
4 ms |
5212 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
4 ms |
8080 KB |
Output is correct |
24 |
Correct |
5 ms |
8484 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
5980 KB |
Output is correct |
29 |
Correct |
4 ms |
6492 KB |
Output is correct |
30 |
Correct |
4 ms |
6492 KB |
Output is correct |
31 |
Correct |
4 ms |
7768 KB |
Output is correct |
32 |
Correct |
5 ms |
8024 KB |
Output is correct |
33 |
Correct |
5 ms |
8284 KB |
Output is correct |
34 |
Correct |
4 ms |
7516 KB |
Output is correct |
35 |
Correct |
5 ms |
8284 KB |
Output is correct |
36 |
Correct |
5 ms |
8792 KB |
Output is correct |
37 |
Correct |
5 ms |
8284 KB |
Output is correct |
38 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5148 KB |
Output is correct |
5 |
Correct |
2 ms |
5152 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
5180 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
121 ms |
11820 KB |
Output is correct |
20 |
Correct |
115 ms |
11812 KB |
Output is correct |
21 |
Correct |
114 ms |
11840 KB |
Output is correct |
22 |
Correct |
111 ms |
11860 KB |
Output is correct |
23 |
Correct |
71 ms |
12112 KB |
Output is correct |
24 |
Correct |
47 ms |
11856 KB |
Output is correct |
25 |
Correct |
111 ms |
15188 KB |
Output is correct |
26 |
Correct |
68 ms |
17492 KB |
Output is correct |
27 |
Correct |
125 ms |
18772 KB |
Output is correct |
28 |
Correct |
251 ms |
30140 KB |
Output is correct |
29 |
Correct |
248 ms |
29268 KB |
Output is correct |
30 |
Correct |
133 ms |
18724 KB |
Output is correct |
31 |
Correct |
127 ms |
18772 KB |
Output is correct |
32 |
Correct |
150 ms |
19024 KB |
Output is correct |
33 |
Correct |
211 ms |
17748 KB |
Output is correct |
34 |
Correct |
178 ms |
18516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5148 KB |
Output is correct |
5 |
Correct |
2 ms |
5152 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5208 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
5180 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
4 ms |
5212 KB |
Output is correct |
22 |
Correct |
4 ms |
8796 KB |
Output is correct |
23 |
Correct |
4 ms |
8080 KB |
Output is correct |
24 |
Correct |
5 ms |
8484 KB |
Output is correct |
25 |
Correct |
4 ms |
8540 KB |
Output is correct |
26 |
Correct |
4 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
4 ms |
5980 KB |
Output is correct |
29 |
Correct |
4 ms |
6492 KB |
Output is correct |
30 |
Correct |
4 ms |
6492 KB |
Output is correct |
31 |
Correct |
4 ms |
7768 KB |
Output is correct |
32 |
Correct |
5 ms |
8024 KB |
Output is correct |
33 |
Correct |
5 ms |
8284 KB |
Output is correct |
34 |
Correct |
4 ms |
7516 KB |
Output is correct |
35 |
Correct |
5 ms |
8284 KB |
Output is correct |
36 |
Correct |
5 ms |
8792 KB |
Output is correct |
37 |
Correct |
5 ms |
8284 KB |
Output is correct |
38 |
Correct |
4 ms |
7004 KB |
Output is correct |
39 |
Correct |
121 ms |
11820 KB |
Output is correct |
40 |
Correct |
115 ms |
11812 KB |
Output is correct |
41 |
Correct |
114 ms |
11840 KB |
Output is correct |
42 |
Correct |
111 ms |
11860 KB |
Output is correct |
43 |
Correct |
71 ms |
12112 KB |
Output is correct |
44 |
Correct |
47 ms |
11856 KB |
Output is correct |
45 |
Correct |
111 ms |
15188 KB |
Output is correct |
46 |
Correct |
68 ms |
17492 KB |
Output is correct |
47 |
Correct |
125 ms |
18772 KB |
Output is correct |
48 |
Correct |
251 ms |
30140 KB |
Output is correct |
49 |
Correct |
248 ms |
29268 KB |
Output is correct |
50 |
Correct |
133 ms |
18724 KB |
Output is correct |
51 |
Correct |
127 ms |
18772 KB |
Output is correct |
52 |
Correct |
150 ms |
19024 KB |
Output is correct |
53 |
Correct |
211 ms |
17748 KB |
Output is correct |
54 |
Correct |
178 ms |
18516 KB |
Output is correct |
55 |
Correct |
12 ms |
5980 KB |
Output is correct |
56 |
Correct |
9 ms |
5724 KB |
Output is correct |
57 |
Correct |
77 ms |
11944 KB |
Output is correct |
58 |
Correct |
37 ms |
11720 KB |
Output is correct |
59 |
Correct |
150 ms |
21552 KB |
Output is correct |
60 |
Correct |
508 ms |
41812 KB |
Output is correct |
61 |
Correct |
165 ms |
19284 KB |
Output is correct |
62 |
Correct |
171 ms |
23056 KB |
Output is correct |
63 |
Correct |
193 ms |
22868 KB |
Output is correct |
64 |
Correct |
522 ms |
25684 KB |
Output is correct |
65 |
Correct |
165 ms |
19792 KB |
Output is correct |
66 |
Correct |
425 ms |
31784 KB |
Output is correct |
67 |
Correct |
67 ms |
23492 KB |
Output is correct |
68 |
Correct |
244 ms |
26992 KB |
Output is correct |
69 |
Correct |
259 ms |
27080 KB |
Output is correct |
70 |
Correct |
222 ms |
26452 KB |
Output is correct |