#include "race.h"
#include <bits/stdc++.h>
#define xx first
#define yy second
using namespace std;
long long int typedef li;
vector<vector<pair<int,int>>> g(200005);
int ch[200005],ans=1e9;
bitset<200005> vis;
li K;
void dfs(int p, int q)
{
ch[p]=1;
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) dfs(it,p),ch[p]+=ch[it];
}
int centroid(int p, int q, int s)
{
int c=p;
for (auto [it,w] : g[p]) if (it!=q && !vis[it])
{
int tmp=centroid(it,p,s);
if (tmp!=-1) return tmp;
if (2*ch[it]>s) c=-1;
}
if (2*ch[p]<s) c=-1;
return c;
}
map<li,int> dub;
void add(int p, int q, li s, int d)
{
auto it=dub.find(s);
if (it==dub.end()) dub[s]=d;
else it->yy=min(it->yy,d);
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) add(it,p,s+w,d+1);
}
void iter(int p, int q, li s, int d)
{
auto it=dub.find(K-s);
if (it!=dub.end()) ans=min(ans,d+it->yy);
for (auto [it,w] : g[p]) if (it!=q && !vis[it]) iter(it,p,s+w,d+1);
}
void decomp(int p)
{
dfs(p,-1);
int c=centroid(p,-1,ch[p]);
//cout<<","<<c<<endl;
dub.clear(); dub[0]=0;
for (auto [it,w] : g[c]) if (!vis[it]) iter(it,c,w,1),add(it,c,w,1);
vis[c]=1;
for (auto [it,w] : g[c]) if (!vis[it]) decomp(it);
}
int best_path(int n, int k, int H[][2], int L[])
{
for (int i=0;i<n-1;i++) g[H[i][0]].push_back({H[i][1],L[i]}),g[H[i][1]].push_back({H[i][0],L[i]});
K=k,decomp(0); return (ans==1e9?-1:ans);
}
/*
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
7 3
0 1 1
0 4 1
4 5 1
5 6 1
1 2 3
1 3 2
7 8
0 1 1
0 4 1
4 5 1
5 6 1
1 2 3
1 3 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
6 ms |
5144 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
257 ms |
10152 KB |
Output is correct |
20 |
Correct |
232 ms |
10256 KB |
Output is correct |
21 |
Correct |
210 ms |
10288 KB |
Output is correct |
22 |
Correct |
202 ms |
10228 KB |
Output is correct |
23 |
Correct |
240 ms |
10480 KB |
Output is correct |
24 |
Correct |
117 ms |
10424 KB |
Output is correct |
25 |
Correct |
164 ms |
14184 KB |
Output is correct |
26 |
Correct |
98 ms |
17476 KB |
Output is correct |
27 |
Correct |
278 ms |
15936 KB |
Output is correct |
28 |
Correct |
866 ms |
42376 KB |
Output is correct |
29 |
Correct |
864 ms |
41992 KB |
Output is correct |
30 |
Correct |
293 ms |
16708 KB |
Output is correct |
31 |
Correct |
279 ms |
16664 KB |
Output is correct |
32 |
Correct |
355 ms |
16728 KB |
Output is correct |
33 |
Correct |
368 ms |
15412 KB |
Output is correct |
34 |
Correct |
853 ms |
27756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5028 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
4 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
5 ms |
5076 KB |
Output is correct |
33 |
Correct |
6 ms |
5144 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
257 ms |
10152 KB |
Output is correct |
40 |
Correct |
232 ms |
10256 KB |
Output is correct |
41 |
Correct |
210 ms |
10288 KB |
Output is correct |
42 |
Correct |
202 ms |
10228 KB |
Output is correct |
43 |
Correct |
240 ms |
10480 KB |
Output is correct |
44 |
Correct |
117 ms |
10424 KB |
Output is correct |
45 |
Correct |
164 ms |
14184 KB |
Output is correct |
46 |
Correct |
98 ms |
17476 KB |
Output is correct |
47 |
Correct |
278 ms |
15936 KB |
Output is correct |
48 |
Correct |
866 ms |
42376 KB |
Output is correct |
49 |
Correct |
864 ms |
41992 KB |
Output is correct |
50 |
Correct |
293 ms |
16708 KB |
Output is correct |
51 |
Correct |
279 ms |
16664 KB |
Output is correct |
52 |
Correct |
355 ms |
16728 KB |
Output is correct |
53 |
Correct |
368 ms |
15412 KB |
Output is correct |
54 |
Correct |
853 ms |
27756 KB |
Output is correct |
55 |
Correct |
23 ms |
5928 KB |
Output is correct |
56 |
Correct |
15 ms |
5588 KB |
Output is correct |
57 |
Correct |
127 ms |
11288 KB |
Output is correct |
58 |
Correct |
43 ms |
11252 KB |
Output is correct |
59 |
Correct |
251 ms |
23020 KB |
Output is correct |
60 |
Correct |
895 ms |
41420 KB |
Output is correct |
61 |
Correct |
340 ms |
18308 KB |
Output is correct |
62 |
Correct |
310 ms |
16620 KB |
Output is correct |
63 |
Correct |
396 ms |
16624 KB |
Output is correct |
64 |
Correct |
1291 ms |
22236 KB |
Output is correct |
65 |
Correct |
1054 ms |
28820 KB |
Output is correct |
66 |
Correct |
1045 ms |
39476 KB |
Output is correct |
67 |
Correct |
147 ms |
16748 KB |
Output is correct |
68 |
Correct |
598 ms |
27208 KB |
Output is correct |
69 |
Correct |
533 ms |
27484 KB |
Output is correct |
70 |
Correct |
494 ms |
26420 KB |
Output is correct |