#include<bits/stdc++.h>
#include "race.h"
using namespace std;
const int N = 2e5+37;
int mi = 1e7, n, k, p;
vector<array<int, 2>> adj[N];
vector<int> s(N), vis2(N);
vector<int> best((int)1e6+37, 1e7);
vector<array<int, 2>> mp;
vector<int> pff;
void dfs(int v, int p){
int pf=0;
s[v] = 1;
for(auto i: adj[v]){
if(vis2[i[0]]||i[0]==p) continue;
pf++;
dfs(i[0], v); s[v]+=s[i[0]];
}
}
int dfs2(int v, int p, int gh){
for(auto i: adj[v]){
if(vis2[i[0]]||i[0]==p) continue;
if(s[i[0]]>=gh/2) return dfs2(i[0], v, gh);
}
return v;
}
void dfs3(int v, int dist, int z, int p){
// cout<<v<<" "<<dist<<" "<<z<<"\n";
if(dist<=k){
mp.push_back({dist, z});
for(auto i: adj[v]){
if(vis2[i[0]]||i[0]==p) continue;
dfs3(i[0], dist+i[1], z+1, v);
}
}
}
void dfs4(int v){
for(auto pf: adj[v]){
if(vis2[pf[0]]) continue;
mp.clear();
dfs3(pf[0], pf[1], 1, v);
for(auto i: mp){
if(i[0]>k) continue;
if(best[k-i[0]]!=1e7){
mi=min(mi, i[1]+best[k-i[0]]);
}
}
for(auto i: mp){
if(i[0]<=k){
best[i[0]]=min(i[1], best[i[0]]);
pff.push_back(i[0]);
}
}
}
}
void gh(int v){
dfs(v, v);
int x = dfs2(v, v, s[v]);
dfs4(x);
for(auto i:pff) if(i<=k)best[i]=1e7;
pff.clear();
vis2[x]=1; best[0]=0;
for(auto i: adj[x]){
if(!vis2[i[0]]) gh(i[0]);
}
}
int best_path(int N, int K, int a[][2], int l[])
{
best[0]=0;
n = N, k = K;
for(int i= 0; i < n-1; i++){
int x=a[i][0], y=a[i][1], z=l[i];
adj[x].push_back({y, z});
adj[y].push_back({x, z});
}
p=1;
gh(0);
if(mi==1e7) return -1;
return mi;
}
void f(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
/*
signed main()
{
f();
int n, k; cin >> n >> k;
int a[n-1][2], l[n-1];
for(int i=0; i<n-1; i++){
cin>>a[i][0]>>a[i][1] >> l[i];
}
cout<< best_path(n, k, a, l);
cerr<<(float)clock()/CLOCKS_PER_SEC;
}
*/
Compilation message
race.cpp: In function 'void f()':
race.cpp:107:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp:108:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10452 KB |
Output is correct |
2 |
Correct |
5 ms |
10452 KB |
Output is correct |
3 |
Correct |
5 ms |
10452 KB |
Output is correct |
4 |
Correct |
5 ms |
10516 KB |
Output is correct |
5 |
Correct |
4 ms |
10452 KB |
Output is correct |
6 |
Correct |
5 ms |
10500 KB |
Output is correct |
7 |
Correct |
5 ms |
10452 KB |
Output is correct |
8 |
Correct |
5 ms |
10452 KB |
Output is correct |
9 |
Correct |
4 ms |
10452 KB |
Output is correct |
10 |
Correct |
5 ms |
10452 KB |
Output is correct |
11 |
Correct |
5 ms |
10452 KB |
Output is correct |
12 |
Correct |
5 ms |
10452 KB |
Output is correct |
13 |
Correct |
4 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10520 KB |
Output is correct |
15 |
Correct |
5 ms |
10452 KB |
Output is correct |
16 |
Correct |
4 ms |
10452 KB |
Output is correct |
17 |
Correct |
5 ms |
10452 KB |
Output is correct |
18 |
Correct |
5 ms |
10480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10452 KB |
Output is correct |
2 |
Correct |
5 ms |
10452 KB |
Output is correct |
3 |
Correct |
5 ms |
10452 KB |
Output is correct |
4 |
Correct |
5 ms |
10516 KB |
Output is correct |
5 |
Correct |
4 ms |
10452 KB |
Output is correct |
6 |
Correct |
5 ms |
10500 KB |
Output is correct |
7 |
Correct |
5 ms |
10452 KB |
Output is correct |
8 |
Correct |
5 ms |
10452 KB |
Output is correct |
9 |
Correct |
4 ms |
10452 KB |
Output is correct |
10 |
Correct |
5 ms |
10452 KB |
Output is correct |
11 |
Correct |
5 ms |
10452 KB |
Output is correct |
12 |
Correct |
5 ms |
10452 KB |
Output is correct |
13 |
Correct |
4 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10520 KB |
Output is correct |
15 |
Correct |
5 ms |
10452 KB |
Output is correct |
16 |
Correct |
4 ms |
10452 KB |
Output is correct |
17 |
Correct |
5 ms |
10452 KB |
Output is correct |
18 |
Correct |
5 ms |
10480 KB |
Output is correct |
19 |
Correct |
5 ms |
10452 KB |
Output is correct |
20 |
Correct |
5 ms |
10452 KB |
Output is correct |
21 |
Correct |
5 ms |
10452 KB |
Output is correct |
22 |
Correct |
5 ms |
10452 KB |
Output is correct |
23 |
Correct |
6 ms |
10436 KB |
Output is correct |
24 |
Correct |
5 ms |
10452 KB |
Output is correct |
25 |
Correct |
5 ms |
10452 KB |
Output is correct |
26 |
Correct |
6 ms |
10476 KB |
Output is correct |
27 |
Correct |
5 ms |
10452 KB |
Output is correct |
28 |
Correct |
5 ms |
10452 KB |
Output is correct |
29 |
Correct |
6 ms |
10452 KB |
Output is correct |
30 |
Correct |
5 ms |
10452 KB |
Output is correct |
31 |
Correct |
5 ms |
10452 KB |
Output is correct |
32 |
Correct |
6 ms |
10548 KB |
Output is correct |
33 |
Correct |
5 ms |
10452 KB |
Output is correct |
34 |
Correct |
6 ms |
10452 KB |
Output is correct |
35 |
Correct |
5 ms |
10560 KB |
Output is correct |
36 |
Correct |
5 ms |
10452 KB |
Output is correct |
37 |
Correct |
5 ms |
10580 KB |
Output is correct |
38 |
Correct |
5 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10452 KB |
Output is correct |
2 |
Correct |
5 ms |
10452 KB |
Output is correct |
3 |
Correct |
5 ms |
10452 KB |
Output is correct |
4 |
Correct |
5 ms |
10516 KB |
Output is correct |
5 |
Correct |
4 ms |
10452 KB |
Output is correct |
6 |
Correct |
5 ms |
10500 KB |
Output is correct |
7 |
Correct |
5 ms |
10452 KB |
Output is correct |
8 |
Correct |
5 ms |
10452 KB |
Output is correct |
9 |
Correct |
4 ms |
10452 KB |
Output is correct |
10 |
Correct |
5 ms |
10452 KB |
Output is correct |
11 |
Correct |
5 ms |
10452 KB |
Output is correct |
12 |
Correct |
5 ms |
10452 KB |
Output is correct |
13 |
Correct |
4 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10520 KB |
Output is correct |
15 |
Correct |
5 ms |
10452 KB |
Output is correct |
16 |
Correct |
4 ms |
10452 KB |
Output is correct |
17 |
Correct |
5 ms |
10452 KB |
Output is correct |
18 |
Correct |
5 ms |
10480 KB |
Output is correct |
19 |
Correct |
78 ms |
15512 KB |
Output is correct |
20 |
Correct |
97 ms |
15392 KB |
Output is correct |
21 |
Correct |
80 ms |
15880 KB |
Output is correct |
22 |
Correct |
72 ms |
16260 KB |
Output is correct |
23 |
Correct |
50 ms |
15516 KB |
Output is correct |
24 |
Correct |
40 ms |
15408 KB |
Output is correct |
25 |
Correct |
86 ms |
18884 KB |
Output is correct |
26 |
Correct |
79 ms |
23756 KB |
Output is correct |
27 |
Correct |
103 ms |
20376 KB |
Output is correct |
28 |
Correct |
173 ms |
34684 KB |
Output is correct |
29 |
Correct |
171 ms |
33484 KB |
Output is correct |
30 |
Correct |
103 ms |
20400 KB |
Output is correct |
31 |
Correct |
106 ms |
20560 KB |
Output is correct |
32 |
Correct |
122 ms |
20372 KB |
Output is correct |
33 |
Correct |
173 ms |
19312 KB |
Output is correct |
34 |
Correct |
121 ms |
19176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
10452 KB |
Output is correct |
2 |
Correct |
5 ms |
10452 KB |
Output is correct |
3 |
Correct |
5 ms |
10452 KB |
Output is correct |
4 |
Correct |
5 ms |
10516 KB |
Output is correct |
5 |
Correct |
4 ms |
10452 KB |
Output is correct |
6 |
Correct |
5 ms |
10500 KB |
Output is correct |
7 |
Correct |
5 ms |
10452 KB |
Output is correct |
8 |
Correct |
5 ms |
10452 KB |
Output is correct |
9 |
Correct |
4 ms |
10452 KB |
Output is correct |
10 |
Correct |
5 ms |
10452 KB |
Output is correct |
11 |
Correct |
5 ms |
10452 KB |
Output is correct |
12 |
Correct |
5 ms |
10452 KB |
Output is correct |
13 |
Correct |
4 ms |
10452 KB |
Output is correct |
14 |
Correct |
5 ms |
10520 KB |
Output is correct |
15 |
Correct |
5 ms |
10452 KB |
Output is correct |
16 |
Correct |
4 ms |
10452 KB |
Output is correct |
17 |
Correct |
5 ms |
10452 KB |
Output is correct |
18 |
Correct |
5 ms |
10480 KB |
Output is correct |
19 |
Correct |
5 ms |
10452 KB |
Output is correct |
20 |
Correct |
5 ms |
10452 KB |
Output is correct |
21 |
Correct |
5 ms |
10452 KB |
Output is correct |
22 |
Correct |
5 ms |
10452 KB |
Output is correct |
23 |
Correct |
6 ms |
10436 KB |
Output is correct |
24 |
Correct |
5 ms |
10452 KB |
Output is correct |
25 |
Correct |
5 ms |
10452 KB |
Output is correct |
26 |
Correct |
6 ms |
10476 KB |
Output is correct |
27 |
Correct |
5 ms |
10452 KB |
Output is correct |
28 |
Correct |
5 ms |
10452 KB |
Output is correct |
29 |
Correct |
6 ms |
10452 KB |
Output is correct |
30 |
Correct |
5 ms |
10452 KB |
Output is correct |
31 |
Correct |
5 ms |
10452 KB |
Output is correct |
32 |
Correct |
6 ms |
10548 KB |
Output is correct |
33 |
Correct |
5 ms |
10452 KB |
Output is correct |
34 |
Correct |
6 ms |
10452 KB |
Output is correct |
35 |
Correct |
5 ms |
10560 KB |
Output is correct |
36 |
Correct |
5 ms |
10452 KB |
Output is correct |
37 |
Correct |
5 ms |
10580 KB |
Output is correct |
38 |
Correct |
5 ms |
10452 KB |
Output is correct |
39 |
Correct |
78 ms |
15512 KB |
Output is correct |
40 |
Correct |
97 ms |
15392 KB |
Output is correct |
41 |
Correct |
80 ms |
15880 KB |
Output is correct |
42 |
Correct |
72 ms |
16260 KB |
Output is correct |
43 |
Correct |
50 ms |
15516 KB |
Output is correct |
44 |
Correct |
40 ms |
15408 KB |
Output is correct |
45 |
Correct |
86 ms |
18884 KB |
Output is correct |
46 |
Correct |
79 ms |
23756 KB |
Output is correct |
47 |
Correct |
103 ms |
20376 KB |
Output is correct |
48 |
Correct |
173 ms |
34684 KB |
Output is correct |
49 |
Correct |
171 ms |
33484 KB |
Output is correct |
50 |
Correct |
103 ms |
20400 KB |
Output is correct |
51 |
Correct |
106 ms |
20560 KB |
Output is correct |
52 |
Correct |
122 ms |
20372 KB |
Output is correct |
53 |
Correct |
173 ms |
19312 KB |
Output is correct |
54 |
Correct |
121 ms |
19176 KB |
Output is correct |
55 |
Correct |
10 ms |
10964 KB |
Output is correct |
56 |
Correct |
13 ms |
11148 KB |
Output is correct |
57 |
Correct |
56 ms |
16556 KB |
Output is correct |
58 |
Correct |
31 ms |
16484 KB |
Output is correct |
59 |
Correct |
74 ms |
23716 KB |
Output is correct |
60 |
Correct |
275 ms |
36236 KB |
Output is correct |
61 |
Correct |
118 ms |
20740 KB |
Output is correct |
62 |
Correct |
116 ms |
21456 KB |
Output is correct |
63 |
Correct |
152 ms |
21500 KB |
Output is correct |
64 |
Correct |
288 ms |
22336 KB |
Output is correct |
65 |
Correct |
145 ms |
20428 KB |
Output is correct |
66 |
Correct |
253 ms |
30964 KB |
Output is correct |
67 |
Correct |
82 ms |
22360 KB |
Output is correct |
68 |
Correct |
150 ms |
21204 KB |
Output is correct |
69 |
Correct |
170 ms |
21244 KB |
Output is correct |
70 |
Correct |
146 ms |
20808 KB |
Output is correct |