#include "race.h"
#include <bits/stdc++.h>
#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl
#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)
#define type(x) __typeof(x.begin())
#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)
#define pb push_back
#define mp make_pair
#define nd second
#define st first
#define endl '\n'
using namespace std;
#define pii pair < int ,int >
const int N = 1e6 + 5;
const int inf = 1e9 + 7;
int ans = inf;
int k, H[N], h[N], n, m, x, y, t, S, sum[N];
vector< pii > v[N], g[N];
int prep(int node, int root) {
sum[node] = 1;
foreach(it, v[node])
if(!h[it->st] && it->st != root)
sum[node] += prep(it->st, node);
return sum[node];
}
int find(int node, int root) {
foreach(it, v[node])
if(!h[it->st] && it->st != root && sum[it->st] > S)
return find(it->st, node);
return node;
}
void dfs(int node, int root, int dist, int S, int t) {
if(dist > k) return ;
g[S].pb(mp(dist, t));
foreach(it, v[node])
if(it->st != root && !h[it->st])
dfs(it->st, node, it->nd + dist, S, t + 1);
}
void solve(int node) {
prep(node, 0); S = sum[node] >> 1;
node = find(node, 0);
h[node] = 1;
S = 0;
foreach(it, v[node]) {
if(!h[it->st]) {
g[++S].clear();
dfs(it->st, node, it->nd, S, 1);
}
}
H[0] = 0;
FOR(i, 1, S) {
foreach(it, g[i])
if(it->st <= k)
ans = min(H[k-it->st] + it->nd, ans);
foreach(it, g[i])
if(it->st <= k)
H[it->st] = min(H[it->st], it->nd);
}
H[0] = inf;
FOR(i, 1, S)
foreach(it, g[i])
if(it->st <= k)
H[it->st] = inf;
foreach(it, v[node])
if(!h[it->st])
solve(it->st);
}
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 x = H[i][0],
y = H[i][1];
v[x].pb(mp(y, L[i]));
v[y].pb(mp(x, L[i]));
}
memset(::H, 10, sizeof ::H);
ans = inf;
solve(1);
if(ans > n) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
51192 KB |
Output is correct |
2 |
Correct |
45 ms |
51304 KB |
Output is correct |
3 |
Correct |
42 ms |
51304 KB |
Output is correct |
4 |
Correct |
42 ms |
51304 KB |
Output is correct |
5 |
Correct |
42 ms |
51400 KB |
Output is correct |
6 |
Correct |
42 ms |
51400 KB |
Output is correct |
7 |
Correct |
44 ms |
51412 KB |
Output is correct |
8 |
Correct |
41 ms |
51412 KB |
Output is correct |
9 |
Correct |
44 ms |
51412 KB |
Output is correct |
10 |
Correct |
41 ms |
51544 KB |
Output is correct |
11 |
Correct |
41 ms |
51544 KB |
Output is correct |
12 |
Correct |
41 ms |
51544 KB |
Output is correct |
13 |
Correct |
40 ms |
51544 KB |
Output is correct |
14 |
Correct |
42 ms |
51544 KB |
Output is correct |
15 |
Correct |
43 ms |
51544 KB |
Output is correct |
16 |
Correct |
41 ms |
51544 KB |
Output is correct |
17 |
Correct |
41 ms |
51544 KB |
Output is correct |
18 |
Correct |
42 ms |
51544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
51192 KB |
Output is correct |
2 |
Correct |
45 ms |
51304 KB |
Output is correct |
3 |
Correct |
42 ms |
51304 KB |
Output is correct |
4 |
Correct |
42 ms |
51304 KB |
Output is correct |
5 |
Correct |
42 ms |
51400 KB |
Output is correct |
6 |
Correct |
42 ms |
51400 KB |
Output is correct |
7 |
Correct |
44 ms |
51412 KB |
Output is correct |
8 |
Correct |
41 ms |
51412 KB |
Output is correct |
9 |
Correct |
44 ms |
51412 KB |
Output is correct |
10 |
Correct |
41 ms |
51544 KB |
Output is correct |
11 |
Correct |
41 ms |
51544 KB |
Output is correct |
12 |
Correct |
41 ms |
51544 KB |
Output is correct |
13 |
Correct |
40 ms |
51544 KB |
Output is correct |
14 |
Correct |
42 ms |
51544 KB |
Output is correct |
15 |
Correct |
43 ms |
51544 KB |
Output is correct |
16 |
Correct |
41 ms |
51544 KB |
Output is correct |
17 |
Correct |
41 ms |
51544 KB |
Output is correct |
18 |
Correct |
42 ms |
51544 KB |
Output is correct |
19 |
Correct |
41 ms |
51544 KB |
Output is correct |
20 |
Correct |
44 ms |
51544 KB |
Output is correct |
21 |
Correct |
45 ms |
51544 KB |
Output is correct |
22 |
Correct |
42 ms |
51568 KB |
Output is correct |
23 |
Correct |
43 ms |
51568 KB |
Output is correct |
24 |
Correct |
43 ms |
51568 KB |
Output is correct |
25 |
Correct |
49 ms |
51568 KB |
Output is correct |
26 |
Correct |
47 ms |
51568 KB |
Output is correct |
27 |
Correct |
45 ms |
51568 KB |
Output is correct |
28 |
Correct |
43 ms |
51628 KB |
Output is correct |
29 |
Correct |
42 ms |
51628 KB |
Output is correct |
30 |
Correct |
43 ms |
51692 KB |
Output is correct |
31 |
Correct |
42 ms |
51692 KB |
Output is correct |
32 |
Correct |
44 ms |
51692 KB |
Output is correct |
33 |
Correct |
47 ms |
51692 KB |
Output is correct |
34 |
Correct |
45 ms |
51692 KB |
Output is correct |
35 |
Correct |
47 ms |
51692 KB |
Output is correct |
36 |
Correct |
47 ms |
51692 KB |
Output is correct |
37 |
Correct |
42 ms |
51692 KB |
Output is correct |
38 |
Correct |
42 ms |
51692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
51192 KB |
Output is correct |
2 |
Correct |
45 ms |
51304 KB |
Output is correct |
3 |
Correct |
42 ms |
51304 KB |
Output is correct |
4 |
Correct |
42 ms |
51304 KB |
Output is correct |
5 |
Correct |
42 ms |
51400 KB |
Output is correct |
6 |
Correct |
42 ms |
51400 KB |
Output is correct |
7 |
Correct |
44 ms |
51412 KB |
Output is correct |
8 |
Correct |
41 ms |
51412 KB |
Output is correct |
9 |
Correct |
44 ms |
51412 KB |
Output is correct |
10 |
Correct |
41 ms |
51544 KB |
Output is correct |
11 |
Correct |
41 ms |
51544 KB |
Output is correct |
12 |
Correct |
41 ms |
51544 KB |
Output is correct |
13 |
Correct |
40 ms |
51544 KB |
Output is correct |
14 |
Correct |
42 ms |
51544 KB |
Output is correct |
15 |
Correct |
43 ms |
51544 KB |
Output is correct |
16 |
Correct |
41 ms |
51544 KB |
Output is correct |
17 |
Correct |
41 ms |
51544 KB |
Output is correct |
18 |
Correct |
42 ms |
51544 KB |
Output is correct |
19 |
Correct |
176 ms |
57480 KB |
Output is correct |
20 |
Correct |
183 ms |
57480 KB |
Output is correct |
21 |
Correct |
194 ms |
57728 KB |
Output is correct |
22 |
Correct |
159 ms |
58288 KB |
Output is correct |
23 |
Correct |
136 ms |
58288 KB |
Output is correct |
24 |
Correct |
113 ms |
58288 KB |
Output is correct |
25 |
Correct |
200 ms |
60896 KB |
Output is correct |
26 |
Correct |
150 ms |
62216 KB |
Output is correct |
27 |
Correct |
229 ms |
63192 KB |
Output is correct |
28 |
Correct |
381 ms |
70660 KB |
Output is correct |
29 |
Correct |
463 ms |
70660 KB |
Output is correct |
30 |
Correct |
254 ms |
70660 KB |
Output is correct |
31 |
Correct |
289 ms |
70660 KB |
Output is correct |
32 |
Correct |
292 ms |
70660 KB |
Output is correct |
33 |
Correct |
399 ms |
70660 KB |
Output is correct |
34 |
Correct |
434 ms |
70660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
51192 KB |
Output is correct |
2 |
Correct |
45 ms |
51304 KB |
Output is correct |
3 |
Correct |
42 ms |
51304 KB |
Output is correct |
4 |
Correct |
42 ms |
51304 KB |
Output is correct |
5 |
Correct |
42 ms |
51400 KB |
Output is correct |
6 |
Correct |
42 ms |
51400 KB |
Output is correct |
7 |
Correct |
44 ms |
51412 KB |
Output is correct |
8 |
Correct |
41 ms |
51412 KB |
Output is correct |
9 |
Correct |
44 ms |
51412 KB |
Output is correct |
10 |
Correct |
41 ms |
51544 KB |
Output is correct |
11 |
Correct |
41 ms |
51544 KB |
Output is correct |
12 |
Correct |
41 ms |
51544 KB |
Output is correct |
13 |
Correct |
40 ms |
51544 KB |
Output is correct |
14 |
Correct |
42 ms |
51544 KB |
Output is correct |
15 |
Correct |
43 ms |
51544 KB |
Output is correct |
16 |
Correct |
41 ms |
51544 KB |
Output is correct |
17 |
Correct |
41 ms |
51544 KB |
Output is correct |
18 |
Correct |
42 ms |
51544 KB |
Output is correct |
19 |
Correct |
41 ms |
51544 KB |
Output is correct |
20 |
Correct |
44 ms |
51544 KB |
Output is correct |
21 |
Correct |
45 ms |
51544 KB |
Output is correct |
22 |
Correct |
42 ms |
51568 KB |
Output is correct |
23 |
Correct |
43 ms |
51568 KB |
Output is correct |
24 |
Correct |
43 ms |
51568 KB |
Output is correct |
25 |
Correct |
49 ms |
51568 KB |
Output is correct |
26 |
Correct |
47 ms |
51568 KB |
Output is correct |
27 |
Correct |
45 ms |
51568 KB |
Output is correct |
28 |
Correct |
43 ms |
51628 KB |
Output is correct |
29 |
Correct |
42 ms |
51628 KB |
Output is correct |
30 |
Correct |
43 ms |
51692 KB |
Output is correct |
31 |
Correct |
42 ms |
51692 KB |
Output is correct |
32 |
Correct |
44 ms |
51692 KB |
Output is correct |
33 |
Correct |
47 ms |
51692 KB |
Output is correct |
34 |
Correct |
45 ms |
51692 KB |
Output is correct |
35 |
Correct |
47 ms |
51692 KB |
Output is correct |
36 |
Correct |
47 ms |
51692 KB |
Output is correct |
37 |
Correct |
42 ms |
51692 KB |
Output is correct |
38 |
Correct |
42 ms |
51692 KB |
Output is correct |
39 |
Correct |
176 ms |
57480 KB |
Output is correct |
40 |
Correct |
183 ms |
57480 KB |
Output is correct |
41 |
Correct |
194 ms |
57728 KB |
Output is correct |
42 |
Correct |
159 ms |
58288 KB |
Output is correct |
43 |
Correct |
136 ms |
58288 KB |
Output is correct |
44 |
Correct |
113 ms |
58288 KB |
Output is correct |
45 |
Correct |
200 ms |
60896 KB |
Output is correct |
46 |
Correct |
150 ms |
62216 KB |
Output is correct |
47 |
Correct |
229 ms |
63192 KB |
Output is correct |
48 |
Correct |
381 ms |
70660 KB |
Output is correct |
49 |
Correct |
463 ms |
70660 KB |
Output is correct |
50 |
Correct |
254 ms |
70660 KB |
Output is correct |
51 |
Correct |
289 ms |
70660 KB |
Output is correct |
52 |
Correct |
292 ms |
70660 KB |
Output is correct |
53 |
Correct |
399 ms |
70660 KB |
Output is correct |
54 |
Correct |
434 ms |
70660 KB |
Output is correct |
55 |
Correct |
57 ms |
70660 KB |
Output is correct |
56 |
Correct |
57 ms |
70660 KB |
Output is correct |
57 |
Correct |
137 ms |
70660 KB |
Output is correct |
58 |
Correct |
89 ms |
70660 KB |
Output is correct |
59 |
Correct |
152 ms |
70660 KB |
Output is correct |
60 |
Correct |
554 ms |
72804 KB |
Output is correct |
61 |
Correct |
252 ms |
72804 KB |
Output is correct |
62 |
Correct |
323 ms |
72804 KB |
Output is correct |
63 |
Correct |
316 ms |
72804 KB |
Output is correct |
64 |
Correct |
582 ms |
72804 KB |
Output is correct |
65 |
Correct |
351 ms |
72804 KB |
Output is correct |
66 |
Correct |
566 ms |
72804 KB |
Output is correct |
67 |
Correct |
215 ms |
72804 KB |
Output is correct |
68 |
Correct |
329 ms |
72804 KB |
Output is correct |
69 |
Correct |
374 ms |
72804 KB |
Output is correct |
70 |
Correct |
371 ms |
72804 KB |
Output is correct |