#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
const int N = 1e6 + 5;
vector<pii> G[N];
int ans = 1e9, arr[N], tmp[N], sz[N], k, mx,n;
bool vis[N];
int get_sz(int node, int p = -1){
sz[node] = 1;
for(auto next : G[node]){
if(next.first != p && !vis[next.first]){
sz[node] += get_sz(next.first, node);
}
}
return sz[node];
}
int get_centroid(int node, int size, int p = -1){
for(auto next : G[node]){
if(next.first == p || vis[next.first]) continue;
if(sz[next.first] >= size) return get_centroid(next.first, size, node);
}
return node;
}
void cnt(int node, int p, int yes, int sum, int level){
if (sum > k) return;
if (sum == k) ans = min(ans, level);
if (yes == 1) tmp[sum] = min(tmp[sum], level);
else if (yes) tmp[sum] = 1e9;
else ans = min(ans, level + tmp[k - sum]);
for (auto x:G[node])
if (x.first != p && !vis[x.first])
cnt(x.first, node, yes, sum + x.second, level + 1);
}
void decompose(int node = 0){
get_sz(node);
int c = get_centroid(node, sz[node] / 2);
vis[c] = 1;
mx = 0;
for (auto x:G[c]) if (!vis[x.first]) cnt(x.first, c, 0, x.second, 1), cnt(x.first, c, 1, x.second, 1);
for (auto x:G[c]) if (!vis[x.first]) cnt(x.first, c, 2, x.second, 2);
for (auto x:G[c]) if (!vis[x.first]) decompose(x.first);
}
int best_path(int N, int K, int H[][2], int L[]){
fill(tmp, tmp+1000000 + 5, 1e9);
n= N, k = K;
for(int i = 0; i < N-1; i++){
G[H[i][1]].pb({H[i][0], L[i]});
G[H[i][0]].pb({H[i][1], L[i]});
}
decompose();
return (ans == 1e9) ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
12 ms |
27652 KB |
Output is correct |
4 |
Correct |
16 ms |
27740 KB |
Output is correct |
5 |
Correct |
12 ms |
27632 KB |
Output is correct |
6 |
Correct |
14 ms |
27732 KB |
Output is correct |
7 |
Correct |
12 ms |
27636 KB |
Output is correct |
8 |
Correct |
12 ms |
27732 KB |
Output is correct |
9 |
Correct |
14 ms |
27732 KB |
Output is correct |
10 |
Correct |
13 ms |
27628 KB |
Output is correct |
11 |
Correct |
12 ms |
27744 KB |
Output is correct |
12 |
Correct |
12 ms |
27632 KB |
Output is correct |
13 |
Correct |
13 ms |
27632 KB |
Output is correct |
14 |
Correct |
15 ms |
27632 KB |
Output is correct |
15 |
Correct |
13 ms |
27708 KB |
Output is correct |
16 |
Correct |
17 ms |
27628 KB |
Output is correct |
17 |
Correct |
15 ms |
27624 KB |
Output is correct |
18 |
Correct |
16 ms |
27708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
12 ms |
27652 KB |
Output is correct |
4 |
Correct |
16 ms |
27740 KB |
Output is correct |
5 |
Correct |
12 ms |
27632 KB |
Output is correct |
6 |
Correct |
14 ms |
27732 KB |
Output is correct |
7 |
Correct |
12 ms |
27636 KB |
Output is correct |
8 |
Correct |
12 ms |
27732 KB |
Output is correct |
9 |
Correct |
14 ms |
27732 KB |
Output is correct |
10 |
Correct |
13 ms |
27628 KB |
Output is correct |
11 |
Correct |
12 ms |
27744 KB |
Output is correct |
12 |
Correct |
12 ms |
27632 KB |
Output is correct |
13 |
Correct |
13 ms |
27632 KB |
Output is correct |
14 |
Correct |
15 ms |
27632 KB |
Output is correct |
15 |
Correct |
13 ms |
27708 KB |
Output is correct |
16 |
Correct |
17 ms |
27628 KB |
Output is correct |
17 |
Correct |
15 ms |
27624 KB |
Output is correct |
18 |
Correct |
16 ms |
27708 KB |
Output is correct |
19 |
Correct |
14 ms |
27732 KB |
Output is correct |
20 |
Correct |
14 ms |
27628 KB |
Output is correct |
21 |
Correct |
15 ms |
27724 KB |
Output is correct |
22 |
Correct |
15 ms |
27772 KB |
Output is correct |
23 |
Correct |
14 ms |
27756 KB |
Output is correct |
24 |
Correct |
15 ms |
27768 KB |
Output is correct |
25 |
Correct |
13 ms |
27712 KB |
Output is correct |
26 |
Correct |
16 ms |
27768 KB |
Output is correct |
27 |
Correct |
16 ms |
27768 KB |
Output is correct |
28 |
Correct |
16 ms |
27772 KB |
Output is correct |
29 |
Correct |
17 ms |
27784 KB |
Output is correct |
30 |
Correct |
17 ms |
27772 KB |
Output is correct |
31 |
Correct |
13 ms |
27732 KB |
Output is correct |
32 |
Correct |
14 ms |
27696 KB |
Output is correct |
33 |
Correct |
13 ms |
27732 KB |
Output is correct |
34 |
Correct |
13 ms |
27812 KB |
Output is correct |
35 |
Correct |
14 ms |
27812 KB |
Output is correct |
36 |
Correct |
15 ms |
27732 KB |
Output is correct |
37 |
Correct |
18 ms |
27764 KB |
Output is correct |
38 |
Correct |
21 ms |
27724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
12 ms |
27652 KB |
Output is correct |
4 |
Correct |
16 ms |
27740 KB |
Output is correct |
5 |
Correct |
12 ms |
27632 KB |
Output is correct |
6 |
Correct |
14 ms |
27732 KB |
Output is correct |
7 |
Correct |
12 ms |
27636 KB |
Output is correct |
8 |
Correct |
12 ms |
27732 KB |
Output is correct |
9 |
Correct |
14 ms |
27732 KB |
Output is correct |
10 |
Correct |
13 ms |
27628 KB |
Output is correct |
11 |
Correct |
12 ms |
27744 KB |
Output is correct |
12 |
Correct |
12 ms |
27632 KB |
Output is correct |
13 |
Correct |
13 ms |
27632 KB |
Output is correct |
14 |
Correct |
15 ms |
27632 KB |
Output is correct |
15 |
Correct |
13 ms |
27708 KB |
Output is correct |
16 |
Correct |
17 ms |
27628 KB |
Output is correct |
17 |
Correct |
15 ms |
27624 KB |
Output is correct |
18 |
Correct |
16 ms |
27708 KB |
Output is correct |
19 |
Correct |
108 ms |
34392 KB |
Output is correct |
20 |
Correct |
146 ms |
34272 KB |
Output is correct |
21 |
Correct |
135 ms |
34416 KB |
Output is correct |
22 |
Correct |
108 ms |
34504 KB |
Output is correct |
23 |
Correct |
64 ms |
34740 KB |
Output is correct |
24 |
Correct |
66 ms |
34592 KB |
Output is correct |
25 |
Correct |
107 ms |
37144 KB |
Output is correct |
26 |
Correct |
96 ms |
40088 KB |
Output is correct |
27 |
Correct |
135 ms |
41492 KB |
Output is correct |
28 |
Correct |
188 ms |
52852 KB |
Output is correct |
29 |
Correct |
190 ms |
51956 KB |
Output is correct |
30 |
Correct |
133 ms |
41460 KB |
Output is correct |
31 |
Correct |
148 ms |
41488 KB |
Output is correct |
32 |
Correct |
170 ms |
41564 KB |
Output is correct |
33 |
Correct |
195 ms |
40260 KB |
Output is correct |
34 |
Correct |
143 ms |
41236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
11 ms |
27740 KB |
Output is correct |
3 |
Correct |
12 ms |
27652 KB |
Output is correct |
4 |
Correct |
16 ms |
27740 KB |
Output is correct |
5 |
Correct |
12 ms |
27632 KB |
Output is correct |
6 |
Correct |
14 ms |
27732 KB |
Output is correct |
7 |
Correct |
12 ms |
27636 KB |
Output is correct |
8 |
Correct |
12 ms |
27732 KB |
Output is correct |
9 |
Correct |
14 ms |
27732 KB |
Output is correct |
10 |
Correct |
13 ms |
27628 KB |
Output is correct |
11 |
Correct |
12 ms |
27744 KB |
Output is correct |
12 |
Correct |
12 ms |
27632 KB |
Output is correct |
13 |
Correct |
13 ms |
27632 KB |
Output is correct |
14 |
Correct |
15 ms |
27632 KB |
Output is correct |
15 |
Correct |
13 ms |
27708 KB |
Output is correct |
16 |
Correct |
17 ms |
27628 KB |
Output is correct |
17 |
Correct |
15 ms |
27624 KB |
Output is correct |
18 |
Correct |
16 ms |
27708 KB |
Output is correct |
19 |
Correct |
14 ms |
27732 KB |
Output is correct |
20 |
Correct |
14 ms |
27628 KB |
Output is correct |
21 |
Correct |
15 ms |
27724 KB |
Output is correct |
22 |
Correct |
15 ms |
27772 KB |
Output is correct |
23 |
Correct |
14 ms |
27756 KB |
Output is correct |
24 |
Correct |
15 ms |
27768 KB |
Output is correct |
25 |
Correct |
13 ms |
27712 KB |
Output is correct |
26 |
Correct |
16 ms |
27768 KB |
Output is correct |
27 |
Correct |
16 ms |
27768 KB |
Output is correct |
28 |
Correct |
16 ms |
27772 KB |
Output is correct |
29 |
Correct |
17 ms |
27784 KB |
Output is correct |
30 |
Correct |
17 ms |
27772 KB |
Output is correct |
31 |
Correct |
13 ms |
27732 KB |
Output is correct |
32 |
Correct |
14 ms |
27696 KB |
Output is correct |
33 |
Correct |
13 ms |
27732 KB |
Output is correct |
34 |
Correct |
13 ms |
27812 KB |
Output is correct |
35 |
Correct |
14 ms |
27812 KB |
Output is correct |
36 |
Correct |
15 ms |
27732 KB |
Output is correct |
37 |
Correct |
18 ms |
27764 KB |
Output is correct |
38 |
Correct |
21 ms |
27724 KB |
Output is correct |
39 |
Correct |
108 ms |
34392 KB |
Output is correct |
40 |
Correct |
146 ms |
34272 KB |
Output is correct |
41 |
Correct |
135 ms |
34416 KB |
Output is correct |
42 |
Correct |
108 ms |
34504 KB |
Output is correct |
43 |
Correct |
64 ms |
34740 KB |
Output is correct |
44 |
Correct |
66 ms |
34592 KB |
Output is correct |
45 |
Correct |
107 ms |
37144 KB |
Output is correct |
46 |
Correct |
96 ms |
40088 KB |
Output is correct |
47 |
Correct |
135 ms |
41492 KB |
Output is correct |
48 |
Correct |
188 ms |
52852 KB |
Output is correct |
49 |
Correct |
190 ms |
51956 KB |
Output is correct |
50 |
Correct |
133 ms |
41460 KB |
Output is correct |
51 |
Correct |
148 ms |
41488 KB |
Output is correct |
52 |
Correct |
170 ms |
41564 KB |
Output is correct |
53 |
Correct |
195 ms |
40260 KB |
Output is correct |
54 |
Correct |
143 ms |
41236 KB |
Output is correct |
55 |
Correct |
22 ms |
28372 KB |
Output is correct |
56 |
Correct |
22 ms |
28280 KB |
Output is correct |
57 |
Correct |
93 ms |
34632 KB |
Output is correct |
58 |
Correct |
44 ms |
34368 KB |
Output is correct |
59 |
Correct |
104 ms |
40052 KB |
Output is correct |
60 |
Correct |
343 ms |
52064 KB |
Output is correct |
61 |
Correct |
143 ms |
41656 KB |
Output is correct |
62 |
Correct |
162 ms |
41576 KB |
Output is correct |
63 |
Correct |
199 ms |
41584 KB |
Output is correct |
64 |
Correct |
394 ms |
41828 KB |
Output is correct |
65 |
Correct |
148 ms |
42316 KB |
Output is correct |
66 |
Correct |
293 ms |
50036 KB |
Output is correct |
67 |
Correct |
89 ms |
42244 KB |
Output is correct |
68 |
Correct |
223 ms |
42400 KB |
Output is correct |
69 |
Correct |
202 ms |
42444 KB |
Output is correct |
70 |
Correct |
198 ms |
41700 KB |
Output is correct |