#include<bits/stdc++.h>
#include "race.h"
#define mk make_pair
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define f first
#define s second
using namespace std ;
const int inf = 1e9 + 5 ;
const int maxn = 2e5 + 5 ;
const int MAX = 1e6 + 5 ;
int ans = inf ;
int freq[MAX], k, sz[maxn], mark[maxn] ;
vector<pii> grafo[maxn] ;
void dfs(int v, int p){
sz[v] = 1 ;
for(pii a : grafo[v]){
if(mark[a.f] || a.f == p) continue ;
dfs(a.f, v) ;
sz[v] += sz[a.f] ;
}
}
int centroid(int u, int p, int qtd){
for(pii a : grafo[u]){
if(a.f == p || mark[a.f] || sz[a.f]*2 <= qtd) continue ;
return centroid(a.f, u, qtd) ;
}
return u ;
}
void add(int u, int p, int v, int dist, int len){
if(dist > k) return ;
if(v == -1) freq[dist] = inf ; //voltar p fzr p prox cent
else freq[dist] = min(freq[dist], len) ; //unir c a outra sub
for(pii a : grafo[u]){
if(a.f == p || mark[a.f]) continue ;
add(a.f, u, v, dist + a.s, len + 1) ;
}
}
void solve(int u, int p, int dist, int len){
if(dist > k) return ; //n tem como ter tam k
ans = min(ans, freq[k-dist] + len) ; // quais caras eu uso com tam k -> usamos len p ter dist -> queremos minimizar
for(pii a : grafo[u]){
if(a.f == p || mark[a.f]) continue ;
solve(a.f, u, dist + a.s, len+1) ;
}
}
void make_a(int v, int p){
dfs(v, p) ;
int c = centroid(v, p, sz[v]) ;
mark[c] = 1 ;
freq[0] = 0 ;
for(pii a : grafo[c]){
if(mark[a.f] || a.f == p) continue ;
solve(a.f, c, a.s, 1) ; //resolver p minha
add(a.f, c, 1, a.s, 1) ; //add a minha p fzr c proxs
}
for(pii a : grafo[c]){
if(mark[a.f] || a.f == p) continue ;
add(a.f, c, -1, a.s, 1) ; //retirar tds p fzr p prox
}
for(pii a : grafo[c]) if(!(a.f == p || mark[a.f])) make_a(a.f, c) ; //prox cent
}
int best_path(int N, int K, int H[maxn][2], int L[maxn]){
for(int i = 0 ; i < N - 1 ; i++){
grafo[H[i][0]].pb({H[i][1], L[i]}) ;
grafo[H[i][1]].pb({H[i][0], L[i]}) ;
}
k = K ;
for(int i = 1 ; i <= k ; i++) freq[i] = inf ;
make_a(0, -1) ;
return (ans != inf ? ans : -1) ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4976 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5008 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4976 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5008 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
5 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
5 ms |
7756 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
6 ms |
8676 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
6 ms |
6968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4976 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5008 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
157 ms |
12044 KB |
Output is correct |
20 |
Correct |
152 ms |
11880 KB |
Output is correct |
21 |
Correct |
182 ms |
11992 KB |
Output is correct |
22 |
Correct |
173 ms |
12084 KB |
Output is correct |
23 |
Correct |
87 ms |
12228 KB |
Output is correct |
24 |
Correct |
64 ms |
12120 KB |
Output is correct |
25 |
Correct |
146 ms |
15560 KB |
Output is correct |
26 |
Correct |
113 ms |
19204 KB |
Output is correct |
27 |
Correct |
191 ms |
19272 KB |
Output is correct |
28 |
Correct |
300 ms |
33952 KB |
Output is correct |
29 |
Correct |
293 ms |
32836 KB |
Output is correct |
30 |
Correct |
197 ms |
19292 KB |
Output is correct |
31 |
Correct |
196 ms |
19372 KB |
Output is correct |
32 |
Correct |
243 ms |
19416 KB |
Output is correct |
33 |
Correct |
245 ms |
18348 KB |
Output is correct |
34 |
Correct |
224 ms |
19048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4976 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
5028 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5008 KB |
Output is correct |
14 |
Correct |
3 ms |
5008 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
8652 KB |
Output is correct |
23 |
Correct |
5 ms |
8012 KB |
Output is correct |
24 |
Correct |
6 ms |
8396 KB |
Output is correct |
25 |
Correct |
6 ms |
8268 KB |
Output is correct |
26 |
Correct |
5 ms |
6348 KB |
Output is correct |
27 |
Correct |
5 ms |
8140 KB |
Output is correct |
28 |
Correct |
4 ms |
5836 KB |
Output is correct |
29 |
Correct |
4 ms |
6348 KB |
Output is correct |
30 |
Correct |
5 ms |
6476 KB |
Output is correct |
31 |
Correct |
5 ms |
7628 KB |
Output is correct |
32 |
Correct |
5 ms |
7756 KB |
Output is correct |
33 |
Correct |
6 ms |
8140 KB |
Output is correct |
34 |
Correct |
5 ms |
7372 KB |
Output is correct |
35 |
Correct |
6 ms |
8268 KB |
Output is correct |
36 |
Correct |
6 ms |
8676 KB |
Output is correct |
37 |
Correct |
6 ms |
8140 KB |
Output is correct |
38 |
Correct |
6 ms |
6968 KB |
Output is correct |
39 |
Correct |
157 ms |
12044 KB |
Output is correct |
40 |
Correct |
152 ms |
11880 KB |
Output is correct |
41 |
Correct |
182 ms |
11992 KB |
Output is correct |
42 |
Correct |
173 ms |
12084 KB |
Output is correct |
43 |
Correct |
87 ms |
12228 KB |
Output is correct |
44 |
Correct |
64 ms |
12120 KB |
Output is correct |
45 |
Correct |
146 ms |
15560 KB |
Output is correct |
46 |
Correct |
113 ms |
19204 KB |
Output is correct |
47 |
Correct |
191 ms |
19272 KB |
Output is correct |
48 |
Correct |
300 ms |
33952 KB |
Output is correct |
49 |
Correct |
293 ms |
32836 KB |
Output is correct |
50 |
Correct |
197 ms |
19292 KB |
Output is correct |
51 |
Correct |
196 ms |
19372 KB |
Output is correct |
52 |
Correct |
243 ms |
19416 KB |
Output is correct |
53 |
Correct |
245 ms |
18348 KB |
Output is correct |
54 |
Correct |
224 ms |
19048 KB |
Output is correct |
55 |
Correct |
12 ms |
5656 KB |
Output is correct |
56 |
Correct |
14 ms |
5692 KB |
Output is correct |
57 |
Correct |
94 ms |
12184 KB |
Output is correct |
58 |
Correct |
43 ms |
12052 KB |
Output is correct |
59 |
Correct |
112 ms |
19892 KB |
Output is correct |
60 |
Correct |
566 ms |
36932 KB |
Output is correct |
61 |
Correct |
214 ms |
19524 KB |
Output is correct |
62 |
Correct |
235 ms |
23344 KB |
Output is correct |
63 |
Correct |
338 ms |
23372 KB |
Output is correct |
64 |
Correct |
681 ms |
21832 KB |
Output is correct |
65 |
Correct |
255 ms |
20204 KB |
Output is correct |
66 |
Correct |
471 ms |
34244 KB |
Output is correct |
67 |
Correct |
138 ms |
23916 KB |
Output is correct |
68 |
Correct |
330 ms |
24096 KB |
Output is correct |
69 |
Correct |
331 ms |
24388 KB |
Output is correct |
70 |
Correct |
306 ms |
23428 KB |
Output is correct |