#include <bits/stdc++.h>
using namespace std;
#define MAX_N 300000
long long n,k;
long long ans=1e7;
long long cnst1[MAX_N],cnst2[MAX_N];
vector <array<long long,2>> v[MAX_N];
set <array<long long,2>> nodes[MAX_N];
void dfs(long long i,long long last){
if(v[i].size()==1&&i!=1){
nodes[i].insert({0,0});
return;
}
long long x,siz=0,c;
for(auto [j,w]:v[i]){
if(j==last) continue;
dfs(j,i);
if(nodes[j].size()>siz){
siz=nodes[j].size();
x=j;
c=w;
}
}
swap(nodes[i],nodes[x]);
long long co=c,le=1;
auto it=nodes[i].lower_bound({k-cnst1[x]-co,-1000000});
if(it!=nodes[i].end()&&(*it)[0]==k-cnst1[x]-co){
ans=min(ans,le+(*it)[1]+cnst2[x]);
}
cnst1[i]=cnst1[x]+c,cnst2[i]=cnst2[x]+1;
nodes[i].insert({co-cnst1[i],le-cnst2[i]});
for(auto [j,w]:v[i]){
if(j==last||j==x) continue;
for(auto [cost,len]:nodes[j]){
co=cost+cnst1[j]+w,le=len+cnst2[j]+1;
auto it=nodes[i].lower_bound({k-cnst1[i]-co,-1000000});
if(it!=nodes[i].end()&&(*it)[0]==k-cnst1[i]-co){
ans=min(ans,le+(*it)[1]+cnst2[i]);
}
}
for(auto [cost,len]:nodes[j]){
co=cost+cnst1[j]+w,le=len+cnst2[j]+1;
nodes[i].insert({co-cnst1[i],le-cnst2[i]});
}
nodes[j].clear();
}
return;
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
for(int i=0;i<n-1;i++){
v[H[i][0]+1].push_back({H[i][1]+1,L[i]});
v[H[i][1]+1].push_back({H[i][0]+1,L[i]});
if(L[i]==k) return 1;
}
dfs(1,0);
if(ans!=1e7) return (int)ans;
else return -1;
}
// signed main()
// {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
// int N, K;
// int H[MAX_N][2];
// int L[MAX_N];
// int solution;
// cin>>N>>K;
// for(int i=0;i<N-1;i++) cin>>H[i][0]>>H[i][1]>>L[i];
// cout<<best_path(N,K,H,L)<<endl;
// return 0;
// }
Compilation message
race.cpp: In function 'void dfs(long long int, long long int)':
race.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [j,w]:v[i]){
| ^
race.cpp:18:27: warning: comparison of integer expressions of different signedness: 'std::set<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
18 | if(nodes[j].size()>siz){
| ~~~~~~~~~~~~~~~^~~~
race.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | for(auto [j,w]:v[i]){
| ^
race.cpp:35:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for(auto [cost,len]:nodes[j]){
| ^
race.cpp:42:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
42 | for(auto [cost,len]:nodes[j]){
| ^
race.cpp:30:22: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
30 | cnst1[i]=cnst1[x]+c,cnst2[i]=cnst2[x]+1;
| ~~~~~~~~^~
race.cpp:14:15: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
14 | long long x,siz=0,c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21632 KB |
Output is correct |
3 |
Correct |
9 ms |
21848 KB |
Output is correct |
4 |
Correct |
9 ms |
21636 KB |
Output is correct |
5 |
Correct |
9 ms |
21848 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21568 KB |
Output is correct |
8 |
Correct |
9 ms |
21520 KB |
Output is correct |
9 |
Correct |
9 ms |
21512 KB |
Output is correct |
10 |
Correct |
9 ms |
21592 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
10 ms |
21500 KB |
Output is correct |
13 |
Correct |
10 ms |
21596 KB |
Output is correct |
14 |
Correct |
9 ms |
21592 KB |
Output is correct |
15 |
Correct |
10 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21552 KB |
Output is correct |
17 |
Correct |
9 ms |
21596 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21632 KB |
Output is correct |
3 |
Correct |
9 ms |
21848 KB |
Output is correct |
4 |
Correct |
9 ms |
21636 KB |
Output is correct |
5 |
Correct |
9 ms |
21848 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21568 KB |
Output is correct |
8 |
Correct |
9 ms |
21520 KB |
Output is correct |
9 |
Correct |
9 ms |
21512 KB |
Output is correct |
10 |
Correct |
9 ms |
21592 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
10 ms |
21500 KB |
Output is correct |
13 |
Correct |
10 ms |
21596 KB |
Output is correct |
14 |
Correct |
9 ms |
21592 KB |
Output is correct |
15 |
Correct |
10 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21552 KB |
Output is correct |
17 |
Correct |
9 ms |
21596 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
19 |
Correct |
11 ms |
21596 KB |
Output is correct |
20 |
Correct |
12 ms |
21596 KB |
Output is correct |
21 |
Correct |
11 ms |
21596 KB |
Output is correct |
22 |
Incorrect |
11 ms |
21596 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21632 KB |
Output is correct |
3 |
Correct |
9 ms |
21848 KB |
Output is correct |
4 |
Correct |
9 ms |
21636 KB |
Output is correct |
5 |
Correct |
9 ms |
21848 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21568 KB |
Output is correct |
8 |
Correct |
9 ms |
21520 KB |
Output is correct |
9 |
Correct |
9 ms |
21512 KB |
Output is correct |
10 |
Correct |
9 ms |
21592 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
10 ms |
21500 KB |
Output is correct |
13 |
Correct |
10 ms |
21596 KB |
Output is correct |
14 |
Correct |
9 ms |
21592 KB |
Output is correct |
15 |
Correct |
10 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21552 KB |
Output is correct |
17 |
Correct |
9 ms |
21596 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
19 |
Correct |
79 ms |
29820 KB |
Output is correct |
20 |
Correct |
87 ms |
29996 KB |
Output is correct |
21 |
Incorrect |
79 ms |
29700 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
21596 KB |
Output is correct |
2 |
Correct |
8 ms |
21632 KB |
Output is correct |
3 |
Correct |
9 ms |
21848 KB |
Output is correct |
4 |
Correct |
9 ms |
21636 KB |
Output is correct |
5 |
Correct |
9 ms |
21848 KB |
Output is correct |
6 |
Correct |
8 ms |
21596 KB |
Output is correct |
7 |
Correct |
9 ms |
21568 KB |
Output is correct |
8 |
Correct |
9 ms |
21520 KB |
Output is correct |
9 |
Correct |
9 ms |
21512 KB |
Output is correct |
10 |
Correct |
9 ms |
21592 KB |
Output is correct |
11 |
Correct |
9 ms |
21596 KB |
Output is correct |
12 |
Correct |
10 ms |
21500 KB |
Output is correct |
13 |
Correct |
10 ms |
21596 KB |
Output is correct |
14 |
Correct |
9 ms |
21592 KB |
Output is correct |
15 |
Correct |
10 ms |
21596 KB |
Output is correct |
16 |
Correct |
9 ms |
21552 KB |
Output is correct |
17 |
Correct |
9 ms |
21596 KB |
Output is correct |
18 |
Correct |
10 ms |
21596 KB |
Output is correct |
19 |
Correct |
11 ms |
21596 KB |
Output is correct |
20 |
Correct |
12 ms |
21596 KB |
Output is correct |
21 |
Correct |
11 ms |
21596 KB |
Output is correct |
22 |
Incorrect |
11 ms |
21596 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |