#include <bits/stdc++.h>
using namespace std;
template< class T, class T2 > inline bool chkmin( T& a, const T2& b ){ return a > b ? a = b, 1 : 0 ; }
#ifndef LOCAL
#define cerr if( false )cerr
#endif
#define out(x) #x << " = " << x << " "
typedef long long ll;
const int MAX_N = 1e6 + 42;
const ll mod = 1e9 + 7;
int n, k;
std::vector< std::pair< int , int >> v[MAX_N];
int l[MAX_N];
bool viz[MAX_N];
int sz[MAX_N];
void dfs( int x, int par = -1 ){
cerr << out( x ) << out( par ) << endl;
sz[x] = 1;
for( auto J : v[x] ){
int j = J.first;
if( j == par || viz[j] ) continue;
dfs( j, x );
sz[x] += sz[j];
}
}
int centroid( int x, int SZ, int par = -1 ){
cerr << out( x ) << out( SZ ) << endl;
for( auto J : v[x] ){
int j = J.first;
if( j == par || viz[j] ) continue;
if( sz[j] > ( SZ / 2 ) ) return centroid( j, SZ, x );
}
return x;
}
std::unordered_map< int , std::pair< int, int > >m;
ll nas = mod;
int tt = 0;
void findNas( int x, bool type, int len, int par = -1, int d = 1 ){
auto pos = m.find( k - len );
//if( pos != m.end() and ( pos->second.second < tt ) ) m.erase( pos );
if( type ){
if( pos != m.end() and ( pos->second.second >= tt ) ){
chkmin( nas, d + pos->second.first );
}
}else{
auto pos2 = m.find( len );
if( pos2 == m.end() || pos2->second.second < tt ) m[len] = { d, tt };
if( pos2 != m.end() and pos2->second.second == tt and pos2->second.first > d ){
m[len] = { d, tt };
}
}
for( auto J : v[x] ){
int j = J.first;
if( j == par || viz[j] ) continue;
findNas( j, type, len + J.second, x, d + 1 );
}
}
void centDecomp( int x, int par = -1 ){
cerr << out( x ) << endl;
dfs( x );
int c = centroid( x, sz[x] );
cerr << out( c ) << endl;
viz[c] = true;
for( auto J : v[c] ){
int j = J.first;
if( viz[j] ) continue;
findNas( j, true , J.second);
findNas( j, false, J.second );
}
tt ++;
for( auto J : v[c] ){
int j = J.first;
if( viz[j] ) continue;
centDecomp( j, c );
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
for( int i=0 ; i < n-1 ; i ++ ){
int a = H[i][0];
int b = H[i][1];
v[a].push_back({ b, L[i] });
v[b].push_back({ a, L[i] });
}
m[0] = { 0, mod };
centDecomp( 0 );
if( nas == mod ) nas = -1;
return nas;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27996 KB |
Output is correct |
2 |
Correct |
7 ms |
27996 KB |
Output is correct |
3 |
Correct |
6 ms |
27996 KB |
Output is correct |
4 |
Correct |
7 ms |
27996 KB |
Output is correct |
5 |
Correct |
6 ms |
27996 KB |
Output is correct |
6 |
Correct |
6 ms |
27996 KB |
Output is correct |
7 |
Correct |
6 ms |
27996 KB |
Output is correct |
8 |
Correct |
6 ms |
27996 KB |
Output is correct |
9 |
Correct |
6 ms |
27996 KB |
Output is correct |
10 |
Correct |
6 ms |
27992 KB |
Output is correct |
11 |
Correct |
6 ms |
28248 KB |
Output is correct |
12 |
Correct |
6 ms |
28252 KB |
Output is correct |
13 |
Correct |
6 ms |
28248 KB |
Output is correct |
14 |
Correct |
6 ms |
28092 KB |
Output is correct |
15 |
Correct |
6 ms |
27996 KB |
Output is correct |
16 |
Correct |
6 ms |
27996 KB |
Output is correct |
17 |
Correct |
6 ms |
27996 KB |
Output is correct |
18 |
Correct |
6 ms |
27996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27996 KB |
Output is correct |
2 |
Correct |
7 ms |
27996 KB |
Output is correct |
3 |
Correct |
6 ms |
27996 KB |
Output is correct |
4 |
Correct |
7 ms |
27996 KB |
Output is correct |
5 |
Correct |
6 ms |
27996 KB |
Output is correct |
6 |
Correct |
6 ms |
27996 KB |
Output is correct |
7 |
Correct |
6 ms |
27996 KB |
Output is correct |
8 |
Correct |
6 ms |
27996 KB |
Output is correct |
9 |
Correct |
6 ms |
27996 KB |
Output is correct |
10 |
Correct |
6 ms |
27992 KB |
Output is correct |
11 |
Correct |
6 ms |
28248 KB |
Output is correct |
12 |
Correct |
6 ms |
28252 KB |
Output is correct |
13 |
Correct |
6 ms |
28248 KB |
Output is correct |
14 |
Correct |
6 ms |
28092 KB |
Output is correct |
15 |
Correct |
6 ms |
27996 KB |
Output is correct |
16 |
Correct |
6 ms |
27996 KB |
Output is correct |
17 |
Correct |
6 ms |
27996 KB |
Output is correct |
18 |
Correct |
6 ms |
27996 KB |
Output is correct |
19 |
Correct |
6 ms |
28248 KB |
Output is correct |
20 |
Correct |
6 ms |
27996 KB |
Output is correct |
21 |
Correct |
7 ms |
28116 KB |
Output is correct |
22 |
Correct |
8 ms |
28252 KB |
Output is correct |
23 |
Correct |
7 ms |
28248 KB |
Output is correct |
24 |
Correct |
8 ms |
28252 KB |
Output is correct |
25 |
Correct |
7 ms |
28388 KB |
Output is correct |
26 |
Correct |
7 ms |
28252 KB |
Output is correct |
27 |
Correct |
7 ms |
28224 KB |
Output is correct |
28 |
Correct |
7 ms |
28252 KB |
Output is correct |
29 |
Correct |
7 ms |
28252 KB |
Output is correct |
30 |
Correct |
7 ms |
28252 KB |
Output is correct |
31 |
Correct |
7 ms |
28252 KB |
Output is correct |
32 |
Correct |
7 ms |
28252 KB |
Output is correct |
33 |
Correct |
8 ms |
28252 KB |
Output is correct |
34 |
Correct |
7 ms |
28508 KB |
Output is correct |
35 |
Correct |
8 ms |
28344 KB |
Output is correct |
36 |
Correct |
7 ms |
28508 KB |
Output is correct |
37 |
Correct |
7 ms |
28252 KB |
Output is correct |
38 |
Correct |
7 ms |
28252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27996 KB |
Output is correct |
2 |
Correct |
7 ms |
27996 KB |
Output is correct |
3 |
Correct |
6 ms |
27996 KB |
Output is correct |
4 |
Correct |
7 ms |
27996 KB |
Output is correct |
5 |
Correct |
6 ms |
27996 KB |
Output is correct |
6 |
Correct |
6 ms |
27996 KB |
Output is correct |
7 |
Correct |
6 ms |
27996 KB |
Output is correct |
8 |
Correct |
6 ms |
27996 KB |
Output is correct |
9 |
Correct |
6 ms |
27996 KB |
Output is correct |
10 |
Correct |
6 ms |
27992 KB |
Output is correct |
11 |
Correct |
6 ms |
28248 KB |
Output is correct |
12 |
Correct |
6 ms |
28252 KB |
Output is correct |
13 |
Correct |
6 ms |
28248 KB |
Output is correct |
14 |
Correct |
6 ms |
28092 KB |
Output is correct |
15 |
Correct |
6 ms |
27996 KB |
Output is correct |
16 |
Correct |
6 ms |
27996 KB |
Output is correct |
17 |
Correct |
6 ms |
27996 KB |
Output is correct |
18 |
Correct |
6 ms |
27996 KB |
Output is correct |
19 |
Correct |
129 ms |
34148 KB |
Output is correct |
20 |
Correct |
99 ms |
34132 KB |
Output is correct |
21 |
Correct |
98 ms |
34132 KB |
Output is correct |
22 |
Correct |
84 ms |
34108 KB |
Output is correct |
23 |
Correct |
117 ms |
34460 KB |
Output is correct |
24 |
Correct |
67 ms |
34136 KB |
Output is correct |
25 |
Correct |
116 ms |
37772 KB |
Output is correct |
26 |
Correct |
75 ms |
41300 KB |
Output is correct |
27 |
Correct |
152 ms |
40272 KB |
Output is correct |
28 |
Correct |
788 ms |
93648 KB |
Output is correct |
29 |
Correct |
684 ms |
83612 KB |
Output is correct |
30 |
Correct |
138 ms |
43208 KB |
Output is correct |
31 |
Correct |
140 ms |
43088 KB |
Output is correct |
32 |
Correct |
162 ms |
43344 KB |
Output is correct |
33 |
Correct |
192 ms |
41988 KB |
Output is correct |
34 |
Correct |
1702 ms |
144008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27996 KB |
Output is correct |
2 |
Correct |
7 ms |
27996 KB |
Output is correct |
3 |
Correct |
6 ms |
27996 KB |
Output is correct |
4 |
Correct |
7 ms |
27996 KB |
Output is correct |
5 |
Correct |
6 ms |
27996 KB |
Output is correct |
6 |
Correct |
6 ms |
27996 KB |
Output is correct |
7 |
Correct |
6 ms |
27996 KB |
Output is correct |
8 |
Correct |
6 ms |
27996 KB |
Output is correct |
9 |
Correct |
6 ms |
27996 KB |
Output is correct |
10 |
Correct |
6 ms |
27992 KB |
Output is correct |
11 |
Correct |
6 ms |
28248 KB |
Output is correct |
12 |
Correct |
6 ms |
28252 KB |
Output is correct |
13 |
Correct |
6 ms |
28248 KB |
Output is correct |
14 |
Correct |
6 ms |
28092 KB |
Output is correct |
15 |
Correct |
6 ms |
27996 KB |
Output is correct |
16 |
Correct |
6 ms |
27996 KB |
Output is correct |
17 |
Correct |
6 ms |
27996 KB |
Output is correct |
18 |
Correct |
6 ms |
27996 KB |
Output is correct |
19 |
Correct |
6 ms |
28248 KB |
Output is correct |
20 |
Correct |
6 ms |
27996 KB |
Output is correct |
21 |
Correct |
7 ms |
28116 KB |
Output is correct |
22 |
Correct |
8 ms |
28252 KB |
Output is correct |
23 |
Correct |
7 ms |
28248 KB |
Output is correct |
24 |
Correct |
8 ms |
28252 KB |
Output is correct |
25 |
Correct |
7 ms |
28388 KB |
Output is correct |
26 |
Correct |
7 ms |
28252 KB |
Output is correct |
27 |
Correct |
7 ms |
28224 KB |
Output is correct |
28 |
Correct |
7 ms |
28252 KB |
Output is correct |
29 |
Correct |
7 ms |
28252 KB |
Output is correct |
30 |
Correct |
7 ms |
28252 KB |
Output is correct |
31 |
Correct |
7 ms |
28252 KB |
Output is correct |
32 |
Correct |
7 ms |
28252 KB |
Output is correct |
33 |
Correct |
8 ms |
28252 KB |
Output is correct |
34 |
Correct |
7 ms |
28508 KB |
Output is correct |
35 |
Correct |
8 ms |
28344 KB |
Output is correct |
36 |
Correct |
7 ms |
28508 KB |
Output is correct |
37 |
Correct |
7 ms |
28252 KB |
Output is correct |
38 |
Correct |
7 ms |
28252 KB |
Output is correct |
39 |
Correct |
129 ms |
34148 KB |
Output is correct |
40 |
Correct |
99 ms |
34132 KB |
Output is correct |
41 |
Correct |
98 ms |
34132 KB |
Output is correct |
42 |
Correct |
84 ms |
34108 KB |
Output is correct |
43 |
Correct |
117 ms |
34460 KB |
Output is correct |
44 |
Correct |
67 ms |
34136 KB |
Output is correct |
45 |
Correct |
116 ms |
37772 KB |
Output is correct |
46 |
Correct |
75 ms |
41300 KB |
Output is correct |
47 |
Correct |
152 ms |
40272 KB |
Output is correct |
48 |
Correct |
788 ms |
93648 KB |
Output is correct |
49 |
Correct |
684 ms |
83612 KB |
Output is correct |
50 |
Correct |
138 ms |
43208 KB |
Output is correct |
51 |
Correct |
140 ms |
43088 KB |
Output is correct |
52 |
Correct |
162 ms |
43344 KB |
Output is correct |
53 |
Correct |
192 ms |
41988 KB |
Output is correct |
54 |
Correct |
1702 ms |
144008 KB |
Output is correct |
55 |
Correct |
14 ms |
29016 KB |
Output is correct |
56 |
Correct |
13 ms |
28508 KB |
Output is correct |
57 |
Correct |
68 ms |
35736 KB |
Output is correct |
58 |
Correct |
33 ms |
35456 KB |
Output is correct |
59 |
Correct |
106 ms |
47204 KB |
Output is correct |
60 |
Correct |
449 ms |
69712 KB |
Output is correct |
61 |
Correct |
147 ms |
44464 KB |
Output is correct |
62 |
Correct |
133 ms |
43320 KB |
Output is correct |
63 |
Correct |
177 ms |
43360 KB |
Output is correct |
64 |
Correct |
439 ms |
52932 KB |
Output is correct |
65 |
Correct |
1274 ms |
123388 KB |
Output is correct |
66 |
Correct |
944 ms |
93480 KB |
Output is correct |
67 |
Correct |
79 ms |
43656 KB |
Output is correct |
68 |
Correct |
285 ms |
56380 KB |
Output is correct |
69 |
Correct |
315 ms |
56692 KB |
Output is correct |
70 |
Correct |
257 ms |
55604 KB |
Output is correct |