#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for(int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (int) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n, k, sz[200005], coun, ans = INF;
int newdist[1000005], dist[1000005];
bool vis[200005];
vpi adj[200005];
int sztrsz(int x, int p) {
sz[x] = 1;
for (auto it:adj[x]) if (!vis[it.f] and it.f != p) sz[x] += sztrsz(it.f, x);
return sz[x];
}
int findcent(int x, int p, int n) {
for (auto it:adj[x]) {
if (vis[it.f]) continue;
if (it.f != p and sz[it.f] > n/2 and !vis[it.f]) return findcent(it.f, x, n);
}
return x;
}
void dfsend(int x, int p, int d, int num) {
if (d == k) ans = min(ans, num);
if (d > k) return;
for (auto it:adj[x]) if (!vis[it.f] and it.f != p) dfsend(it.f, x, d+it.s, num+1);
}
vi cur, temp;
void dfs(int x, int p, int d, int num) {
if (d == k) ans = min(ans, num);
if (d > k) return;
if (dist[k-d] != INF) ans = min(ans, num + dist[k-d]);
newdist[d] = min(newdist[d], num);
cur.pb(d); temp.pb(d);
for (auto it:adj[x]) if (it.f != p and !vis[it.f]) dfs(it.f, x, d + it.s, num+1);
}
void solve(int x) {
int cent = findcent(x, -1, sztrsz(x, -1));
dfsend(cent, -1, 0, 0);
for (auto it:adj[cent]) if (!vis[it.f]) {
dfs(it.f, cent, it.s, 1);
for (auto d:temp) dist[d] = min(dist[d], newdist[d]);
temp.clear();
}
for (auto it:cur) dist[it] = newdist[it] = INF;
cur.clear();
vis[cent] = 1;
for (auto it:adj[cent]) if (!vis[it.f]) solve(it.f);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
FOR(i, 0, n-2) {
adj[H[i][0]].pb(pi(H[i][1], L[i]));
adj[H[i][1]].pb(pi(H[i][0], L[i]));
}
FOR(i, 0, K) dist[i] = newdist[i] = INF;
solve(0);
return (ans == INF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
13 ms |
5120 KB |
Output is correct |
7 |
Correct |
19 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
13 ms |
5120 KB |
Output is correct |
7 |
Correct |
19 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
9 ms |
12288 KB |
Output is correct |
23 |
Correct |
9 ms |
11008 KB |
Output is correct |
24 |
Correct |
9 ms |
11904 KB |
Output is correct |
25 |
Correct |
9 ms |
11648 KB |
Output is correct |
26 |
Correct |
6 ms |
7808 KB |
Output is correct |
27 |
Correct |
8 ms |
11392 KB |
Output is correct |
28 |
Correct |
7 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
7 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
10240 KB |
Output is correct |
32 |
Correct |
9 ms |
10624 KB |
Output is correct |
33 |
Correct |
9 ms |
11264 KB |
Output is correct |
34 |
Correct |
8 ms |
9728 KB |
Output is correct |
35 |
Correct |
9 ms |
11520 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
9 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
13 ms |
5120 KB |
Output is correct |
7 |
Correct |
19 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
175 ms |
11896 KB |
Output is correct |
20 |
Correct |
176 ms |
11768 KB |
Output is correct |
21 |
Correct |
203 ms |
12188 KB |
Output is correct |
22 |
Correct |
173 ms |
12580 KB |
Output is correct |
23 |
Correct |
110 ms |
12024 KB |
Output is correct |
24 |
Correct |
78 ms |
12024 KB |
Output is correct |
25 |
Correct |
149 ms |
13688 KB |
Output is correct |
26 |
Correct |
112 ms |
16756 KB |
Output is correct |
27 |
Correct |
255 ms |
18936 KB |
Output is correct |
28 |
Correct |
350 ms |
27000 KB |
Output is correct |
29 |
Correct |
409 ms |
26488 KB |
Output is correct |
30 |
Correct |
241 ms |
18808 KB |
Output is correct |
31 |
Correct |
242 ms |
18936 KB |
Output is correct |
32 |
Correct |
276 ms |
18936 KB |
Output is correct |
33 |
Correct |
290 ms |
17656 KB |
Output is correct |
34 |
Correct |
276 ms |
18788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
13 ms |
5120 KB |
Output is correct |
7 |
Correct |
19 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
3 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
5120 KB |
Output is correct |
20 |
Correct |
4 ms |
5120 KB |
Output is correct |
21 |
Correct |
5 ms |
5120 KB |
Output is correct |
22 |
Correct |
9 ms |
12288 KB |
Output is correct |
23 |
Correct |
9 ms |
11008 KB |
Output is correct |
24 |
Correct |
9 ms |
11904 KB |
Output is correct |
25 |
Correct |
9 ms |
11648 KB |
Output is correct |
26 |
Correct |
6 ms |
7808 KB |
Output is correct |
27 |
Correct |
8 ms |
11392 KB |
Output is correct |
28 |
Correct |
7 ms |
6656 KB |
Output is correct |
29 |
Correct |
6 ms |
7680 KB |
Output is correct |
30 |
Correct |
7 ms |
7936 KB |
Output is correct |
31 |
Correct |
8 ms |
10240 KB |
Output is correct |
32 |
Correct |
9 ms |
10624 KB |
Output is correct |
33 |
Correct |
9 ms |
11264 KB |
Output is correct |
34 |
Correct |
8 ms |
9728 KB |
Output is correct |
35 |
Correct |
9 ms |
11520 KB |
Output is correct |
36 |
Correct |
9 ms |
12416 KB |
Output is correct |
37 |
Correct |
9 ms |
11392 KB |
Output is correct |
38 |
Correct |
7 ms |
9088 KB |
Output is correct |
39 |
Correct |
175 ms |
11896 KB |
Output is correct |
40 |
Correct |
176 ms |
11768 KB |
Output is correct |
41 |
Correct |
203 ms |
12188 KB |
Output is correct |
42 |
Correct |
173 ms |
12580 KB |
Output is correct |
43 |
Correct |
110 ms |
12024 KB |
Output is correct |
44 |
Correct |
78 ms |
12024 KB |
Output is correct |
45 |
Correct |
149 ms |
13688 KB |
Output is correct |
46 |
Correct |
112 ms |
16756 KB |
Output is correct |
47 |
Correct |
255 ms |
18936 KB |
Output is correct |
48 |
Correct |
350 ms |
27000 KB |
Output is correct |
49 |
Correct |
409 ms |
26488 KB |
Output is correct |
50 |
Correct |
241 ms |
18808 KB |
Output is correct |
51 |
Correct |
242 ms |
18936 KB |
Output is correct |
52 |
Correct |
276 ms |
18936 KB |
Output is correct |
53 |
Correct |
290 ms |
17656 KB |
Output is correct |
54 |
Correct |
276 ms |
18788 KB |
Output is correct |
55 |
Correct |
14 ms |
5888 KB |
Output is correct |
56 |
Correct |
15 ms |
5888 KB |
Output is correct |
57 |
Correct |
111 ms |
12700 KB |
Output is correct |
58 |
Correct |
65 ms |
12636 KB |
Output is correct |
59 |
Correct |
112 ms |
18164 KB |
Output is correct |
60 |
Correct |
630 ms |
35692 KB |
Output is correct |
61 |
Correct |
258 ms |
19320 KB |
Output is correct |
62 |
Correct |
299 ms |
27644 KB |
Output is correct |
63 |
Correct |
402 ms |
27644 KB |
Output is correct |
64 |
Correct |
742 ms |
24604 KB |
Output is correct |
65 |
Correct |
302 ms |
19704 KB |
Output is correct |
66 |
Correct |
567 ms |
32888 KB |
Output is correct |
67 |
Correct |
188 ms |
29160 KB |
Output is correct |
68 |
Correct |
405 ms |
27892 KB |
Output is correct |
69 |
Correct |
482 ms |
28028 KB |
Output is correct |
70 |
Correct |
376 ms |
27252 KB |
Output is correct |