#include <iostream>
#include <vector>
#include <utility>
using namespace std;
vector<pair<int , int>> adjlist[200001];
bool used[200001];
int subtree[200001];
int ans = 1e9 , target;
int cnt_subtree(int node , int bef){
subtree[node] = 1;
for (const pair<int , int> &it : adjlist[node])
if (it.first != bef && !used[it.first])subtree[node] += cnt_subtree(it.first , node);
return subtree[node];
}
int cari_centroid(int node , int bef , const int &max_bound){
for (const pair<int , int> &it : adjlist[node])
if (!used[it.first] && it.first != bef && subtree[it.first] > max_bound)
return cari_centroid(it.first , node , max_bound);
return node;
}
void dfs(int node , int bef , int depth , int val , int min_len[]){
if (min_len[target - val])
ans = min(ans , depth + min_len[target - val] - 2);
for (const pair<int , int> &it : adjlist[node])
if (it.first != bef && !used[it.first] && val + it.second <= target)
dfs(it.first , node , depth + 1 , val + it.second , min_len);
if (!min_len[val])
min_len[val] = depth;
else min_len[val] = min(min_len[val] , depth);
}
void cari(int now){
int centroid = cari_centroid(now , -1 , cnt_subtree(now , -1) >> 1);
int min_len[target + 1] = {};
min_len[0] = used[centroid] = 1;
for (const pair<int , int> &it : adjlist[centroid])
if (!used[it.first] && it.second <= target)
dfs(it.first , centroid , 2 , it.second , min_len);
for (const pair<int , int> &it : adjlist[centroid])
if (!used[it.first])
cari(it.first);
}
int best_path(int N, int K, int H[][2], int L[]) //K : adalah nilai yang dibutuhkan
{
target = K;
for (int i = 0 ; i < N - 1 ; ++i)
adjlist[H[i][0]].push_back({H[i][1] , L[i]}),
adjlist[H[i][1]].push_back({H[i][0] , L[i]});
cari(0);
return ans > 1e8 ? -1 : ans;
}
/*int main(){
int N , K;
cin >> N >> K;
int arr[N - 1][2] , L[N - 1];
for (int i = 0 ; i < N - 1 ; ++i)
cin >> arr[i][0] >> arr[i][1];
for (int i = 0 ; i < N - 1 ; ++i)
cin >> L[i];
cout << best_path(N , K , arr , L) << endl;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
559 ms |
41204 KB |
Output is correct |
23 |
Correct |
448 ms |
34660 KB |
Output is correct |
24 |
Correct |
515 ms |
32228 KB |
Output is correct |
25 |
Correct |
502 ms |
34916 KB |
Output is correct |
26 |
Correct |
181 ms |
15724 KB |
Output is correct |
27 |
Correct |
494 ms |
33380 KB |
Output is correct |
28 |
Correct |
107 ms |
12140 KB |
Output is correct |
29 |
Correct |
171 ms |
16484 KB |
Output is correct |
30 |
Correct |
194 ms |
18020 KB |
Output is correct |
31 |
Correct |
385 ms |
28260 KB |
Output is correct |
32 |
Correct |
437 ms |
32868 KB |
Output is correct |
33 |
Correct |
464 ms |
35684 KB |
Output is correct |
34 |
Correct |
324 ms |
28136 KB |
Output is correct |
35 |
Correct |
456 ms |
36936 KB |
Output is correct |
36 |
Correct |
506 ms |
41572 KB |
Output is correct |
37 |
Correct |
420 ms |
36196 KB |
Output is correct |
38 |
Correct |
276 ms |
25060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
143 ms |
11748 KB |
Output is correct |
20 |
Correct |
148 ms |
11880 KB |
Output is correct |
21 |
Correct |
147 ms |
11752 KB |
Output is correct |
22 |
Correct |
154 ms |
11908 KB |
Output is correct |
23 |
Correct |
97 ms |
12132 KB |
Output is correct |
24 |
Correct |
76 ms |
12004 KB |
Output is correct |
25 |
Correct |
135 ms |
14432 KB |
Output is correct |
26 |
Correct |
93 ms |
17508 KB |
Output is correct |
27 |
Correct |
204 ms |
18888 KB |
Output is correct |
28 |
Correct |
347 ms |
30180 KB |
Output is correct |
29 |
Correct |
355 ms |
29412 KB |
Output is correct |
30 |
Correct |
209 ms |
18916 KB |
Output is correct |
31 |
Correct |
216 ms |
18776 KB |
Output is correct |
32 |
Correct |
251 ms |
19044 KB |
Output is correct |
33 |
Correct |
245 ms |
17896 KB |
Output is correct |
34 |
Correct |
246 ms |
18532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
5 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
559 ms |
41204 KB |
Output is correct |
23 |
Correct |
448 ms |
34660 KB |
Output is correct |
24 |
Correct |
515 ms |
32228 KB |
Output is correct |
25 |
Correct |
502 ms |
34916 KB |
Output is correct |
26 |
Correct |
181 ms |
15724 KB |
Output is correct |
27 |
Correct |
494 ms |
33380 KB |
Output is correct |
28 |
Correct |
107 ms |
12140 KB |
Output is correct |
29 |
Correct |
171 ms |
16484 KB |
Output is correct |
30 |
Correct |
194 ms |
18020 KB |
Output is correct |
31 |
Correct |
385 ms |
28260 KB |
Output is correct |
32 |
Correct |
437 ms |
32868 KB |
Output is correct |
33 |
Correct |
464 ms |
35684 KB |
Output is correct |
34 |
Correct |
324 ms |
28136 KB |
Output is correct |
35 |
Correct |
456 ms |
36936 KB |
Output is correct |
36 |
Correct |
506 ms |
41572 KB |
Output is correct |
37 |
Correct |
420 ms |
36196 KB |
Output is correct |
38 |
Correct |
276 ms |
25060 KB |
Output is correct |
39 |
Correct |
143 ms |
11748 KB |
Output is correct |
40 |
Correct |
148 ms |
11880 KB |
Output is correct |
41 |
Correct |
147 ms |
11752 KB |
Output is correct |
42 |
Correct |
154 ms |
11908 KB |
Output is correct |
43 |
Correct |
97 ms |
12132 KB |
Output is correct |
44 |
Correct |
76 ms |
12004 KB |
Output is correct |
45 |
Correct |
135 ms |
14432 KB |
Output is correct |
46 |
Correct |
93 ms |
17508 KB |
Output is correct |
47 |
Correct |
204 ms |
18888 KB |
Output is correct |
48 |
Correct |
347 ms |
30180 KB |
Output is correct |
49 |
Correct |
355 ms |
29412 KB |
Output is correct |
50 |
Correct |
209 ms |
18916 KB |
Output is correct |
51 |
Correct |
216 ms |
18776 KB |
Output is correct |
52 |
Correct |
251 ms |
19044 KB |
Output is correct |
53 |
Correct |
245 ms |
17896 KB |
Output is correct |
54 |
Correct |
246 ms |
18532 KB |
Output is correct |
55 |
Correct |
35 ms |
5996 KB |
Output is correct |
56 |
Incorrect |
13 ms |
5740 KB |
Output isn't correct |
57 |
Halted |
0 ms |
0 KB |
- |