#include<bits/stdc++.h>
using namespace std ;
#define maxn 200009
#define ll long long
#define fi first
#define se second
#define pb push_back
#define left id<<1
#define right id<<1|1
#define re exit(0);
const int mod = 1e9+7;
const int INF = 1e9;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<ll> vl;
void add ( int &a , int b )
{
a += b ;
if ( a > mod ) a -= mod ;
if ( a < 0 ) a += mod ;
}
template <typename T> void chkmin ( T &a , T b ) { if ( a > b ) a = b ; }
template <typename T> void chkmax ( T &a , T b ) { if ( a < b ) a = b ; }
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow (a,n/2) ;
if ( n % 2 ) return 1ll*res*res%mod*a%mod ;
else return 1ll*res*res%mod ;
}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
}
const int maxm = 1e6+9 ;
int n , k ;
int h [maxn][2] , l [maxn] ;
int res = INF ;
vii adjList [maxn] ;
int sz [maxn] , cnt [maxm] ;
bool vis [maxn] ;
int dfs_sz ( int u , int par )
{
sz [u] = 1 ;
for ( auto x : adjList [u] )
{
int v = x.fi ;
if ( v == par || vis [v] ) continue ;
sz [u] += dfs_sz (v,u) ;
}
return sz [u] ;
}
int find_centroid ( int u , int par , int n )
{
for ( auto x : adjList [u] )
{
int v = x.fi ;
if ( v == par || vis [v] ) continue ;
if ( sz [v]*2 > n ) return find_centroid (v,u,n) ;
}
return u ;
}
void dfs1 ( int u , int par , int dep , int len )
{
if ( len > k ) return ;
chkmin (res,dep+cnt[k-len]) ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( v == par || vis [v] ) continue ;
dfs1 (v,u,dep+1,len+w) ;
}
}
void dfs2 ( int u , int par , int dep , int len )
{
if ( len > k ) return ;
chkmin (cnt[len],dep) ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( v == par || vis [v] ) continue ;
dfs2 (v,u,dep+1,len+w) ;
}
}
void dfs3 ( int u , int par , int dep , int len )
{
if ( len > k ) return ;
cnt [len] = INF ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( v == par || vis [v] ) continue ;
dfs3 (v,u,dep+1,len+w) ;
}
}
void solve ( int u )
{
int n = dfs_sz (u,-1) ;
u = find_centroid (u,-1,n) ;
cnt [0] = 0 ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( vis [v] ) continue ;
dfs1 (v,u,1,w) ;
dfs2 (v,u,1,w) ;
}
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( vis [v] ) continue ;
dfs3 (v,u,1,w) ;
}
vis [u] = true ;
for ( auto x : adjList [u] )
{
int v = x.fi , w = x.se ;
if ( vis [v] ) continue ;
solve (v) ;
}
}
int best_path ( int N , int K , int H[][2] , int L[] )
{
n = N , k = K ;
for ( int i = 0 ; i < n-1 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) h [i][j] = H [i][j] ;
for ( int i = 0 ; i < n-1 ; i ++ ) l [i] = L [i] ;
for ( int i = 0 ; i < n-1 ; i ++ )
{
int u = h [i][0] , v = h [i][1] ;
adjList [u] . pb ({v,l[i]}) ;
adjList [v] . pb ({u,l[i]}) ;
}
memset (cnt,0x3f,sizeof cnt) ;
solve (0) ;
if ( res == INF ) res = - 1 ;
return res ;
}
//int main ()
//{
// ios_base::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// rf () ;
// cin >> n >> k ;
// for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) cin >> h [i][j] ;
// for ( int i = 1 ; i < n ; i ++ ) cin >> l [i] ;
// cout << best_path (n,k,h,l) ;
//}
Compilation message
race.cpp: In function 'void solve(int)':
race.cpp:130:18: warning: unused variable 'w' [-Wunused-variable]
130 | int v = x.fi , w = x.se ;
| ^
race.cpp: In function 'void rf()':
race.cpp:40:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
4 ms |
14680 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14680 KB |
Output is correct |
10 |
Correct |
3 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14680 KB |
Output is correct |
13 |
Correct |
3 ms |
14692 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
4 ms |
14680 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14680 KB |
Output is correct |
10 |
Correct |
3 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14680 KB |
Output is correct |
13 |
Correct |
3 ms |
14692 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14680 KB |
Output is correct |
22 |
Correct |
3 ms |
14680 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
3 ms |
14680 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
14680 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
4 ms |
14680 KB |
Output is correct |
29 |
Correct |
3 ms |
14684 KB |
Output is correct |
30 |
Correct |
3 ms |
14684 KB |
Output is correct |
31 |
Correct |
4 ms |
14680 KB |
Output is correct |
32 |
Correct |
4 ms |
14680 KB |
Output is correct |
33 |
Correct |
3 ms |
14680 KB |
Output is correct |
34 |
Correct |
3 ms |
14680 KB |
Output is correct |
35 |
Correct |
3 ms |
14680 KB |
Output is correct |
36 |
Correct |
3 ms |
14680 KB |
Output is correct |
37 |
Correct |
3 ms |
14680 KB |
Output is correct |
38 |
Correct |
4 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
4 ms |
14680 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14680 KB |
Output is correct |
10 |
Correct |
3 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14680 KB |
Output is correct |
13 |
Correct |
3 ms |
14692 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
86 ms |
22352 KB |
Output is correct |
20 |
Correct |
84 ms |
22456 KB |
Output is correct |
21 |
Correct |
90 ms |
22352 KB |
Output is correct |
22 |
Correct |
82 ms |
22352 KB |
Output is correct |
23 |
Correct |
64 ms |
22868 KB |
Output is correct |
24 |
Correct |
39 ms |
22576 KB |
Output is correct |
25 |
Correct |
84 ms |
25168 KB |
Output is correct |
26 |
Correct |
78 ms |
27984 KB |
Output is correct |
27 |
Correct |
108 ms |
28520 KB |
Output is correct |
28 |
Correct |
157 ms |
40016 KB |
Output is correct |
29 |
Correct |
171 ms |
39248 KB |
Output is correct |
30 |
Correct |
107 ms |
28496 KB |
Output is correct |
31 |
Correct |
109 ms |
28496 KB |
Output is correct |
32 |
Correct |
118 ms |
28628 KB |
Output is correct |
33 |
Correct |
128 ms |
27472 KB |
Output is correct |
34 |
Correct |
113 ms |
28496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14680 KB |
Output is correct |
2 |
Correct |
3 ms |
14680 KB |
Output is correct |
3 |
Correct |
4 ms |
14680 KB |
Output is correct |
4 |
Correct |
3 ms |
14684 KB |
Output is correct |
5 |
Correct |
3 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
3 ms |
14680 KB |
Output is correct |
9 |
Correct |
3 ms |
14680 KB |
Output is correct |
10 |
Correct |
3 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14680 KB |
Output is correct |
12 |
Correct |
3 ms |
14680 KB |
Output is correct |
13 |
Correct |
3 ms |
14692 KB |
Output is correct |
14 |
Correct |
3 ms |
14680 KB |
Output is correct |
15 |
Correct |
3 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14680 KB |
Output is correct |
22 |
Correct |
3 ms |
14680 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
3 ms |
14680 KB |
Output is correct |
25 |
Correct |
3 ms |
14684 KB |
Output is correct |
26 |
Correct |
3 ms |
14680 KB |
Output is correct |
27 |
Correct |
3 ms |
14680 KB |
Output is correct |
28 |
Correct |
4 ms |
14680 KB |
Output is correct |
29 |
Correct |
3 ms |
14684 KB |
Output is correct |
30 |
Correct |
3 ms |
14684 KB |
Output is correct |
31 |
Correct |
4 ms |
14680 KB |
Output is correct |
32 |
Correct |
4 ms |
14680 KB |
Output is correct |
33 |
Correct |
3 ms |
14680 KB |
Output is correct |
34 |
Correct |
3 ms |
14680 KB |
Output is correct |
35 |
Correct |
3 ms |
14680 KB |
Output is correct |
36 |
Correct |
3 ms |
14680 KB |
Output is correct |
37 |
Correct |
3 ms |
14680 KB |
Output is correct |
38 |
Correct |
4 ms |
15192 KB |
Output is correct |
39 |
Correct |
86 ms |
22352 KB |
Output is correct |
40 |
Correct |
84 ms |
22456 KB |
Output is correct |
41 |
Correct |
90 ms |
22352 KB |
Output is correct |
42 |
Correct |
82 ms |
22352 KB |
Output is correct |
43 |
Correct |
64 ms |
22868 KB |
Output is correct |
44 |
Correct |
39 ms |
22576 KB |
Output is correct |
45 |
Correct |
84 ms |
25168 KB |
Output is correct |
46 |
Correct |
78 ms |
27984 KB |
Output is correct |
47 |
Correct |
108 ms |
28520 KB |
Output is correct |
48 |
Correct |
157 ms |
40016 KB |
Output is correct |
49 |
Correct |
171 ms |
39248 KB |
Output is correct |
50 |
Correct |
107 ms |
28496 KB |
Output is correct |
51 |
Correct |
109 ms |
28496 KB |
Output is correct |
52 |
Correct |
118 ms |
28628 KB |
Output is correct |
53 |
Correct |
128 ms |
27472 KB |
Output is correct |
54 |
Correct |
113 ms |
28496 KB |
Output is correct |
55 |
Correct |
9 ms |
15192 KB |
Output is correct |
56 |
Correct |
10 ms |
15192 KB |
Output is correct |
57 |
Correct |
62 ms |
22872 KB |
Output is correct |
58 |
Correct |
28 ms |
22320 KB |
Output is correct |
59 |
Correct |
76 ms |
27984 KB |
Output is correct |
60 |
Correct |
320 ms |
39292 KB |
Output is correct |
61 |
Correct |
127 ms |
28772 KB |
Output is correct |
62 |
Correct |
132 ms |
28752 KB |
Output is correct |
63 |
Correct |
158 ms |
28784 KB |
Output is correct |
64 |
Correct |
287 ms |
29004 KB |
Output is correct |
65 |
Correct |
118 ms |
29520 KB |
Output is correct |
66 |
Correct |
248 ms |
37024 KB |
Output is correct |
67 |
Correct |
75 ms |
29252 KB |
Output is correct |
68 |
Correct |
167 ms |
29524 KB |
Output is correct |
69 |
Correct |
169 ms |
29520 KB |
Output is correct |
70 |
Correct |
138 ms |
28904 KB |
Output is correct |