//#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , sz[N] , us[N];
vector<pair<int,int>>v[N];
int dfs_sz(int n , int par = 0){
sz[n] = 1;
for(auto to : v[n])
if(!us[to.fi] && to.fi != par)
sz[n] += dfs_sz(to.fi , n);
return sz[n];
}
ll ans = 1e9 , k;
int get_centroid(int n , int par , int s){
for(auto to : v[n])
if(!us[to.fi] && to.fi != par && sz[to.fi] > s / 2)
return get_centroid(to.fi , n , s);
return n;
}
set<pair<ll,ll>>st,st1;
void dfs(int n , int par, ll dis , ll cnt){
st1.insert({dis , cnt});
if(dis == k)
ans = min(ans , cnt);
auto it = st.lower_bound({k - dis , 0});
if(it->fi == k - dis)
ans = min(ans , cnt + it->se);
for(auto to : v[n])
if(!us[to.fi] && to.fi != par)
dfs(to.fi , n , dis + to.se , cnt + 1);
}
void centroid(int n){
int x = get_centroid(n , 0 , dfs_sz(n));
us[x] = 1;
st.clear();
for(auto to : v[x]){
if(!us[to.fi]){
st1.clear();
dfs(to.fi , x , to.se , 1);
for(auto it : st1)
st.insert(it);
}
}
for(auto to : v[x])
if(!us[to.fi])
centroid(to.fi);
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
for(int i = 0; i < N - 1; i++) {
int x = H[i][0] + 1,y = H[i][1] + 1;
v[x].pb({y, L[i]});
v[y].pb({x, L[i]});
}
centroid(1);
if(ans > N)
ans = -1;
return ans;
}
// Author : حسن
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
2 ms |
18780 KB |
Output is correct |
18 |
Correct |
2 ms |
18776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
2 ms |
18780 KB |
Output is correct |
18 |
Correct |
2 ms |
18776 KB |
Output is correct |
19 |
Correct |
2 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
18888 KB |
Output is correct |
22 |
Correct |
5 ms |
18780 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
3 ms |
19036 KB |
Output is correct |
26 |
Correct |
3 ms |
19032 KB |
Output is correct |
27 |
Correct |
3 ms |
18780 KB |
Output is correct |
28 |
Correct |
4 ms |
18780 KB |
Output is correct |
29 |
Correct |
3 ms |
18912 KB |
Output is correct |
30 |
Correct |
4 ms |
19032 KB |
Output is correct |
31 |
Correct |
4 ms |
18780 KB |
Output is correct |
32 |
Correct |
4 ms |
18776 KB |
Output is correct |
33 |
Correct |
4 ms |
19036 KB |
Output is correct |
34 |
Correct |
3 ms |
19036 KB |
Output is correct |
35 |
Correct |
4 ms |
19036 KB |
Output is correct |
36 |
Correct |
4 ms |
18780 KB |
Output is correct |
37 |
Correct |
4 ms |
18780 KB |
Output is correct |
38 |
Correct |
4 ms |
18892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
2 ms |
18780 KB |
Output is correct |
18 |
Correct |
2 ms |
18776 KB |
Output is correct |
19 |
Correct |
179 ms |
24912 KB |
Output is correct |
20 |
Correct |
175 ms |
26132 KB |
Output is correct |
21 |
Correct |
166 ms |
25680 KB |
Output is correct |
22 |
Correct |
149 ms |
26192 KB |
Output is correct |
23 |
Correct |
233 ms |
26992 KB |
Output is correct |
24 |
Correct |
93 ms |
26352 KB |
Output is correct |
25 |
Incorrect |
343 ms |
39508 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18780 KB |
Output is correct |
2 |
Correct |
3 ms |
18780 KB |
Output is correct |
3 |
Correct |
2 ms |
18780 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
2 ms |
18780 KB |
Output is correct |
6 |
Correct |
2 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
3 ms |
18780 KB |
Output is correct |
9 |
Correct |
2 ms |
18780 KB |
Output is correct |
10 |
Correct |
2 ms |
18780 KB |
Output is correct |
11 |
Correct |
2 ms |
18780 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
13 |
Correct |
2 ms |
18780 KB |
Output is correct |
14 |
Correct |
2 ms |
18780 KB |
Output is correct |
15 |
Correct |
2 ms |
18780 KB |
Output is correct |
16 |
Correct |
3 ms |
18780 KB |
Output is correct |
17 |
Correct |
2 ms |
18780 KB |
Output is correct |
18 |
Correct |
2 ms |
18776 KB |
Output is correct |
19 |
Correct |
2 ms |
18780 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
4 ms |
18888 KB |
Output is correct |
22 |
Correct |
5 ms |
18780 KB |
Output is correct |
23 |
Correct |
4 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19036 KB |
Output is correct |
25 |
Correct |
3 ms |
19036 KB |
Output is correct |
26 |
Correct |
3 ms |
19032 KB |
Output is correct |
27 |
Correct |
3 ms |
18780 KB |
Output is correct |
28 |
Correct |
4 ms |
18780 KB |
Output is correct |
29 |
Correct |
3 ms |
18912 KB |
Output is correct |
30 |
Correct |
4 ms |
19032 KB |
Output is correct |
31 |
Correct |
4 ms |
18780 KB |
Output is correct |
32 |
Correct |
4 ms |
18776 KB |
Output is correct |
33 |
Correct |
4 ms |
19036 KB |
Output is correct |
34 |
Correct |
3 ms |
19036 KB |
Output is correct |
35 |
Correct |
4 ms |
19036 KB |
Output is correct |
36 |
Correct |
4 ms |
18780 KB |
Output is correct |
37 |
Correct |
4 ms |
18780 KB |
Output is correct |
38 |
Correct |
4 ms |
18892 KB |
Output is correct |
39 |
Correct |
179 ms |
24912 KB |
Output is correct |
40 |
Correct |
175 ms |
26132 KB |
Output is correct |
41 |
Correct |
166 ms |
25680 KB |
Output is correct |
42 |
Correct |
149 ms |
26192 KB |
Output is correct |
43 |
Correct |
233 ms |
26992 KB |
Output is correct |
44 |
Correct |
93 ms |
26352 KB |
Output is correct |
45 |
Incorrect |
343 ms |
39508 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |