#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
#define int long long
const int MAXN = 2e5+1, LOG = 19, MAXK = 1e6+5;
int n,k;
vector<int> v[MAXN], toclr;
vector<PII> v1[MAXN], add;
int dist[MAXN], len[MAXN], sz[MAXN], mp[MAXK];
bool z[MAXN];
int res = LLINF;
int find_centroid(int s, int pa, int num) {
FORX(u,v1[s]) {
if (u.F == pa || z[u.F]) continue;
if (sz[u.F] > num/2) return find_centroid(u.F,s,num);
}
return s;
}
void dfs(int s, int pa) {
sz[s] = 1;
FORX(u,v1[s]) {
if (u.F == pa || z[u.F]) continue;
dfs(u.F,s);
sz[s] += sz[u.F];
}
}
void dfs1(int s, int pa, int src, int l, int d) {
FORX(u,v1[s]) {
if (u.F == pa || z[u.F]) continue;
int nl = l+u.S, nd = d+1LL;
if (nl > k) continue;
if (nl == k) res = min(res, nd);
else if (nl < k) res = min(res, nd+mp[k-nl]);
add.PB({nl,nd});
dfs1(u.F,s,src,nl,nd);
if (s == src) {
FORX(y,add) {
int cl = y.F, cd = y.S;
mp[cl] = min(mp[cl],cd);
toclr.PB(cl);
}
add.clear();
}
}
}
void decomp(int s, int pa) {
FORX(u,toclr) mp[u] = LLINF;
toclr.clear();
dfs(s,pa);
int c = find_centroid(s,pa,sz[s]);
dfs1(c,-1,c,0LL,0LL);
z[c] = 1;
FORX(u,v1[c]) if (u.F != pa && !z[u.F]) decomp(u.F,c);
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[]) {
OPTM;
n = N; k = K;
FOR(i,0,n-1) {
v1[H[i][0]].PB({H[i][1],L[i]});
v1[H[i][1]].PB({H[i][0],L[i]});
}
FOR(i,0,MAXK) mp[i] = LLINF;
decomp(0,-1);
return res == LLINF ? -1 : res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
8 ms |
17492 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
8 ms |
17616 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
8 ms |
17572 KB |
Output is correct |
7 |
Correct |
8 ms |
17492 KB |
Output is correct |
8 |
Correct |
8 ms |
17580 KB |
Output is correct |
9 |
Correct |
8 ms |
17524 KB |
Output is correct |
10 |
Correct |
8 ms |
17492 KB |
Output is correct |
11 |
Correct |
8 ms |
17492 KB |
Output is correct |
12 |
Correct |
8 ms |
17492 KB |
Output is correct |
13 |
Correct |
7 ms |
17616 KB |
Output is correct |
14 |
Correct |
8 ms |
17492 KB |
Output is correct |
15 |
Correct |
8 ms |
17492 KB |
Output is correct |
16 |
Correct |
8 ms |
17492 KB |
Output is correct |
17 |
Correct |
8 ms |
17492 KB |
Output is correct |
18 |
Correct |
8 ms |
17492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
8 ms |
17492 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
8 ms |
17616 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
8 ms |
17572 KB |
Output is correct |
7 |
Correct |
8 ms |
17492 KB |
Output is correct |
8 |
Correct |
8 ms |
17580 KB |
Output is correct |
9 |
Correct |
8 ms |
17524 KB |
Output is correct |
10 |
Correct |
8 ms |
17492 KB |
Output is correct |
11 |
Correct |
8 ms |
17492 KB |
Output is correct |
12 |
Correct |
8 ms |
17492 KB |
Output is correct |
13 |
Correct |
7 ms |
17616 KB |
Output is correct |
14 |
Correct |
8 ms |
17492 KB |
Output is correct |
15 |
Correct |
8 ms |
17492 KB |
Output is correct |
16 |
Correct |
8 ms |
17492 KB |
Output is correct |
17 |
Correct |
8 ms |
17492 KB |
Output is correct |
18 |
Correct |
8 ms |
17492 KB |
Output is correct |
19 |
Correct |
8 ms |
17492 KB |
Output is correct |
20 |
Correct |
8 ms |
17492 KB |
Output is correct |
21 |
Correct |
9 ms |
17604 KB |
Output is correct |
22 |
Correct |
11 ms |
17620 KB |
Output is correct |
23 |
Correct |
8 ms |
17620 KB |
Output is correct |
24 |
Correct |
9 ms |
17580 KB |
Output is correct |
25 |
Correct |
9 ms |
17620 KB |
Output is correct |
26 |
Correct |
8 ms |
17600 KB |
Output is correct |
27 |
Correct |
8 ms |
17620 KB |
Output is correct |
28 |
Correct |
8 ms |
17620 KB |
Output is correct |
29 |
Correct |
9 ms |
17556 KB |
Output is correct |
30 |
Correct |
11 ms |
17576 KB |
Output is correct |
31 |
Correct |
9 ms |
17616 KB |
Output is correct |
32 |
Correct |
8 ms |
17556 KB |
Output is correct |
33 |
Correct |
8 ms |
17676 KB |
Output is correct |
34 |
Correct |
9 ms |
17620 KB |
Output is correct |
35 |
Correct |
8 ms |
17584 KB |
Output is correct |
36 |
Correct |
8 ms |
17656 KB |
Output is correct |
37 |
Correct |
9 ms |
17620 KB |
Output is correct |
38 |
Correct |
9 ms |
17620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
8 ms |
17492 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
8 ms |
17616 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
8 ms |
17572 KB |
Output is correct |
7 |
Correct |
8 ms |
17492 KB |
Output is correct |
8 |
Correct |
8 ms |
17580 KB |
Output is correct |
9 |
Correct |
8 ms |
17524 KB |
Output is correct |
10 |
Correct |
8 ms |
17492 KB |
Output is correct |
11 |
Correct |
8 ms |
17492 KB |
Output is correct |
12 |
Correct |
8 ms |
17492 KB |
Output is correct |
13 |
Correct |
7 ms |
17616 KB |
Output is correct |
14 |
Correct |
8 ms |
17492 KB |
Output is correct |
15 |
Correct |
8 ms |
17492 KB |
Output is correct |
16 |
Correct |
8 ms |
17492 KB |
Output is correct |
17 |
Correct |
8 ms |
17492 KB |
Output is correct |
18 |
Correct |
8 ms |
17492 KB |
Output is correct |
19 |
Correct |
85 ms |
25008 KB |
Output is correct |
20 |
Correct |
83 ms |
24864 KB |
Output is correct |
21 |
Correct |
89 ms |
25564 KB |
Output is correct |
22 |
Correct |
76 ms |
26328 KB |
Output is correct |
23 |
Correct |
66 ms |
25004 KB |
Output is correct |
24 |
Correct |
43 ms |
24256 KB |
Output is correct |
25 |
Correct |
94 ms |
27688 KB |
Output is correct |
26 |
Correct |
78 ms |
34316 KB |
Output is correct |
27 |
Correct |
111 ms |
32464 KB |
Output is correct |
28 |
Correct |
168 ms |
43484 KB |
Output is correct |
29 |
Correct |
197 ms |
42680 KB |
Output is correct |
30 |
Correct |
120 ms |
32520 KB |
Output is correct |
31 |
Correct |
116 ms |
32516 KB |
Output is correct |
32 |
Correct |
149 ms |
32452 KB |
Output is correct |
33 |
Correct |
156 ms |
31216 KB |
Output is correct |
34 |
Correct |
126 ms |
31136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
17492 KB |
Output is correct |
2 |
Correct |
8 ms |
17492 KB |
Output is correct |
3 |
Correct |
8 ms |
17492 KB |
Output is correct |
4 |
Correct |
8 ms |
17616 KB |
Output is correct |
5 |
Correct |
7 ms |
17492 KB |
Output is correct |
6 |
Correct |
8 ms |
17572 KB |
Output is correct |
7 |
Correct |
8 ms |
17492 KB |
Output is correct |
8 |
Correct |
8 ms |
17580 KB |
Output is correct |
9 |
Correct |
8 ms |
17524 KB |
Output is correct |
10 |
Correct |
8 ms |
17492 KB |
Output is correct |
11 |
Correct |
8 ms |
17492 KB |
Output is correct |
12 |
Correct |
8 ms |
17492 KB |
Output is correct |
13 |
Correct |
7 ms |
17616 KB |
Output is correct |
14 |
Correct |
8 ms |
17492 KB |
Output is correct |
15 |
Correct |
8 ms |
17492 KB |
Output is correct |
16 |
Correct |
8 ms |
17492 KB |
Output is correct |
17 |
Correct |
8 ms |
17492 KB |
Output is correct |
18 |
Correct |
8 ms |
17492 KB |
Output is correct |
19 |
Correct |
8 ms |
17492 KB |
Output is correct |
20 |
Correct |
8 ms |
17492 KB |
Output is correct |
21 |
Correct |
9 ms |
17604 KB |
Output is correct |
22 |
Correct |
11 ms |
17620 KB |
Output is correct |
23 |
Correct |
8 ms |
17620 KB |
Output is correct |
24 |
Correct |
9 ms |
17580 KB |
Output is correct |
25 |
Correct |
9 ms |
17620 KB |
Output is correct |
26 |
Correct |
8 ms |
17600 KB |
Output is correct |
27 |
Correct |
8 ms |
17620 KB |
Output is correct |
28 |
Correct |
8 ms |
17620 KB |
Output is correct |
29 |
Correct |
9 ms |
17556 KB |
Output is correct |
30 |
Correct |
11 ms |
17576 KB |
Output is correct |
31 |
Correct |
9 ms |
17616 KB |
Output is correct |
32 |
Correct |
8 ms |
17556 KB |
Output is correct |
33 |
Correct |
8 ms |
17676 KB |
Output is correct |
34 |
Correct |
9 ms |
17620 KB |
Output is correct |
35 |
Correct |
8 ms |
17584 KB |
Output is correct |
36 |
Correct |
8 ms |
17656 KB |
Output is correct |
37 |
Correct |
9 ms |
17620 KB |
Output is correct |
38 |
Correct |
9 ms |
17620 KB |
Output is correct |
39 |
Correct |
85 ms |
25008 KB |
Output is correct |
40 |
Correct |
83 ms |
24864 KB |
Output is correct |
41 |
Correct |
89 ms |
25564 KB |
Output is correct |
42 |
Correct |
76 ms |
26328 KB |
Output is correct |
43 |
Correct |
66 ms |
25004 KB |
Output is correct |
44 |
Correct |
43 ms |
24256 KB |
Output is correct |
45 |
Correct |
94 ms |
27688 KB |
Output is correct |
46 |
Correct |
78 ms |
34316 KB |
Output is correct |
47 |
Correct |
111 ms |
32464 KB |
Output is correct |
48 |
Correct |
168 ms |
43484 KB |
Output is correct |
49 |
Correct |
197 ms |
42680 KB |
Output is correct |
50 |
Correct |
120 ms |
32520 KB |
Output is correct |
51 |
Correct |
116 ms |
32516 KB |
Output is correct |
52 |
Correct |
149 ms |
32452 KB |
Output is correct |
53 |
Correct |
156 ms |
31216 KB |
Output is correct |
54 |
Correct |
126 ms |
31136 KB |
Output is correct |
55 |
Correct |
13 ms |
18412 KB |
Output is correct |
56 |
Correct |
14 ms |
18640 KB |
Output is correct |
57 |
Correct |
64 ms |
27024 KB |
Output is correct |
58 |
Correct |
36 ms |
26044 KB |
Output is correct |
59 |
Correct |
83 ms |
34260 KB |
Output is correct |
60 |
Correct |
278 ms |
51148 KB |
Output is correct |
61 |
Correct |
133 ms |
35812 KB |
Output is correct |
62 |
Correct |
150 ms |
37428 KB |
Output is correct |
63 |
Correct |
196 ms |
37368 KB |
Output is correct |
64 |
Correct |
381 ms |
39572 KB |
Output is correct |
65 |
Correct |
198 ms |
35792 KB |
Output is correct |
66 |
Correct |
295 ms |
43948 KB |
Output is correct |
67 |
Correct |
151 ms |
37972 KB |
Output is correct |
68 |
Correct |
214 ms |
36548 KB |
Output is correct |
69 |
Correct |
193 ms |
36496 KB |
Output is correct |
70 |
Correct |
224 ms |
35596 KB |
Output is correct |