#include<bits/stdc++.h>
#include "race.h"
#define maxn 200010
#define maxk 1000010
#define f first
#define s second
#define ll long long
using namespace std;
int n,k;
int subsz[maxn],cnt[maxk];
bool is_cen[maxn];
int mx_sz=0;
int ans=INT_MAX;
vector<pair<int,int>> edges[maxn];
int get_size(int start,int parent=-1)
{
int sz=1;
for(auto e:edges[start])
{
if(e.f==parent || is_cen[e.f]) continue;
sz+=get_size(e.f,start);
}
return subsz[start]=sz;
}
int find_centroid(int start,int tree_sz,int parent=-1)
{
for(auto e:edges[start])
{
if(e.f==parent || is_cen[e.f]) continue;
if(2*subsz[e.f]>tree_sz) return find_centroid(e.f,tree_sz,start);
}
return start;
}
void get_cnt(int start,bool filling,int parent,int dist,int depth=1)
{
if(dist>k) return;
mx_sz=max(mx_sz,dist);
if(filling)
{
//cnt[depth]++;
//if(cnt[dist]==INT_MAX) cnt[dist]
cnt[dist]=min(cnt[dist],depth);
}
else if(cnt[k-dist]!=INT_MAX) ans=min(ans,cnt[k-dist]+depth);
for(auto e:edges[start])
{
if(is_cen[e.f] || e.f==parent) continue;
get_cnt(e.f,filling,start,dist+e.s,depth+1);
}
}
void decomp(int start=1)
{
int centroid=find_centroid(start,get_size(start));
is_cen[centroid]=1;
mx_sz=0;
for(auto e:edges[centroid])
{
if(is_cen[e.f]) continue;
get_cnt(e.f,false,centroid,e.s);
get_cnt(e.f,true,centroid,e.s);
}
for(int i=1;i<=mx_sz;i++) cnt[i]=INT_MAX;
for(auto e:edges[centroid])
{
if(is_cen[e.f]) continue;
decomp(e.f);
}
}
int best_path(int N,int K,int H[][2],int L[])
{
/*ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>k;*/
n=N;
k=K;
for(int i=1;i<=k;i++) cnt[i]=INT_MAX;
for(int i=0;i<n-1;i++)
{
int a=H[i][0],b=H[i][1],w=L[i];
//cin>>a>>b>>w;
a++;
b++;
edges[a].push_back({b,w});
edges[b].push_back({a,w});
}
decomp();
if(ans!=INT_MAX) return ans;
else return -1;
}
/*
3 3
0 1 1
1 2 1
*/
/*
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10580 KB |
Output is correct |
4 |
Correct |
2 ms |
10580 KB |
Output is correct |
5 |
Correct |
2 ms |
10580 KB |
Output is correct |
6 |
Correct |
2 ms |
10580 KB |
Output is correct |
7 |
Correct |
2 ms |
10580 KB |
Output is correct |
8 |
Correct |
2 ms |
10708 KB |
Output is correct |
9 |
Correct |
2 ms |
10580 KB |
Output is correct |
10 |
Correct |
2 ms |
10580 KB |
Output is correct |
11 |
Correct |
2 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10744 KB |
Output is correct |
13 |
Correct |
2 ms |
10580 KB |
Output is correct |
14 |
Correct |
2 ms |
10580 KB |
Output is correct |
15 |
Correct |
2 ms |
10580 KB |
Output is correct |
16 |
Correct |
2 ms |
10580 KB |
Output is correct |
17 |
Correct |
2 ms |
10580 KB |
Output is correct |
18 |
Correct |
3 ms |
10580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10580 KB |
Output is correct |
4 |
Correct |
2 ms |
10580 KB |
Output is correct |
5 |
Correct |
2 ms |
10580 KB |
Output is correct |
6 |
Correct |
2 ms |
10580 KB |
Output is correct |
7 |
Correct |
2 ms |
10580 KB |
Output is correct |
8 |
Correct |
2 ms |
10708 KB |
Output is correct |
9 |
Correct |
2 ms |
10580 KB |
Output is correct |
10 |
Correct |
2 ms |
10580 KB |
Output is correct |
11 |
Correct |
2 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10744 KB |
Output is correct |
13 |
Correct |
2 ms |
10580 KB |
Output is correct |
14 |
Correct |
2 ms |
10580 KB |
Output is correct |
15 |
Correct |
2 ms |
10580 KB |
Output is correct |
16 |
Correct |
2 ms |
10580 KB |
Output is correct |
17 |
Correct |
2 ms |
10580 KB |
Output is correct |
18 |
Correct |
3 ms |
10580 KB |
Output is correct |
19 |
Correct |
2 ms |
10580 KB |
Output is correct |
20 |
Correct |
2 ms |
10580 KB |
Output is correct |
21 |
Correct |
2 ms |
10580 KB |
Output is correct |
22 |
Correct |
103 ms |
12816 KB |
Output is correct |
23 |
Correct |
74 ms |
12628 KB |
Output is correct |
24 |
Correct |
88 ms |
12628 KB |
Output is correct |
25 |
Correct |
16 ms |
12628 KB |
Output is correct |
26 |
Correct |
35 ms |
10760 KB |
Output is correct |
27 |
Correct |
3 ms |
12628 KB |
Output is correct |
28 |
Correct |
13 ms |
10580 KB |
Output is correct |
29 |
Correct |
13 ms |
10768 KB |
Output is correct |
30 |
Correct |
14 ms |
10580 KB |
Output is correct |
31 |
Correct |
14 ms |
12628 KB |
Output is correct |
32 |
Correct |
14 ms |
12628 KB |
Output is correct |
33 |
Correct |
18 ms |
12628 KB |
Output is correct |
34 |
Correct |
20 ms |
12628 KB |
Output is correct |
35 |
Correct |
22 ms |
12628 KB |
Output is correct |
36 |
Correct |
24 ms |
12628 KB |
Output is correct |
37 |
Correct |
5 ms |
12628 KB |
Output is correct |
38 |
Correct |
6 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10580 KB |
Output is correct |
4 |
Correct |
2 ms |
10580 KB |
Output is correct |
5 |
Correct |
2 ms |
10580 KB |
Output is correct |
6 |
Correct |
2 ms |
10580 KB |
Output is correct |
7 |
Correct |
2 ms |
10580 KB |
Output is correct |
8 |
Correct |
2 ms |
10708 KB |
Output is correct |
9 |
Correct |
2 ms |
10580 KB |
Output is correct |
10 |
Correct |
2 ms |
10580 KB |
Output is correct |
11 |
Correct |
2 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10744 KB |
Output is correct |
13 |
Correct |
2 ms |
10580 KB |
Output is correct |
14 |
Correct |
2 ms |
10580 KB |
Output is correct |
15 |
Correct |
2 ms |
10580 KB |
Output is correct |
16 |
Correct |
2 ms |
10580 KB |
Output is correct |
17 |
Correct |
2 ms |
10580 KB |
Output is correct |
18 |
Correct |
3 ms |
10580 KB |
Output is correct |
19 |
Correct |
82 ms |
18252 KB |
Output is correct |
20 |
Correct |
66 ms |
18260 KB |
Output is correct |
21 |
Correct |
71 ms |
18428 KB |
Output is correct |
22 |
Correct |
69 ms |
18252 KB |
Output is correct |
23 |
Correct |
42 ms |
18520 KB |
Output is correct |
24 |
Correct |
32 ms |
18508 KB |
Output is correct |
25 |
Correct |
70 ms |
21076 KB |
Output is correct |
26 |
Correct |
70 ms |
24060 KB |
Output is correct |
27 |
Correct |
86 ms |
24140 KB |
Output is correct |
28 |
Correct |
139 ms |
35444 KB |
Output is correct |
29 |
Correct |
139 ms |
34632 KB |
Output is correct |
30 |
Correct |
84 ms |
24140 KB |
Output is correct |
31 |
Correct |
108 ms |
24052 KB |
Output is correct |
32 |
Correct |
98 ms |
24140 KB |
Output is correct |
33 |
Correct |
108 ms |
22860 KB |
Output is correct |
34 |
Correct |
95 ms |
23884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
10580 KB |
Output is correct |
3 |
Correct |
2 ms |
10580 KB |
Output is correct |
4 |
Correct |
2 ms |
10580 KB |
Output is correct |
5 |
Correct |
2 ms |
10580 KB |
Output is correct |
6 |
Correct |
2 ms |
10580 KB |
Output is correct |
7 |
Correct |
2 ms |
10580 KB |
Output is correct |
8 |
Correct |
2 ms |
10708 KB |
Output is correct |
9 |
Correct |
2 ms |
10580 KB |
Output is correct |
10 |
Correct |
2 ms |
10580 KB |
Output is correct |
11 |
Correct |
2 ms |
10580 KB |
Output is correct |
12 |
Correct |
2 ms |
10744 KB |
Output is correct |
13 |
Correct |
2 ms |
10580 KB |
Output is correct |
14 |
Correct |
2 ms |
10580 KB |
Output is correct |
15 |
Correct |
2 ms |
10580 KB |
Output is correct |
16 |
Correct |
2 ms |
10580 KB |
Output is correct |
17 |
Correct |
2 ms |
10580 KB |
Output is correct |
18 |
Correct |
3 ms |
10580 KB |
Output is correct |
19 |
Correct |
2 ms |
10580 KB |
Output is correct |
20 |
Correct |
2 ms |
10580 KB |
Output is correct |
21 |
Correct |
2 ms |
10580 KB |
Output is correct |
22 |
Correct |
103 ms |
12816 KB |
Output is correct |
23 |
Correct |
74 ms |
12628 KB |
Output is correct |
24 |
Correct |
88 ms |
12628 KB |
Output is correct |
25 |
Correct |
16 ms |
12628 KB |
Output is correct |
26 |
Correct |
35 ms |
10760 KB |
Output is correct |
27 |
Correct |
3 ms |
12628 KB |
Output is correct |
28 |
Correct |
13 ms |
10580 KB |
Output is correct |
29 |
Correct |
13 ms |
10768 KB |
Output is correct |
30 |
Correct |
14 ms |
10580 KB |
Output is correct |
31 |
Correct |
14 ms |
12628 KB |
Output is correct |
32 |
Correct |
14 ms |
12628 KB |
Output is correct |
33 |
Correct |
18 ms |
12628 KB |
Output is correct |
34 |
Correct |
20 ms |
12628 KB |
Output is correct |
35 |
Correct |
22 ms |
12628 KB |
Output is correct |
36 |
Correct |
24 ms |
12628 KB |
Output is correct |
37 |
Correct |
5 ms |
12628 KB |
Output is correct |
38 |
Correct |
6 ms |
12628 KB |
Output is correct |
39 |
Correct |
82 ms |
18252 KB |
Output is correct |
40 |
Correct |
66 ms |
18260 KB |
Output is correct |
41 |
Correct |
71 ms |
18428 KB |
Output is correct |
42 |
Correct |
69 ms |
18252 KB |
Output is correct |
43 |
Correct |
42 ms |
18520 KB |
Output is correct |
44 |
Correct |
32 ms |
18508 KB |
Output is correct |
45 |
Correct |
70 ms |
21076 KB |
Output is correct |
46 |
Correct |
70 ms |
24060 KB |
Output is correct |
47 |
Correct |
86 ms |
24140 KB |
Output is correct |
48 |
Correct |
139 ms |
35444 KB |
Output is correct |
49 |
Correct |
139 ms |
34632 KB |
Output is correct |
50 |
Correct |
84 ms |
24140 KB |
Output is correct |
51 |
Correct |
108 ms |
24052 KB |
Output is correct |
52 |
Correct |
98 ms |
24140 KB |
Output is correct |
53 |
Correct |
108 ms |
22860 KB |
Output is correct |
54 |
Correct |
95 ms |
23884 KB |
Output is correct |
55 |
Correct |
9 ms |
11304 KB |
Output is correct |
56 |
Correct |
8 ms |
11276 KB |
Output is correct |
57 |
Correct |
48 ms |
18544 KB |
Output is correct |
58 |
Correct |
25 ms |
18128 KB |
Output is correct |
59 |
Correct |
67 ms |
23884 KB |
Output is correct |
60 |
Correct |
228 ms |
36960 KB |
Output is correct |
61 |
Correct |
105 ms |
24176 KB |
Output is correct |
62 |
Correct |
123 ms |
26444 KB |
Output is correct |
63 |
Correct |
128 ms |
26480 KB |
Output is correct |
64 |
Correct |
631 ms |
26444 KB |
Output is correct |
65 |
Correct |
100 ms |
24908 KB |
Output is correct |
66 |
Correct |
421 ms |
34892 KB |
Output is correct |
67 |
Correct |
57 ms |
27580 KB |
Output is correct |
68 |
Correct |
234 ms |
27092 KB |
Output is correct |
69 |
Correct |
211 ms |
27224 KB |
Output is correct |
70 |
Correct |
217 ms |
26700 KB |
Output is correct |