#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) ((x).begin(), (x).end())
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5 + 5;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
int N, sz[MAXN], ans, K;
vector<pii> G[MAXN];
bool del[MAXN];
map<int, int> mp;
int szdfs(int cur, int par) {
sz[cur] = 1;
for(auto i : G[cur]) if(!del[i.fi] && i.fi != par) sz[cur] += szdfs(i.fi, cur);
return sz[cur];
}
int cdfs(int cur, int par, int cap) {
for(auto i : G[cur]) if(!del[i.fi] && i.fi != par && sz[i.fi] > cap) return cdfs(i.fi, cur, cap);
return cur;
}
void dfs(int cur, int par, int dis, int cnt) {
if(dis > K) return;
if(mp.find(K - dis) != mp.end()) ans = min(ans, mp[K - dis] + cnt);
for(auto i : G[cur]) if(!del[i.fi] && i.fi != par) dfs(i.fi, cur, dis + i.se, cnt+1);
}
void add(int cur, int par, int dis, int cnt) {
if(dis > K) return;
if(mp.find(dis) == mp.end()) mp[dis] = cnt;
else mp[dis] = min(mp[dis], cnt);
for(auto i : G[cur]) if(!del[i.fi] && i.fi != par) add(i.fi, cur, dis + i.se, cnt+1);
}
void solve(int cur) {
mp.clear();
mp[0] = 0;
for(auto i : G[cur]) if(!del[i.fi]) dfs(i.fi, cur, i.se, 1), add(i.fi, cur, i.se, 1);
}
void decompose(int root, int par) {
int cap = szdfs(root, 0);
int cen = cdfs(root, 0, cap / 2);
del[cen] = 1;
solve(cen);
for(auto i : G[cen]) if(!del[i.fi]) decompose(i.fi, cen);
}
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 a = H[i][0]+1, b = H[i][1]+1, c = L[i];
G[a].push_back(pii(b, c)); G[b].push_back(pii(a, c));
}
ans = 1e9;
decompose(1, 0);
if(ans == 1e9) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
17 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
7 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
4988 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
4984 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
17 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
7 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
4988 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
4984 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
7 ms |
5112 KB |
Output is correct |
20 |
Correct |
7 ms |
4984 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
7 ms |
5084 KB |
Output is correct |
23 |
Correct |
8 ms |
5124 KB |
Output is correct |
24 |
Correct |
7 ms |
5116 KB |
Output is correct |
25 |
Correct |
9 ms |
5112 KB |
Output is correct |
26 |
Correct |
7 ms |
5112 KB |
Output is correct |
27 |
Correct |
7 ms |
5116 KB |
Output is correct |
28 |
Correct |
8 ms |
5112 KB |
Output is correct |
29 |
Correct |
8 ms |
5112 KB |
Output is correct |
30 |
Correct |
9 ms |
5116 KB |
Output is correct |
31 |
Correct |
9 ms |
5112 KB |
Output is correct |
32 |
Correct |
18 ms |
5140 KB |
Output is correct |
33 |
Correct |
9 ms |
5116 KB |
Output is correct |
34 |
Correct |
8 ms |
5112 KB |
Output is correct |
35 |
Correct |
7 ms |
5112 KB |
Output is correct |
36 |
Correct |
8 ms |
5112 KB |
Output is correct |
37 |
Correct |
8 ms |
5112 KB |
Output is correct |
38 |
Correct |
8 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
17 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
7 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
4988 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
4984 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
293 ms |
11768 KB |
Output is correct |
20 |
Correct |
282 ms |
11768 KB |
Output is correct |
21 |
Correct |
315 ms |
11768 KB |
Output is correct |
22 |
Correct |
292 ms |
11896 KB |
Output is correct |
23 |
Correct |
135 ms |
12024 KB |
Output is correct |
24 |
Correct |
116 ms |
12024 KB |
Output is correct |
25 |
Correct |
219 ms |
13736 KB |
Output is correct |
26 |
Correct |
135 ms |
16804 KB |
Output is correct |
27 |
Correct |
284 ms |
18808 KB |
Output is correct |
28 |
Correct |
451 ms |
27128 KB |
Output is correct |
29 |
Correct |
491 ms |
26364 KB |
Output is correct |
30 |
Correct |
307 ms |
19044 KB |
Output is correct |
31 |
Correct |
298 ms |
18936 KB |
Output is correct |
32 |
Correct |
343 ms |
19064 KB |
Output is correct |
33 |
Correct |
476 ms |
17644 KB |
Output is correct |
34 |
Correct |
338 ms |
18068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
17 ms |
5112 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
7 ms |
4984 KB |
Output is correct |
13 |
Correct |
7 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
7 ms |
4988 KB |
Output is correct |
16 |
Correct |
7 ms |
5112 KB |
Output is correct |
17 |
Correct |
7 ms |
4984 KB |
Output is correct |
18 |
Correct |
6 ms |
5112 KB |
Output is correct |
19 |
Correct |
7 ms |
5112 KB |
Output is correct |
20 |
Correct |
7 ms |
4984 KB |
Output is correct |
21 |
Correct |
8 ms |
5112 KB |
Output is correct |
22 |
Correct |
7 ms |
5084 KB |
Output is correct |
23 |
Correct |
8 ms |
5124 KB |
Output is correct |
24 |
Correct |
7 ms |
5116 KB |
Output is correct |
25 |
Correct |
9 ms |
5112 KB |
Output is correct |
26 |
Correct |
7 ms |
5112 KB |
Output is correct |
27 |
Correct |
7 ms |
5116 KB |
Output is correct |
28 |
Correct |
8 ms |
5112 KB |
Output is correct |
29 |
Correct |
8 ms |
5112 KB |
Output is correct |
30 |
Correct |
9 ms |
5116 KB |
Output is correct |
31 |
Correct |
9 ms |
5112 KB |
Output is correct |
32 |
Correct |
18 ms |
5140 KB |
Output is correct |
33 |
Correct |
9 ms |
5116 KB |
Output is correct |
34 |
Correct |
8 ms |
5112 KB |
Output is correct |
35 |
Correct |
7 ms |
5112 KB |
Output is correct |
36 |
Correct |
8 ms |
5112 KB |
Output is correct |
37 |
Correct |
8 ms |
5112 KB |
Output is correct |
38 |
Correct |
8 ms |
5112 KB |
Output is correct |
39 |
Correct |
293 ms |
11768 KB |
Output is correct |
40 |
Correct |
282 ms |
11768 KB |
Output is correct |
41 |
Correct |
315 ms |
11768 KB |
Output is correct |
42 |
Correct |
292 ms |
11896 KB |
Output is correct |
43 |
Correct |
135 ms |
12024 KB |
Output is correct |
44 |
Correct |
116 ms |
12024 KB |
Output is correct |
45 |
Correct |
219 ms |
13736 KB |
Output is correct |
46 |
Correct |
135 ms |
16804 KB |
Output is correct |
47 |
Correct |
284 ms |
18808 KB |
Output is correct |
48 |
Correct |
451 ms |
27128 KB |
Output is correct |
49 |
Correct |
491 ms |
26364 KB |
Output is correct |
50 |
Correct |
307 ms |
19044 KB |
Output is correct |
51 |
Correct |
298 ms |
18936 KB |
Output is correct |
52 |
Correct |
343 ms |
19064 KB |
Output is correct |
53 |
Correct |
476 ms |
17644 KB |
Output is correct |
54 |
Correct |
338 ms |
18068 KB |
Output is correct |
55 |
Correct |
33 ms |
5912 KB |
Output is correct |
56 |
Correct |
25 ms |
5752 KB |
Output is correct |
57 |
Correct |
185 ms |
12092 KB |
Output is correct |
58 |
Correct |
68 ms |
11628 KB |
Output is correct |
59 |
Correct |
387 ms |
20216 KB |
Output is correct |
60 |
Correct |
1324 ms |
37208 KB |
Output is correct |
61 |
Correct |
464 ms |
19228 KB |
Output is correct |
62 |
Correct |
431 ms |
19136 KB |
Output is correct |
63 |
Correct |
520 ms |
19064 KB |
Output is correct |
64 |
Correct |
1410 ms |
23672 KB |
Output is correct |
65 |
Correct |
316 ms |
19708 KB |
Output is correct |
66 |
Correct |
856 ms |
25360 KB |
Output is correct |
67 |
Correct |
209 ms |
19700 KB |
Output is correct |
68 |
Correct |
592 ms |
23288 KB |
Output is correct |
69 |
Correct |
595 ms |
23160 KB |
Output is correct |
70 |
Correct |
596 ms |
22540 KB |
Output is correct |