#include<bits/stdc++.h>
using namespace std ;
const int mx = 2e5 + 5 , ww = 1e7 + 6 ;
vector<pair<int,int> > adj[mx] ;
int subtree[mx] , vis[mx] , dist[ww] , n , ans = mx , k ;
void dfs(int s , int par)
{
int siz = adj[s].size() ;
subtree[s] = 1 ;
for(int i = 0 ; i < siz ; ++i)
{
if(adj[s][i].first!=par && vis[adj[s][i].first]==0)
{
dfs(adj[s][i].first,s) ;
subtree[s] += subtree[adj[s][i].first] ;
}
}
}
int centroid(int s , int par , int n)
{
int siz = adj[s].size() ;
for(int i = 0 ; i < siz ; ++i)
{
if(adj[s][i].first!=par && vis[adj[s][i].first]==0)
{
if((subtree[adj[s][i].first])>n) return centroid(adj[s][i].first,s,n) ;
}
}
return s ;
}
void findans(int s , int p , int num , int sum)
{
if(sum<=k) ans = min(ans,num + dist[k-sum]) ;
int siz = adj[s].size() ;
for(int i = 0 ; i < siz ; ++i)
{
if(vis[adj[s][i].first]==0 && adj[s][i].first!=p)
{
findans(adj[s][i].first,s,num+1,sum+adj[s][i].second) ;
}
}
}
void addcnt(int s , int p , int num , int sum)
{
if(sum<=k) dist[sum] = min(dist[sum],num) ;
int siz = adj[s].size() ;
for(int i = 0 ; i < siz ; ++i)
{
if(vis[adj[s][i].first]==0 && adj[s][i].first!=p) addcnt(adj[s][i].first,s,num+1,sum+adj[s][i].second) ;
}
}
void eras(int s , int p , int sum)
{
if(sum<=k) dist[sum] = mx ;
int siz = adj[s].size() ;
for(int i = 0 ; i < siz ; ++i)
{
if(adj[s][i].first != p && vis[adj[s][i].first]==0)
{
eras(adj[s][i].first,s,sum+adj[s][i].second) ;
}
}
}
void decomp(int s , int par)
{
dfs(s,par) ;
int c = centroid(s,par,subtree[s]/2) ;
vis[c] = 1 ;
int siz = adj[c].size() ;
dist[0] = 0 ;
for(int i = 0 ; i < siz ; ++i)
{
int v = adj[c][i].first ;
if(!vis[v])
{
findans(v,c,1,adj[c][i].second) ;
addcnt(v,c,1,adj[c][i].second) ;
}
}
for(int i = 0 ; i < siz ; ++i)
{
int v = adj[c][i].first ;
if(!vis[v])
{
eras(v,c,adj[c][i].second) ;
}
}
for(int i = 0 ; i < siz ; ++i)
{
if(!vis[adj[c][i].first]) decomp(adj[c][i].first,c) ;
}
}
int best_path(int a , int b , int H[][2] , int L[])
{
n = a , k = b ;
for(int i = 0 ; i < n - 1 ; ++i)
{
int ww = H[i][0] , vv = H[i][1] ;
adj[ww].push_back({vv,L[i]}) ;
adj[vv].push_back({ww,L[i]}) ;
}
for(int i = 0 ; i <= k ; ++i) dist[i] = mx ;
decomp(0,-1) ;
if(ans==mx) return -1 ;
else return ans ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
4984 KB |
Output is correct |
8 |
Correct |
9 ms |
4984 KB |
Output is correct |
9 |
Correct |
10 ms |
5100 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
9 ms |
5112 KB |
Output is correct |
14 |
Correct |
9 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
4984 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
4984 KB |
Output is correct |
8 |
Correct |
9 ms |
4984 KB |
Output is correct |
9 |
Correct |
10 ms |
5100 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
9 ms |
5112 KB |
Output is correct |
14 |
Correct |
9 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
4984 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
9 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
4984 KB |
Output is correct |
21 |
Correct |
9 ms |
5112 KB |
Output is correct |
22 |
Correct |
11 ms |
8696 KB |
Output is correct |
23 |
Correct |
11 ms |
8060 KB |
Output is correct |
24 |
Correct |
11 ms |
8440 KB |
Output is correct |
25 |
Correct |
12 ms |
8440 KB |
Output is correct |
26 |
Correct |
10 ms |
6392 KB |
Output is correct |
27 |
Correct |
10 ms |
8184 KB |
Output is correct |
28 |
Correct |
10 ms |
5884 KB |
Output is correct |
29 |
Correct |
10 ms |
6392 KB |
Output is correct |
30 |
Correct |
11 ms |
6520 KB |
Output is correct |
31 |
Correct |
12 ms |
7672 KB |
Output is correct |
32 |
Correct |
10 ms |
7824 KB |
Output is correct |
33 |
Correct |
11 ms |
8184 KB |
Output is correct |
34 |
Correct |
11 ms |
7416 KB |
Output is correct |
35 |
Correct |
12 ms |
8312 KB |
Output is correct |
36 |
Correct |
11 ms |
8696 KB |
Output is correct |
37 |
Correct |
12 ms |
8184 KB |
Output is correct |
38 |
Correct |
11 ms |
7160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
4984 KB |
Output is correct |
8 |
Correct |
9 ms |
4984 KB |
Output is correct |
9 |
Correct |
10 ms |
5100 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
9 ms |
5112 KB |
Output is correct |
14 |
Correct |
9 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
4984 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
345 ms |
10616 KB |
Output is correct |
20 |
Correct |
286 ms |
10744 KB |
Output is correct |
21 |
Correct |
228 ms |
10616 KB |
Output is correct |
22 |
Correct |
191 ms |
10744 KB |
Output is correct |
23 |
Correct |
196 ms |
10964 KB |
Output is correct |
24 |
Correct |
114 ms |
10744 KB |
Output is correct |
25 |
Correct |
240 ms |
14072 KB |
Output is correct |
26 |
Correct |
121 ms |
16376 KB |
Output is correct |
27 |
Correct |
326 ms |
16504 KB |
Output is correct |
28 |
Correct |
746 ms |
27768 KB |
Output is correct |
29 |
Correct |
775 ms |
30072 KB |
Output is correct |
30 |
Correct |
298 ms |
19456 KB |
Output is correct |
31 |
Correct |
318 ms |
19448 KB |
Output is correct |
32 |
Correct |
408 ms |
19560 KB |
Output is correct |
33 |
Correct |
458 ms |
18428 KB |
Output is correct |
34 |
Correct |
560 ms |
19320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
4984 KB |
Output is correct |
2 |
Correct |
8 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
4984 KB |
Output is correct |
4 |
Correct |
8 ms |
4984 KB |
Output is correct |
5 |
Correct |
8 ms |
4984 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
4984 KB |
Output is correct |
8 |
Correct |
9 ms |
4984 KB |
Output is correct |
9 |
Correct |
10 ms |
5100 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
8 ms |
4984 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
9 ms |
5112 KB |
Output is correct |
14 |
Correct |
9 ms |
5112 KB |
Output is correct |
15 |
Correct |
9 ms |
4984 KB |
Output is correct |
16 |
Correct |
9 ms |
5112 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
9 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
4984 KB |
Output is correct |
21 |
Correct |
9 ms |
5112 KB |
Output is correct |
22 |
Correct |
11 ms |
8696 KB |
Output is correct |
23 |
Correct |
11 ms |
8060 KB |
Output is correct |
24 |
Correct |
11 ms |
8440 KB |
Output is correct |
25 |
Correct |
12 ms |
8440 KB |
Output is correct |
26 |
Correct |
10 ms |
6392 KB |
Output is correct |
27 |
Correct |
10 ms |
8184 KB |
Output is correct |
28 |
Correct |
10 ms |
5884 KB |
Output is correct |
29 |
Correct |
10 ms |
6392 KB |
Output is correct |
30 |
Correct |
11 ms |
6520 KB |
Output is correct |
31 |
Correct |
12 ms |
7672 KB |
Output is correct |
32 |
Correct |
10 ms |
7824 KB |
Output is correct |
33 |
Correct |
11 ms |
8184 KB |
Output is correct |
34 |
Correct |
11 ms |
7416 KB |
Output is correct |
35 |
Correct |
12 ms |
8312 KB |
Output is correct |
36 |
Correct |
11 ms |
8696 KB |
Output is correct |
37 |
Correct |
12 ms |
8184 KB |
Output is correct |
38 |
Correct |
11 ms |
7160 KB |
Output is correct |
39 |
Correct |
345 ms |
10616 KB |
Output is correct |
40 |
Correct |
286 ms |
10744 KB |
Output is correct |
41 |
Correct |
228 ms |
10616 KB |
Output is correct |
42 |
Correct |
191 ms |
10744 KB |
Output is correct |
43 |
Correct |
196 ms |
10964 KB |
Output is correct |
44 |
Correct |
114 ms |
10744 KB |
Output is correct |
45 |
Correct |
240 ms |
14072 KB |
Output is correct |
46 |
Correct |
121 ms |
16376 KB |
Output is correct |
47 |
Correct |
326 ms |
16504 KB |
Output is correct |
48 |
Correct |
746 ms |
27768 KB |
Output is correct |
49 |
Correct |
775 ms |
30072 KB |
Output is correct |
50 |
Correct |
298 ms |
19456 KB |
Output is correct |
51 |
Correct |
318 ms |
19448 KB |
Output is correct |
52 |
Correct |
408 ms |
19560 KB |
Output is correct |
53 |
Correct |
458 ms |
18428 KB |
Output is correct |
54 |
Correct |
560 ms |
19320 KB |
Output is correct |
55 |
Correct |
19 ms |
5752 KB |
Output is correct |
56 |
Correct |
20 ms |
5752 KB |
Output is correct |
57 |
Correct |
107 ms |
12280 KB |
Output is correct |
58 |
Correct |
48 ms |
12008 KB |
Output is correct |
59 |
Correct |
133 ms |
18428 KB |
Output is correct |
60 |
Correct |
790 ms |
34040 KB |
Output is correct |
61 |
Correct |
328 ms |
19576 KB |
Output is correct |
62 |
Correct |
331 ms |
23416 KB |
Output is correct |
63 |
Correct |
402 ms |
23416 KB |
Output is correct |
64 |
Correct |
947 ms |
21880 KB |
Output is correct |
65 |
Correct |
669 ms |
20600 KB |
Output is correct |
66 |
Correct |
883 ms |
32056 KB |
Output is correct |
67 |
Correct |
175 ms |
24164 KB |
Output is correct |
68 |
Correct |
462 ms |
24184 KB |
Output is correct |
69 |
Correct |
465 ms |
24312 KB |
Output is correct |
70 |
Correct |
452 ms |
23544 KB |
Output is correct |