#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define S second
#define F first
#define pii pair<ll,ll>
#define vi vector <ll>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF INT_MAX
#define MOD 998244353
#define MAXN 200000
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
vvp adj(MAXN);
vb deleted(MAXN);
vi s(MAXN);
ll k;
void up(ll v,ll p){
if (deleted[v])s[v]=0;else{
s[v]=1;
for (auto& [to,w]:adj[v]){
if (to==p)continue;
up(to,v);
s[v]+=s[to];
}
}
}
ll find(ll v,ll p,ll n){
for (auto& [to,w]:adj[v])
if (to!=p && !deleted[to] && s[to]>n/2)
return find(to,v,n);
return v;
}
vi cnt(1000001,INF);
ll ans=INF;
void add(ll v,ll p,ll dist,ll depth){
if (dist>k)return;
cnt[dist]=min(cnt[dist],depth);
for (auto& [to,w]:adj[v])
if (to!=p && !deleted[to])
add(to,v,dist+w,depth+1);
}
void calc(ll v,ll p,ll dist,ll depth){
if (dist>k)return;
ans=min(ans,depth+cnt[k-dist]);
// cout<<v<<" "<<p<<" "<<dist<<" "<<depth<<" "<<cnt[k-dist]<<"\n";
for (auto& [to,w]:adj[v])
if (to!=p && !deleted[to])
calc(to,v,dist+w,depth+1);
}
void remove(ll v,ll p,ll dist){
if (dist>k)return;
cnt[dist]=INF;
for (auto& [to,w]:adj[v])
if (to!=p && !deleted[to])
remove(to,v,dist+w);
}
void build(ll v){
up(v,-1);
ll c=find(v,-1,s[v]);
cnt[0]=0;
for (auto& [to,w]:adj[c]){
if (!deleted[to]){
calc(to,c,w,1);
add(to,c,w,1);
}
}
for (auto& [to,w]:adj[c]){
if (!deleted[to]){
remove(to,c,w);
}
}
bool check=true;
for (int i=1;i<=1000000;i++)if (cnt[i]!=INF)check=false;
deleted[c]=true;
for (auto &[to,w]:adj[c])if (!deleted[to])build(to);
}
int best_path(int N, int K, int H[][2], int L[]){
k=K;
for (ll i=0;i<N-1;i++){
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
build(0);
if (ans==INF)return -1;
return ans;
}
Compilation message
race.cpp: In function 'void up(long long int, long long int)':
race.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for (auto& [to,w]:adj[v]){
| ^
race.cpp: In function 'long long int find(long long int, long long int, long long int)':
race.cpp:34:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for (auto& [to,w]:adj[v])
| ^
race.cpp: In function 'void add(long long int, long long int, long long int, long long int)':
race.cpp:44:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for (auto& [to,w]:adj[v])
| ^
race.cpp: In function 'void calc(long long int, long long int, long long int, long long int)':
race.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | for (auto& [to,w]:adj[v])
| ^
race.cpp: In function 'void remove(long long int, long long int, long long int)':
race.cpp:59:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | for (auto& [to,w]:adj[v])
| ^
race.cpp: In function 'void build(long long int)':
race.cpp:67:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
67 | for (auto& [to,w]:adj[c]){
| ^
race.cpp:73:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for (auto& [to,w]:adj[c]){
| ^
race.cpp:81:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | for (auto &[to,w]:adj[c])if (!deleted[to])build(to);
| ^
race.cpp:78:7: warning: variable 'check' set but not used [-Wunused-but-set-variable]
78 | bool check=true;
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
5 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
6 ms |
16476 KB |
Output is correct |
9 |
Correct |
4 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16476 KB |
Output is correct |
11 |
Correct |
5 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16476 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
4 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16476 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
5 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
5 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
6 ms |
16476 KB |
Output is correct |
9 |
Correct |
4 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16476 KB |
Output is correct |
11 |
Correct |
5 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16476 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
4 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16476 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
5 ms |
16472 KB |
Output is correct |
19 |
Correct |
4 ms |
16476 KB |
Output is correct |
20 |
Correct |
5 ms |
16476 KB |
Output is correct |
21 |
Correct |
5 ms |
16640 KB |
Output is correct |
22 |
Correct |
5 ms |
16476 KB |
Output is correct |
23 |
Correct |
5 ms |
16684 KB |
Output is correct |
24 |
Correct |
6 ms |
16732 KB |
Output is correct |
25 |
Correct |
5 ms |
16656 KB |
Output is correct |
26 |
Correct |
5 ms |
16476 KB |
Output is correct |
27 |
Correct |
5 ms |
16476 KB |
Output is correct |
28 |
Correct |
5 ms |
16476 KB |
Output is correct |
29 |
Correct |
5 ms |
16476 KB |
Output is correct |
30 |
Correct |
4 ms |
16476 KB |
Output is correct |
31 |
Correct |
6 ms |
16476 KB |
Output is correct |
32 |
Correct |
5 ms |
16476 KB |
Output is correct |
33 |
Correct |
6 ms |
16476 KB |
Output is correct |
34 |
Correct |
4 ms |
16476 KB |
Output is correct |
35 |
Correct |
5 ms |
16476 KB |
Output is correct |
36 |
Correct |
5 ms |
16476 KB |
Output is correct |
37 |
Correct |
4 ms |
16596 KB |
Output is correct |
38 |
Correct |
5 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
5 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
6 ms |
16476 KB |
Output is correct |
9 |
Correct |
4 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16476 KB |
Output is correct |
11 |
Correct |
5 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16476 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
4 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16476 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
5 ms |
16472 KB |
Output is correct |
19 |
Correct |
102 ms |
23912 KB |
Output is correct |
20 |
Correct |
86 ms |
24916 KB |
Output is correct |
21 |
Correct |
96 ms |
24924 KB |
Output is correct |
22 |
Correct |
90 ms |
24972 KB |
Output is correct |
23 |
Correct |
53 ms |
25168 KB |
Output is correct |
24 |
Correct |
42 ms |
23384 KB |
Output is correct |
25 |
Correct |
87 ms |
28052 KB |
Output is correct |
26 |
Correct |
77 ms |
31032 KB |
Output is correct |
27 |
Correct |
118 ms |
32120 KB |
Output is correct |
28 |
Correct |
178 ms |
43524 KB |
Output is correct |
29 |
Correct |
166 ms |
42576 KB |
Output is correct |
30 |
Correct |
114 ms |
32080 KB |
Output is correct |
31 |
Correct |
124 ms |
32336 KB |
Output is correct |
32 |
Correct |
127 ms |
32080 KB |
Output is correct |
33 |
Correct |
142 ms |
30804 KB |
Output is correct |
34 |
Correct |
121 ms |
31568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16472 KB |
Output is correct |
2 |
Correct |
4 ms |
16476 KB |
Output is correct |
3 |
Correct |
5 ms |
16476 KB |
Output is correct |
4 |
Correct |
4 ms |
16476 KB |
Output is correct |
5 |
Correct |
4 ms |
16476 KB |
Output is correct |
6 |
Correct |
4 ms |
16476 KB |
Output is correct |
7 |
Correct |
4 ms |
16476 KB |
Output is correct |
8 |
Correct |
6 ms |
16476 KB |
Output is correct |
9 |
Correct |
4 ms |
16476 KB |
Output is correct |
10 |
Correct |
4 ms |
16476 KB |
Output is correct |
11 |
Correct |
5 ms |
16476 KB |
Output is correct |
12 |
Correct |
4 ms |
16476 KB |
Output is correct |
13 |
Correct |
4 ms |
16476 KB |
Output is correct |
14 |
Correct |
4 ms |
16476 KB |
Output is correct |
15 |
Correct |
4 ms |
16476 KB |
Output is correct |
16 |
Correct |
4 ms |
16476 KB |
Output is correct |
17 |
Correct |
4 ms |
16476 KB |
Output is correct |
18 |
Correct |
5 ms |
16472 KB |
Output is correct |
19 |
Correct |
4 ms |
16476 KB |
Output is correct |
20 |
Correct |
5 ms |
16476 KB |
Output is correct |
21 |
Correct |
5 ms |
16640 KB |
Output is correct |
22 |
Correct |
5 ms |
16476 KB |
Output is correct |
23 |
Correct |
5 ms |
16684 KB |
Output is correct |
24 |
Correct |
6 ms |
16732 KB |
Output is correct |
25 |
Correct |
5 ms |
16656 KB |
Output is correct |
26 |
Correct |
5 ms |
16476 KB |
Output is correct |
27 |
Correct |
5 ms |
16476 KB |
Output is correct |
28 |
Correct |
5 ms |
16476 KB |
Output is correct |
29 |
Correct |
5 ms |
16476 KB |
Output is correct |
30 |
Correct |
4 ms |
16476 KB |
Output is correct |
31 |
Correct |
6 ms |
16476 KB |
Output is correct |
32 |
Correct |
5 ms |
16476 KB |
Output is correct |
33 |
Correct |
6 ms |
16476 KB |
Output is correct |
34 |
Correct |
4 ms |
16476 KB |
Output is correct |
35 |
Correct |
5 ms |
16476 KB |
Output is correct |
36 |
Correct |
5 ms |
16476 KB |
Output is correct |
37 |
Correct |
4 ms |
16596 KB |
Output is correct |
38 |
Correct |
5 ms |
16472 KB |
Output is correct |
39 |
Correct |
102 ms |
23912 KB |
Output is correct |
40 |
Correct |
86 ms |
24916 KB |
Output is correct |
41 |
Correct |
96 ms |
24924 KB |
Output is correct |
42 |
Correct |
90 ms |
24972 KB |
Output is correct |
43 |
Correct |
53 ms |
25168 KB |
Output is correct |
44 |
Correct |
42 ms |
23384 KB |
Output is correct |
45 |
Correct |
87 ms |
28052 KB |
Output is correct |
46 |
Correct |
77 ms |
31032 KB |
Output is correct |
47 |
Correct |
118 ms |
32120 KB |
Output is correct |
48 |
Correct |
178 ms |
43524 KB |
Output is correct |
49 |
Correct |
166 ms |
42576 KB |
Output is correct |
50 |
Correct |
114 ms |
32080 KB |
Output is correct |
51 |
Correct |
124 ms |
32336 KB |
Output is correct |
52 |
Correct |
127 ms |
32080 KB |
Output is correct |
53 |
Correct |
142 ms |
30804 KB |
Output is correct |
54 |
Correct |
121 ms |
31568 KB |
Output is correct |
55 |
Correct |
10 ms |
17240 KB |
Output is correct |
56 |
Correct |
12 ms |
17092 KB |
Output is correct |
57 |
Correct |
61 ms |
25600 KB |
Output is correct |
58 |
Correct |
33 ms |
24512 KB |
Output is correct |
59 |
Correct |
77 ms |
31108 KB |
Output is correct |
60 |
Correct |
358 ms |
43028 KB |
Output is correct |
61 |
Correct |
136 ms |
32624 KB |
Output is correct |
62 |
Correct |
139 ms |
32800 KB |
Output is correct |
63 |
Correct |
178 ms |
32580 KB |
Output is correct |
64 |
Correct |
345 ms |
32440 KB |
Output is correct |
65 |
Correct |
151 ms |
33104 KB |
Output is correct |
66 |
Correct |
246 ms |
40952 KB |
Output is correct |
67 |
Correct |
77 ms |
32492 KB |
Output is correct |
68 |
Correct |
200 ms |
32848 KB |
Output is correct |
69 |
Correct |
193 ms |
32672 KB |
Output is correct |
70 |
Correct |
166 ms |
31824 KB |
Output is correct |