#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
//#define int long long
const int N = 1e6+1, inf = 1e9+1;
vector <vector<pair<int,int>>> g;
map <int,int> cnt;
int sub[N], used[N], K, res;
void get_size(int x, int par){
sub[x] = 1;
for ( auto [to, w]: g[x] ){
if ( used[to] or to == par ) continue;
get_size(to, x);
sub[x] += sub[to];
}
}
int get_ctr(int x, int par, int desired){
for ( auto [to, w]: g[x] ){
if ( used[to] or to == par ) continue;
if ( desired <= sub[to] ) return get_ctr(to, x, desired);
}
return x;
}
void dfs(int x, int par, int depth, int len, bool add){
if ( len > K ) return;
if ( !add ){
if ( len == K ) res = min(res, depth);
else res = min(res, !cnt.count(K-len) ? inf : cnt[K-len]+depth);
}
else{
if ( !cnt.count(len) ) cnt[len] = depth;
else cnt[len] = min(cnt[len], depth);
}
for ( auto [to, w]: g[x] ){
if ( used[to] or to == par ) continue;
dfs(to, x, depth+1, len+w, add);
}
}
void ctr_dec(int x){
get_size(x, -1);
int cur = get_ctr(x, -1, sub[x]/2);
cnt.clear();
used[cur] = true;
for ( auto [to, w]: g[cur] ){
if ( used[to] ) continue;
dfs(to, -1, 1, w, false);
dfs(to, -1, 1, w, true);
}
for ( auto [to, w]: g[cur] ){
if ( used[to] ) continue;
ctr_dec(to);
}
}
int best_path(int n, int k, int h[][2], int L[]){
g.resize(n);
for ( int i = 0; i+1 < n; i++ ){
g[h[i][0]].pb({h[i][1], L[i]});
g[h[i][1]].pb({h[i][0], L[i]});
}
K = k; res = inf;
ctr_dec(0);
if ( res == inf ) res = -1;
return res;
}
Compilation message
race.cpp: In function 'void IO(std::string)':
race.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
184 ms |
8308 KB |
Output is correct |
20 |
Correct |
206 ms |
8416 KB |
Output is correct |
21 |
Correct |
191 ms |
8376 KB |
Output is correct |
22 |
Correct |
192 ms |
8420 KB |
Output is correct |
23 |
Correct |
72 ms |
8596 KB |
Output is correct |
24 |
Correct |
92 ms |
8552 KB |
Output is correct |
25 |
Correct |
126 ms |
12004 KB |
Output is correct |
26 |
Correct |
98 ms |
15692 KB |
Output is correct |
27 |
Correct |
189 ms |
16712 KB |
Output is correct |
28 |
Correct |
263 ms |
30908 KB |
Output is correct |
29 |
Correct |
326 ms |
29752 KB |
Output is correct |
30 |
Correct |
192 ms |
16648 KB |
Output is correct |
31 |
Correct |
182 ms |
16588 KB |
Output is correct |
32 |
Correct |
232 ms |
16544 KB |
Output is correct |
33 |
Correct |
282 ms |
15420 KB |
Output is correct |
34 |
Correct |
190 ms |
15388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
184 ms |
8308 KB |
Output is correct |
40 |
Correct |
206 ms |
8416 KB |
Output is correct |
41 |
Correct |
191 ms |
8376 KB |
Output is correct |
42 |
Correct |
192 ms |
8420 KB |
Output is correct |
43 |
Correct |
72 ms |
8596 KB |
Output is correct |
44 |
Correct |
92 ms |
8552 KB |
Output is correct |
45 |
Correct |
126 ms |
12004 KB |
Output is correct |
46 |
Correct |
98 ms |
15692 KB |
Output is correct |
47 |
Correct |
189 ms |
16712 KB |
Output is correct |
48 |
Correct |
263 ms |
30908 KB |
Output is correct |
49 |
Correct |
326 ms |
29752 KB |
Output is correct |
50 |
Correct |
192 ms |
16648 KB |
Output is correct |
51 |
Correct |
182 ms |
16588 KB |
Output is correct |
52 |
Correct |
232 ms |
16544 KB |
Output is correct |
53 |
Correct |
282 ms |
15420 KB |
Output is correct |
54 |
Correct |
190 ms |
15388 KB |
Output is correct |
55 |
Correct |
17 ms |
1364 KB |
Output is correct |
56 |
Correct |
12 ms |
1236 KB |
Output is correct |
57 |
Correct |
113 ms |
8664 KB |
Output is correct |
58 |
Correct |
40 ms |
8612 KB |
Output is correct |
59 |
Correct |
268 ms |
20368 KB |
Output is correct |
60 |
Correct |
908 ms |
41592 KB |
Output is correct |
61 |
Correct |
254 ms |
19720 KB |
Output is correct |
62 |
Correct |
303 ms |
19752 KB |
Output is correct |
63 |
Correct |
369 ms |
19532 KB |
Output is correct |
64 |
Correct |
1082 ms |
24404 KB |
Output is correct |
65 |
Correct |
211 ms |
20176 KB |
Output is correct |
66 |
Correct |
544 ms |
30408 KB |
Output is correct |
67 |
Correct |
135 ms |
20148 KB |
Output is correct |
68 |
Correct |
383 ms |
23712 KB |
Output is correct |
69 |
Correct |
455 ms |
23804 KB |
Output is correct |
70 |
Correct |
375 ms |
22768 KB |
Output is correct |