//#include "race.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
template<class T> using V = vector<T>;
#define arr array
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
int N, K, ans;
vi ssz, kp;
vb vis;
V<vpi> adj;
int dfs0(int s, int p = -1) {
ssz[s] = 1;
trav(u, adj[s]) {
if(u.ff != p && !vis[u.ff]) ssz[s] += dfs0(u.ff, s);
}
return ssz[s];
}
int cent(int s, int des, int p = -1) {
trav(u, adj[s]) {
if(u.ff != p && !vis[u.ff] && 2 * ssz[u.ff] > des) return cent(u.ff, des, s);
}
return s;
}
void dfs1(int s, pi D, int c, int p = -1) {
if(D.ff > K) return;
if(c == 0) {
ckmin(ans, D.ss + kp[K - D.ff]);
} else if(c == 1) {
ckmin(kp[D.ff], D.ss);
} else {
kp[D.ff] = 0x3f3f3f3f;
}
trav(u, adj[s]) {
if(u.ff != p && !vis[u.ff]) dfs1(u.ff, mp(D.ff + u.ss, D.ss + 1), c, s);
}
}
void cent_deco(int s = 0) {
s = cent(s, dfs0(s));
vis[s] = 1;
kp[0] = 0;
trav(u, adj[s]) {
if(vis[u.ff]) continue;
dfs1(u.ff, mp(u.ss, 1), 0);
dfs1(u.ff, mp(u.ss, 1), 1);
}
trav(u, adj[s]) {
if(vis[u.ff]) continue;
dfs1(u.ff, mp(u.ss, 1), 2);
}
trav(u, adj[s]) {
if(vis[u.ff]) continue;
cent_deco(u.ff);
}
}
int best_path(int _N, int _K, int e[][2], int w[]) {
N = _N; K = _K;
vis.rsz(N);
adj.rsz(N);
ssz.rsz(N);
ans = 0x3f3f3f3f;
kp = vi(K + 1, ans);
F0R(i, N - 1) {
adj[e[i][0]].eb(e[i][1], w[i]);
adj[e[i][1]].eb(e[i][0], w[i]);
}
cent_deco();
if(ans == 0x3f3f3f3f) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
3984 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
3680 KB |
Output is correct |
26 |
Correct |
2 ms |
1620 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
1748 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
3132 KB |
Output is correct |
33 |
Correct |
3 ms |
3392 KB |
Output is correct |
34 |
Correct |
2 ms |
2628 KB |
Output is correct |
35 |
Correct |
2 ms |
3516 KB |
Output is correct |
36 |
Correct |
3 ms |
4052 KB |
Output is correct |
37 |
Correct |
3 ms |
3504 KB |
Output is correct |
38 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
125 ms |
9376 KB |
Output is correct |
20 |
Correct |
134 ms |
9244 KB |
Output is correct |
21 |
Correct |
148 ms |
9264 KB |
Output is correct |
22 |
Correct |
118 ms |
9488 KB |
Output is correct |
23 |
Correct |
62 ms |
9592 KB |
Output is correct |
24 |
Correct |
52 ms |
9468 KB |
Output is correct |
25 |
Correct |
113 ms |
12712 KB |
Output is correct |
26 |
Correct |
95 ms |
16532 KB |
Output is correct |
27 |
Correct |
147 ms |
18508 KB |
Output is correct |
28 |
Correct |
234 ms |
33084 KB |
Output is correct |
29 |
Correct |
271 ms |
32080 KB |
Output is correct |
30 |
Correct |
149 ms |
18596 KB |
Output is correct |
31 |
Correct |
180 ms |
18588 KB |
Output is correct |
32 |
Correct |
203 ms |
18592 KB |
Output is correct |
33 |
Correct |
184 ms |
17476 KB |
Output is correct |
34 |
Correct |
170 ms |
18364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
3984 KB |
Output is correct |
23 |
Correct |
2 ms |
3284 KB |
Output is correct |
24 |
Correct |
3 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
3680 KB |
Output is correct |
26 |
Correct |
2 ms |
1620 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
2 ms |
1620 KB |
Output is correct |
30 |
Correct |
2 ms |
1748 KB |
Output is correct |
31 |
Correct |
2 ms |
2900 KB |
Output is correct |
32 |
Correct |
3 ms |
3132 KB |
Output is correct |
33 |
Correct |
3 ms |
3392 KB |
Output is correct |
34 |
Correct |
2 ms |
2628 KB |
Output is correct |
35 |
Correct |
2 ms |
3516 KB |
Output is correct |
36 |
Correct |
3 ms |
4052 KB |
Output is correct |
37 |
Correct |
3 ms |
3504 KB |
Output is correct |
38 |
Correct |
2 ms |
2388 KB |
Output is correct |
39 |
Correct |
125 ms |
9376 KB |
Output is correct |
40 |
Correct |
134 ms |
9244 KB |
Output is correct |
41 |
Correct |
148 ms |
9264 KB |
Output is correct |
42 |
Correct |
118 ms |
9488 KB |
Output is correct |
43 |
Correct |
62 ms |
9592 KB |
Output is correct |
44 |
Correct |
52 ms |
9468 KB |
Output is correct |
45 |
Correct |
113 ms |
12712 KB |
Output is correct |
46 |
Correct |
95 ms |
16532 KB |
Output is correct |
47 |
Correct |
147 ms |
18508 KB |
Output is correct |
48 |
Correct |
234 ms |
33084 KB |
Output is correct |
49 |
Correct |
271 ms |
32080 KB |
Output is correct |
50 |
Correct |
149 ms |
18596 KB |
Output is correct |
51 |
Correct |
180 ms |
18588 KB |
Output is correct |
52 |
Correct |
203 ms |
18592 KB |
Output is correct |
53 |
Correct |
184 ms |
17476 KB |
Output is correct |
54 |
Correct |
170 ms |
18364 KB |
Output is correct |
55 |
Correct |
8 ms |
1236 KB |
Output is correct |
56 |
Correct |
10 ms |
1204 KB |
Output is correct |
57 |
Correct |
81 ms |
9460 KB |
Output is correct |
58 |
Correct |
31 ms |
9212 KB |
Output is correct |
59 |
Correct |
98 ms |
17252 KB |
Output is correct |
60 |
Correct |
455 ms |
36140 KB |
Output is correct |
61 |
Correct |
163 ms |
18772 KB |
Output is correct |
62 |
Correct |
203 ms |
22572 KB |
Output is correct |
63 |
Correct |
237 ms |
22696 KB |
Output is correct |
64 |
Correct |
485 ms |
20984 KB |
Output is correct |
65 |
Correct |
184 ms |
19512 KB |
Output is correct |
66 |
Correct |
349 ms |
33440 KB |
Output is correct |
67 |
Correct |
92 ms |
23264 KB |
Output is correct |
68 |
Correct |
240 ms |
23344 KB |
Output is correct |
69 |
Correct |
247 ms |
23452 KB |
Output is correct |
70 |
Correct |
212 ms |
22392 KB |
Output is correct |