#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
int n, k;
vector<pii> g[200010];
bool v[200010];
int mn[200010][101];
int res=1e9;
void dfs(int s){
v[s]=true;
mn[s][0]=1;
for(auto pr:g[s]){
int u=pr.ft, d=pr.sc;
if(!v[u]){
dfs(u);
for(int sum=0; (sum+d)<=( k ); sum++){
if( (mn[s][k-(sum+d) ]>0) && (mn[u][sum]>0) ){
res=min(res, mn[s][k-(sum+d)]+mn[u][sum]);
}
}
for(int sum=0; ( (sum+d)<=k ); sum++){
if( (mn[u][sum]>0) ){
if(mn[s][sum+d]==0){
mn[s][sum+d]=mn[u][sum]+1;
}
else{
mn[s][sum+d]=min(mn[s][sum+d], mn[u][sum]+1);
}
}
}
}
}
if(mn[s][k]>0){
res=min(res, mn[s][k]);
}
return;
}
int dfs2(int s, int l, int h){
int res=1e9;
if(l>k){
v[s]=false;
return res;
}
if(l==k){
v[s]=false;
return h;
}
v[s]=true;
for(pii pr:g[s]){
int u=pr.ft, d=pr.sc;
if(!v[u]){
res=min(dfs2(u, l+d, h+1), res);
}
}
v[s]=false;
return res;
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
for(int i=0; i<(n-1); i++){
int u=H[i][0]+1, v=H[i][1]+1, d=L[i];
g[u].pb({v, d}); g[v].pb({u, d});
}
if( (n>1000) ){ dfs(1);}
else{
for(int i=1; i<=n; i++){
res=min(res, dfs2(i, 0, 1));
}
}
if(res==(1e9)){
return -1;
}
return res-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
3 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
3 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
3 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
3 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
19 |
Correct |
3 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
19 ms |
5100 KB |
Output is correct |
22 |
Correct |
4 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5100 KB |
Output is correct |
25 |
Correct |
19 ms |
5100 KB |
Output is correct |
26 |
Correct |
4 ms |
5100 KB |
Output is correct |
27 |
Correct |
4 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5100 KB |
Output is correct |
29 |
Correct |
12 ms |
5100 KB |
Output is correct |
30 |
Correct |
13 ms |
5100 KB |
Output is correct |
31 |
Correct |
18 ms |
5100 KB |
Output is correct |
32 |
Correct |
20 ms |
5100 KB |
Output is correct |
33 |
Correct |
16 ms |
5100 KB |
Output is correct |
34 |
Correct |
5 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5100 KB |
Output is correct |
36 |
Correct |
5 ms |
5100 KB |
Output is correct |
37 |
Correct |
12 ms |
5100 KB |
Output is correct |
38 |
Correct |
15 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
3 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
3 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
19 |
Correct |
127 ms |
49516 KB |
Output is correct |
20 |
Correct |
129 ms |
49516 KB |
Output is correct |
21 |
Correct |
129 ms |
49516 KB |
Output is correct |
22 |
Correct |
135 ms |
49652 KB |
Output is correct |
23 |
Correct |
99 ms |
49772 KB |
Output is correct |
24 |
Correct |
101 ms |
49772 KB |
Output is correct |
25 |
Correct |
122 ms |
53868 KB |
Output is correct |
26 |
Correct |
93 ms |
58348 KB |
Output is correct |
27 |
Correct |
244 ms |
93804 KB |
Output is correct |
28 |
Correct |
240 ms |
111724 KB |
Output is correct |
29 |
Correct |
238 ms |
110444 KB |
Output is correct |
30 |
Correct |
244 ms |
93676 KB |
Output is correct |
31 |
Correct |
249 ms |
93804 KB |
Output is correct |
32 |
Correct |
301 ms |
93804 KB |
Output is correct |
33 |
Correct |
258 ms |
92908 KB |
Output is correct |
34 |
Correct |
189 ms |
92396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
3 ms |
5100 KB |
Output is correct |
7 |
Correct |
3 ms |
5100 KB |
Output is correct |
8 |
Correct |
3 ms |
5100 KB |
Output is correct |
9 |
Correct |
3 ms |
5100 KB |
Output is correct |
10 |
Correct |
3 ms |
5100 KB |
Output is correct |
11 |
Correct |
4 ms |
5100 KB |
Output is correct |
12 |
Correct |
3 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
3 ms |
5100 KB |
Output is correct |
19 |
Correct |
3 ms |
4972 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
19 ms |
5100 KB |
Output is correct |
22 |
Correct |
4 ms |
5100 KB |
Output is correct |
23 |
Correct |
4 ms |
5100 KB |
Output is correct |
24 |
Correct |
4 ms |
5100 KB |
Output is correct |
25 |
Correct |
19 ms |
5100 KB |
Output is correct |
26 |
Correct |
4 ms |
5100 KB |
Output is correct |
27 |
Correct |
4 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5100 KB |
Output is correct |
29 |
Correct |
12 ms |
5100 KB |
Output is correct |
30 |
Correct |
13 ms |
5100 KB |
Output is correct |
31 |
Correct |
18 ms |
5100 KB |
Output is correct |
32 |
Correct |
20 ms |
5100 KB |
Output is correct |
33 |
Correct |
16 ms |
5100 KB |
Output is correct |
34 |
Correct |
5 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5100 KB |
Output is correct |
36 |
Correct |
5 ms |
5100 KB |
Output is correct |
37 |
Correct |
12 ms |
5100 KB |
Output is correct |
38 |
Correct |
15 ms |
5100 KB |
Output is correct |
39 |
Correct |
127 ms |
49516 KB |
Output is correct |
40 |
Correct |
129 ms |
49516 KB |
Output is correct |
41 |
Correct |
129 ms |
49516 KB |
Output is correct |
42 |
Correct |
135 ms |
49652 KB |
Output is correct |
43 |
Correct |
99 ms |
49772 KB |
Output is correct |
44 |
Correct |
101 ms |
49772 KB |
Output is correct |
45 |
Correct |
122 ms |
53868 KB |
Output is correct |
46 |
Correct |
93 ms |
58348 KB |
Output is correct |
47 |
Correct |
244 ms |
93804 KB |
Output is correct |
48 |
Correct |
240 ms |
111724 KB |
Output is correct |
49 |
Correct |
238 ms |
110444 KB |
Output is correct |
50 |
Correct |
244 ms |
93676 KB |
Output is correct |
51 |
Correct |
249 ms |
93804 KB |
Output is correct |
52 |
Correct |
301 ms |
93804 KB |
Output is correct |
53 |
Correct |
258 ms |
92908 KB |
Output is correct |
54 |
Correct |
189 ms |
92396 KB |
Output is correct |
55 |
Incorrect |
138 ms |
9788 KB |
Output isn't correct |
56 |
Halted |
0 ms |
0 KB |
- |