// #include "race.h"
#include<bits/stdc++.h>
#define fi first
#define se second
#define ull unsigned long long
#define ll long long
#define BIT(a , b) ((a >> b) & 1)
#define turnOn(a , b) ((a) | (1 << (b)))
#define turnOff(a , b) ((a) ^ (1 << (b)))
#define pii pair<ll , ll>
#define pb push_back
#define nl cout << "\n";
#define __ <<" "<<
#define mem(a, b) memset((a), (b), sizeof((a)))
#define all(c) (c).begin(), (c).end()
#define file "test"
using namespace std;
const int oo = 1e9 + 7;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int LOG = 20;
int n , child[N];
ll depth[N] , ans = oo;
ll k;
ll dist[N];
vector <pii> adj[N];
vector <int> vt[N];
map <ll , set<ll>> cnt;
void getsz(int i , int p){
child[i] = 1;
for(auto it : adj[i]){
int j = it.fi;
if(j == p) continue;
depth[j] = depth[i] + 1;
dist[j] = dist[i] + it.se;
getsz(j , i);
child[i] += child[j];
}
}
void sack(int i , int p , bool keep){
int best_node = -1;
int Max = -1;
for(auto it : adj[i]){
int j = it.fi;
if(j == p) continue;
if(child[j] > Max){
Max = child[j];
best_node = j;
}
}
for(auto it : adj[i]){
int j = it.fi;
if(j != p && j != best_node) sack(j , i , 0);
}
if(best_node != -1){
sack(best_node , i , 1);
swap(vt[i] , vt[best_node]);
}
vt[i].pb(i);
// cout << i << ' ' << cnt[dist[i] + k].size() << ' ' << dist[i] + k << ' ' << keep << '\n';
if(cnt[dist[i] + k].size()){
// cout << "ff\n";
ans = min(ans , *cnt[dist[i] + k].begin() - depth[i]);
}
cnt[dist[i]].insert(depth[i]);
// cout << i << ' ' << dist[i] << ":\n";
for(auto it : adj[i]){
int j = it.fi;
if(j == p || j == best_node) continue;
for(auto x : vt[j]){
ll tmp = k - (dist[x] - dist[i]) + dist[i];
// cout << dist[x] << ' ' << dist[i] << ' ' << tmp << '\n';
if(cnt[tmp].size()){
// cout << i << ' ' << x << ' ' << tmp << '\n';
ans = min(ans , depth[x] + *cnt[tmp].begin() - 2 * depth[i]);
}
}
for(auto x : vt[j]){
cnt[dist[x]].insert(depth[x]);
vt[i].pb(x);
}
}
// cout << i << ' ' << keep << ":\n";
// for(auto j : vt[i]) cout << j << ' ';
// nl;
if(!keep){
for(auto x : vt[i]){
cnt[dist[x]].erase(depth[x]);
// cout << x << '/' << cnt[dist[x]].size() << ' ';
}
// nl;
}
}
int best_path(int _N, int _K, int _H[][2], int _L[])
{
// if(_K == 1) return 0;
n = _N;
k = _K;
for(int i = 0 ; i < n - 1 ; i++){
int u , v , w;
u = _H[i][0];
v = _H[i][1];
w = _L[i];
u++;
v++;
adj[u].pb({v , w});
adj[v].pb({u , w});
}
getsz(1 , 0);
sack(1 , 0 , 0);
if(ans != oo) return ans;
else return -1;
}
// void run_with_file(){
// if(fopen(file".inp","r")){
// freopen(file".inp","r", stdin);
// freopen(file".out", "w", stdout);
// }
// }
// int main(){
// run_with_file();
// cin >> n >> k;
// for(int i = 0 ; i < n - 1 ; i++){
// int u , v , w;
// cin >> u >> v >> w;
// u++;
// v++;
// adj[u].pb({v , w});
// adj[v].pb({u , w});
// }
// getsz(1 , 0);
// sack(1 , 0 , 0);
// if(ans != oo) cout << ans;
// else cout << -1;
// }
/* UwU */
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16864 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
4 ms |
16856 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16864 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
4 ms |
16856 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16852 KB |
Output is correct |
19 |
Correct |
3 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
5 ms |
17240 KB |
Output is correct |
22 |
Correct |
5 ms |
17244 KB |
Output is correct |
23 |
Correct |
5 ms |
17244 KB |
Output is correct |
24 |
Correct |
5 ms |
17372 KB |
Output is correct |
25 |
Correct |
4 ms |
17240 KB |
Output is correct |
26 |
Correct |
5 ms |
17244 KB |
Output is correct |
27 |
Correct |
3 ms |
16732 KB |
Output is correct |
28 |
Correct |
4 ms |
17244 KB |
Output is correct |
29 |
Correct |
5 ms |
17376 KB |
Output is correct |
30 |
Correct |
4 ms |
17500 KB |
Output is correct |
31 |
Correct |
4 ms |
17376 KB |
Output is correct |
32 |
Correct |
4 ms |
17240 KB |
Output is correct |
33 |
Correct |
4 ms |
17244 KB |
Output is correct |
34 |
Correct |
4 ms |
17232 KB |
Output is correct |
35 |
Correct |
4 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
16988 KB |
Output is correct |
37 |
Correct |
5 ms |
16988 KB |
Output is correct |
38 |
Correct |
4 ms |
17244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16864 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
4 ms |
16856 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16852 KB |
Output is correct |
19 |
Correct |
153 ms |
29744 KB |
Output is correct |
20 |
Correct |
147 ms |
29388 KB |
Output is correct |
21 |
Correct |
153 ms |
29784 KB |
Output is correct |
22 |
Correct |
131 ms |
29536 KB |
Output is correct |
23 |
Correct |
224 ms |
30980 KB |
Output is correct |
24 |
Correct |
138 ms |
30156 KB |
Output is correct |
25 |
Correct |
97 ms |
40392 KB |
Output is correct |
26 |
Correct |
57 ms |
47456 KB |
Output is correct |
27 |
Correct |
189 ms |
40648 KB |
Output is correct |
28 |
Correct |
246 ms |
113832 KB |
Output is correct |
29 |
Correct |
249 ms |
112584 KB |
Output is correct |
30 |
Correct |
213 ms |
40532 KB |
Output is correct |
31 |
Correct |
192 ms |
40648 KB |
Output is correct |
32 |
Correct |
233 ms |
40976 KB |
Output is correct |
33 |
Correct |
202 ms |
36356 KB |
Output is correct |
34 |
Correct |
1173 ms |
130304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
3 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
3 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
3 ms |
16864 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
4 ms |
16856 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
3 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16732 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
3 ms |
16852 KB |
Output is correct |
19 |
Correct |
3 ms |
16732 KB |
Output is correct |
20 |
Correct |
3 ms |
16732 KB |
Output is correct |
21 |
Correct |
5 ms |
17240 KB |
Output is correct |
22 |
Correct |
5 ms |
17244 KB |
Output is correct |
23 |
Correct |
5 ms |
17244 KB |
Output is correct |
24 |
Correct |
5 ms |
17372 KB |
Output is correct |
25 |
Correct |
4 ms |
17240 KB |
Output is correct |
26 |
Correct |
5 ms |
17244 KB |
Output is correct |
27 |
Correct |
3 ms |
16732 KB |
Output is correct |
28 |
Correct |
4 ms |
17244 KB |
Output is correct |
29 |
Correct |
5 ms |
17376 KB |
Output is correct |
30 |
Correct |
4 ms |
17500 KB |
Output is correct |
31 |
Correct |
4 ms |
17376 KB |
Output is correct |
32 |
Correct |
4 ms |
17240 KB |
Output is correct |
33 |
Correct |
4 ms |
17244 KB |
Output is correct |
34 |
Correct |
4 ms |
17232 KB |
Output is correct |
35 |
Correct |
4 ms |
17244 KB |
Output is correct |
36 |
Correct |
4 ms |
16988 KB |
Output is correct |
37 |
Correct |
5 ms |
16988 KB |
Output is correct |
38 |
Correct |
4 ms |
17244 KB |
Output is correct |
39 |
Correct |
153 ms |
29744 KB |
Output is correct |
40 |
Correct |
147 ms |
29388 KB |
Output is correct |
41 |
Correct |
153 ms |
29784 KB |
Output is correct |
42 |
Correct |
131 ms |
29536 KB |
Output is correct |
43 |
Correct |
224 ms |
30980 KB |
Output is correct |
44 |
Correct |
138 ms |
30156 KB |
Output is correct |
45 |
Correct |
97 ms |
40392 KB |
Output is correct |
46 |
Correct |
57 ms |
47456 KB |
Output is correct |
47 |
Correct |
189 ms |
40648 KB |
Output is correct |
48 |
Correct |
246 ms |
113832 KB |
Output is correct |
49 |
Correct |
249 ms |
112584 KB |
Output is correct |
50 |
Correct |
213 ms |
40532 KB |
Output is correct |
51 |
Correct |
192 ms |
40648 KB |
Output is correct |
52 |
Correct |
233 ms |
40976 KB |
Output is correct |
53 |
Correct |
202 ms |
36356 KB |
Output is correct |
54 |
Correct |
1173 ms |
130304 KB |
Output is correct |
55 |
Correct |
22 ms |
19540 KB |
Output is correct |
56 |
Correct |
12 ms |
17908 KB |
Output is correct |
57 |
Correct |
108 ms |
30656 KB |
Output is correct |
58 |
Correct |
54 ms |
28848 KB |
Output is correct |
59 |
Correct |
102 ms |
65992 KB |
Output is correct |
60 |
Correct |
267 ms |
112012 KB |
Output is correct |
61 |
Correct |
285 ms |
48952 KB |
Output is correct |
62 |
Correct |
193 ms |
40928 KB |
Output is correct |
63 |
Correct |
243 ms |
40920 KB |
Output is correct |
64 |
Correct |
1501 ms |
91048 KB |
Output is correct |
65 |
Correct |
1996 ms |
159640 KB |
Output is correct |
66 |
Correct |
296 ms |
110136 KB |
Output is correct |
67 |
Correct |
167 ms |
40232 KB |
Output is correct |
68 |
Correct |
968 ms |
103752 KB |
Output is correct |
69 |
Correct |
933 ms |
104484 KB |
Output is correct |
70 |
Correct |
844 ms |
100288 KB |
Output is correct |