#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
// global
int ans = 1e9, n, k;
vector<pair<int,long long> > v[200005];
// centroid
int sz[200005];
bool visited[200005];
// query & update
map<long long,int> mp;
void query(int nw,int fa,int dep,long long total)
{
if(mp.count(k - total))
ans = min(ans, dep + mp[k - total]);
for(auto go: v[nw])
if(go.f != fa && !visited[go.f])
query(go.f, nw, dep + 1, total + go.s);
}
void update(int nw,int fa,int dep, long long total)
{
if(mp.count(total))
mp[total] = min(mp[total], dep);
else
mp[total] = dep;
for(auto go: v[nw])
if(go.f != fa && !visited[go.f])
update(go.f, nw, dep + 1, total + go.s);
}
int re_size(int nw,int fa)
{
sz[nw] = 1;
for(auto go: v[nw])
if(go.f != fa && !visited[go.f])
sz[nw] += re_size(go.f,nw);
return sz[nw];
}
int fi_cen(int nw,int fa,int sum)
{
for(auto go: v[nw])
if(go.f != fa && !visited[go.f] && sz[go.f] > sum/2)
return fi_cen(go.f,nw,sum);
return nw;
}
void centroid_decomp(int nw)
{
int cen = fi_cen(nw, -1, re_size(nw, -1));
visited[cen] = true;
mp.clear();
mp.emplace(0, 0);
for(auto go: v[cen])
{
if(!visited[go.f])
{
query(go.f, cen, 1, go.s);
update(go.f, cen, 1, go.s);
}
}
for(auto go: v[cen])
if(!visited[go.f])
centroid_decomp(go.f);
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N, k = K;
for(int i = 0; i < n - 1; i++)
{
v[H[i][0]].pb({H[i][1], L[i]});
v[H[i][1]].pb({H[i][0], L[i]});
}
centroid_decomp(0);
if(ans == 1e9)
return -1;
return ans;
}
// int a,b;
// int send[200005][2],send2[200005];
// int main()
// {
// scanf("%d %d",&a,&b);
// for(int i = 0; i < a - 1; i++)
// scanf("%d %d %d",&send[i][0],&send[i][1],&send2[i]);
// printf("%d",best_path(a, b, send, send2));
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4916 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 |
4948 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 |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 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 |
4916 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 |
4948 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 |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 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 |
3 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 |
5084 KB |
Output is correct |
26 |
Correct |
4 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 |
4 ms |
5088 KB |
Output is correct |
34 |
Correct |
4 ms |
5144 KB |
Output is correct |
35 |
Correct |
4 ms |
5144 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 |
4916 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 |
4948 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 |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 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 |
244 ms |
11904 KB |
Output is correct |
20 |
Correct |
244 ms |
11776 KB |
Output is correct |
21 |
Correct |
233 ms |
11680 KB |
Output is correct |
22 |
Correct |
211 ms |
11732 KB |
Output is correct |
23 |
Correct |
280 ms |
12108 KB |
Output is correct |
24 |
Correct |
126 ms |
11380 KB |
Output is correct |
25 |
Correct |
180 ms |
15576 KB |
Output is correct |
26 |
Correct |
96 ms |
17504 KB |
Output is correct |
27 |
Correct |
291 ms |
19140 KB |
Output is correct |
28 |
Correct |
877 ms |
42580 KB |
Output is correct |
29 |
Correct |
862 ms |
41756 KB |
Output is correct |
30 |
Correct |
302 ms |
19244 KB |
Output is correct |
31 |
Correct |
289 ms |
19248 KB |
Output is correct |
32 |
Correct |
352 ms |
19256 KB |
Output is correct |
33 |
Correct |
443 ms |
17940 KB |
Output is correct |
34 |
Correct |
828 ms |
30220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4916 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 |
4948 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 |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4944 KB |
Output is correct |
16 |
Correct |
3 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 |
3 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 |
5084 KB |
Output is correct |
26 |
Correct |
4 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 |
4 ms |
5088 KB |
Output is correct |
34 |
Correct |
4 ms |
5144 KB |
Output is correct |
35 |
Correct |
4 ms |
5144 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 |
244 ms |
11904 KB |
Output is correct |
40 |
Correct |
244 ms |
11776 KB |
Output is correct |
41 |
Correct |
233 ms |
11680 KB |
Output is correct |
42 |
Correct |
211 ms |
11732 KB |
Output is correct |
43 |
Correct |
280 ms |
12108 KB |
Output is correct |
44 |
Correct |
126 ms |
11380 KB |
Output is correct |
45 |
Correct |
180 ms |
15576 KB |
Output is correct |
46 |
Correct |
96 ms |
17504 KB |
Output is correct |
47 |
Correct |
291 ms |
19140 KB |
Output is correct |
48 |
Correct |
877 ms |
42580 KB |
Output is correct |
49 |
Correct |
862 ms |
41756 KB |
Output is correct |
50 |
Correct |
302 ms |
19244 KB |
Output is correct |
51 |
Correct |
289 ms |
19248 KB |
Output is correct |
52 |
Correct |
352 ms |
19256 KB |
Output is correct |
53 |
Correct |
443 ms |
17940 KB |
Output is correct |
54 |
Correct |
828 ms |
30220 KB |
Output is correct |
55 |
Correct |
23 ms |
5972 KB |
Output is correct |
56 |
Correct |
15 ms |
5588 KB |
Output is correct |
57 |
Correct |
136 ms |
12200 KB |
Output is correct |
58 |
Correct |
45 ms |
11276 KB |
Output is correct |
59 |
Correct |
267 ms |
22488 KB |
Output is correct |
60 |
Correct |
877 ms |
44008 KB |
Output is correct |
61 |
Correct |
339 ms |
23904 KB |
Output is correct |
62 |
Correct |
298 ms |
22332 KB |
Output is correct |
63 |
Correct |
358 ms |
22300 KB |
Output is correct |
64 |
Correct |
1048 ms |
28136 KB |
Output is correct |
65 |
Correct |
916 ms |
34860 KB |
Output is correct |
66 |
Correct |
913 ms |
43004 KB |
Output is correct |
67 |
Correct |
127 ms |
21168 KB |
Output is correct |
68 |
Correct |
481 ms |
32836 KB |
Output is correct |
69 |
Correct |
483 ms |
32740 KB |
Output is correct |
70 |
Correct |
449 ms |
31600 KB |
Output is correct |