#include "race.h"
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<pair<int,int>> AdjList[200005];
bitset<200005> marked;
int sz[200005];
int getSz(int u, int p) {
sz[u] = 1;
for(auto v: AdjList[u]) {
if(v.first==p) continue;
if(marked[v.first]) continue;
sz[u] += getSz(v.first, u);
}
return sz[u];
}
int getCentroid(int u, int p, int compSz) {
if(sz[u]*2 < compSz)
return -1;
for(auto v: AdjList[u]) {
if(v.first==p) continue;
if(marked[v.first]) continue;
if(sz[v.first]*2 > compSz)
return getCentroid(v.first, u, compSz);
}
return u;
}
int dp[1000005];
int ans = 1<<29;
void checkSecondLeg(int u, int p, int d, int l) {
ans = min(ans, l + dp[k-d]);
for(auto v: AdjList[u]) {
if(v.first==p) continue;
if(marked[v.first]) continue;
if(d+v.second > k) continue;
checkSecondLeg(v.first, u, d+v.second, l+1);
}
}
void markFirstLeg(int u, int p, int d, int l) {
dp[d] = min(dp[d], l);
for(auto v: AdjList[u]) {
if(v.first==p) continue;
if(marked[v.first]) continue;
if(d+v.second > k) continue;
markFirstLeg(v.first, u, d+v.second, l+1);
}
}
void unmarkFirstLeg(int u, int p, int d, int l) {
dp[d] = 1<<29;
for(auto v: AdjList[u]) {
if(v.first==p) continue;
if(marked[v.first]) continue;
if(d+v.second > k) continue;
unmarkFirstLeg(v.first, u, d+v.second, l+1);
}
}
bitset<200005> vis;
void markVis(int u, int p) {
vis[u] = 1;
for(auto v: AdjList[u]) {
if(v.first == p)
continue;
if(marked[v.first])
continue;
if(vis[v.first])
continue;
markVis(v.first, u);
}
}
void unmarkVis(int u, int p) {
vis[u] = 0;
for(auto v: AdjList[u]) {
if(v.first == p)
continue;
if(marked[v.first])
continue;
unmarkVis(v.first, u);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
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]});
}
ans = 1<<29;
for(int i = 1; i <= K; i++)
dp[i] = 1<<29;
dp[0] = 0;
queue<int> q;
q.push(0);
while(!q.empty()) {
int u = q.front();
q.pop();
getSz(u, -1);
int centre = getCentroid(u, -1, sz[u]);
for(auto v: AdjList[centre]) {
if(marked[v.first])
continue;
if(v.second > k) continue;
checkSecondLeg(v.first, centre, v.second, 1);
markFirstLeg(v.first, centre, v.second, 1);
}
for(auto v: AdjList[centre]) {
if(marked[v.first])
continue;
if(v.second > k) continue;
unmarkFirstLeg(v.first, centre, v.second, 1);
}
dp[0] = 0;
for(auto v: AdjList[centre]) {
if(marked[v.first])
continue;
// if(vis[v.first])
// continue;
q.push(v.first);
}
marked[centre]=1;
}
if(ans == 1<<29)
ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4988 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4988 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8696 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
10 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
8 ms |
6392 KB |
Output is correct |
27 |
Correct |
12 ms |
8248 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
8 ms |
6436 KB |
Output is correct |
30 |
Correct |
8 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7672 KB |
Output is correct |
32 |
Correct |
10 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
8184 KB |
Output is correct |
34 |
Correct |
9 ms |
7416 KB |
Output is correct |
35 |
Correct |
10 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8824 KB |
Output is correct |
37 |
Correct |
9 ms |
8184 KB |
Output is correct |
38 |
Correct |
8 ms |
7032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4988 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
182 ms |
11716 KB |
Output is correct |
20 |
Correct |
243 ms |
11932 KB |
Output is correct |
21 |
Correct |
194 ms |
11772 KB |
Output is correct |
22 |
Correct |
175 ms |
11768 KB |
Output is correct |
23 |
Correct |
234 ms |
12192 KB |
Output is correct |
24 |
Correct |
78 ms |
12152 KB |
Output is correct |
25 |
Correct |
294 ms |
14584 KB |
Output is correct |
26 |
Correct |
141 ms |
17528 KB |
Output is correct |
27 |
Correct |
364 ms |
18836 KB |
Output is correct |
28 |
Correct |
600 ms |
30456 KB |
Output is correct |
29 |
Correct |
500 ms |
29432 KB |
Output is correct |
30 |
Correct |
255 ms |
18936 KB |
Output is correct |
31 |
Correct |
273 ms |
18996 KB |
Output is correct |
32 |
Correct |
381 ms |
18936 KB |
Output is correct |
33 |
Correct |
520 ms |
17916 KB |
Output is correct |
34 |
Correct |
439 ms |
18680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
7 ms |
4984 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
7 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4988 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
5112 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
7 ms |
4984 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
6 ms |
4984 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8696 KB |
Output is correct |
23 |
Correct |
9 ms |
8056 KB |
Output is correct |
24 |
Correct |
10 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
8 ms |
6392 KB |
Output is correct |
27 |
Correct |
12 ms |
8248 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
8 ms |
6436 KB |
Output is correct |
30 |
Correct |
8 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7672 KB |
Output is correct |
32 |
Correct |
10 ms |
7928 KB |
Output is correct |
33 |
Correct |
10 ms |
8184 KB |
Output is correct |
34 |
Correct |
9 ms |
7416 KB |
Output is correct |
35 |
Correct |
10 ms |
8312 KB |
Output is correct |
36 |
Correct |
10 ms |
8824 KB |
Output is correct |
37 |
Correct |
9 ms |
8184 KB |
Output is correct |
38 |
Correct |
8 ms |
7032 KB |
Output is correct |
39 |
Correct |
182 ms |
11716 KB |
Output is correct |
40 |
Correct |
243 ms |
11932 KB |
Output is correct |
41 |
Correct |
194 ms |
11772 KB |
Output is correct |
42 |
Correct |
175 ms |
11768 KB |
Output is correct |
43 |
Correct |
234 ms |
12192 KB |
Output is correct |
44 |
Correct |
78 ms |
12152 KB |
Output is correct |
45 |
Correct |
294 ms |
14584 KB |
Output is correct |
46 |
Correct |
141 ms |
17528 KB |
Output is correct |
47 |
Correct |
364 ms |
18836 KB |
Output is correct |
48 |
Correct |
600 ms |
30456 KB |
Output is correct |
49 |
Correct |
500 ms |
29432 KB |
Output is correct |
50 |
Correct |
255 ms |
18936 KB |
Output is correct |
51 |
Correct |
273 ms |
18996 KB |
Output is correct |
52 |
Correct |
381 ms |
18936 KB |
Output is correct |
53 |
Correct |
520 ms |
17916 KB |
Output is correct |
54 |
Correct |
439 ms |
18680 KB |
Output is correct |
55 |
Correct |
18 ms |
5752 KB |
Output is correct |
56 |
Correct |
20 ms |
5624 KB |
Output is correct |
57 |
Correct |
106 ms |
12152 KB |
Output is correct |
58 |
Correct |
48 ms |
12012 KB |
Output is correct |
59 |
Correct |
136 ms |
18200 KB |
Output is correct |
60 |
Correct |
966 ms |
33556 KB |
Output is correct |
61 |
Correct |
298 ms |
19168 KB |
Output is correct |
62 |
Correct |
340 ms |
22904 KB |
Output is correct |
63 |
Correct |
630 ms |
23032 KB |
Output is correct |
64 |
Correct |
868 ms |
21240 KB |
Output is correct |
65 |
Correct |
364 ms |
19704 KB |
Output is correct |
66 |
Correct |
816 ms |
31444 KB |
Output is correct |
67 |
Correct |
147 ms |
24168 KB |
Output is correct |
68 |
Correct |
432 ms |
23816 KB |
Output is correct |
69 |
Correct |
446 ms |
24156 KB |
Output is correct |
70 |
Correct |
402 ms |
23424 KB |
Output is correct |