#include "race.h"
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
//#include "race.h"
const int dydis = 2e5 + 10;
vector<pair<int, int> > gr[dydis];
int n, k;
bool vis[dydis] = {};
int tevas[dydis];
vector<int> visi;
int sz[dydis];
int ans = inf;
void dfs(int v, int came){
tevas[v] = came;
visi.push_back(v);
sz[v] = 1;
for(auto x : gr[v]){
if(x.first == came) continue;
dfs(x.first, v);
sz[v] += sz[x.first];
}
}
int findCentroid(int v){
visi.clear();
dfs(v, v);
for(auto x : visi){
int mx = 0;
int sm = 0;
for(int i = 0; i < (int)gr[x].size(); i++){
if(gr[x][i].first == tevas[x]) continue;
mx = max(mx, sz[gr[x][i].first]);
sm += sz[gr[x][i].first];
}
mx = max(mx, (int)visi.size()-sm-1);
if(mx <= (int)visi.size()/2+1) return x;
}
exit(0);
return -1;
}
int curAns[1000010];
int known[1000010];
vector<int> stt;
void dfs1(int v, int came, long long dst, int kek){
if(dst > k) return;
stt.push_back(dst);
curAns[dst] = min(curAns[dst], kek);
for(auto x : gr[v]){
if(x.first == came) continue;
dfs1(x.first, v, dst + x.second, kek+1);
}
}
void jeiPrik(int v){ // suskaiciuojam, koks ats, jei v priklauso atsakymui
known[0] = 0;
vector<int> lik;
for(auto x : gr[v]){
stt.clear();
dfs1(x.first, v, x.second, 1);
for(auto y : stt){
// cout << "bandau " << y << ", tai bus galimai " << curAns[y] << " + " << known[k-y] << endl;
ans = min(ans, curAns[y] + known[k-y]);
}
for(auto y : stt){
known[y] = min(known[y], curAns[y]);
}
for(auto x : stt) curAns[x] = inf;
for(auto x : stt) lik.push_back(x);
}
for(auto x : lik){
curAns[x] = known[x] = inf;
}
}
void calc(int v){
v = findCentroid(v);
jeiPrik(v);
// cout << "gaunam, kad centras yra " << v << endl;
vector<int> kaims;
for(auto x : gr[v]) kaims.push_back(x.first);
for(auto x : visi){
vector<pair<int, int> > naujas;
for(auto y : gr[x]){
if(y.first == v) continue;
naujas.push_back(y);
}
gr[x] = naujas;
}
// cout << "istrynem " << v << endl;
for(auto x : kaims) calc(x);
}
int best_path(int N, int K, int H[][2], int L[]){
for(int i = 0; i <= 1000000; i++) curAns[i] = known[i] = inf;
n = N; k = K;
for(int i = 0; i < n-1; i++){
gr[H[i][0]].push_back({H[i][1], L[i]});
gr[H[i][1]].push_back({H[i][0], L[i]});
}
calc(0);
return (ans == inf ? -1ll : ans);
}
/*
4 3
0 1 1
1 2 2
1 3 4
2
5 5
0 1 3
1 2 2
2 3 1
3 4 5
1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12748 KB |
Output is correct |
3 |
Correct |
8 ms |
12748 KB |
Output is correct |
4 |
Correct |
9 ms |
12748 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12748 KB |
Output is correct |
7 |
Correct |
8 ms |
12856 KB |
Output is correct |
8 |
Correct |
9 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12732 KB |
Output is correct |
10 |
Correct |
9 ms |
12796 KB |
Output is correct |
11 |
Correct |
8 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12764 KB |
Output is correct |
13 |
Correct |
9 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12784 KB |
Output is correct |
15 |
Correct |
8 ms |
12748 KB |
Output is correct |
16 |
Correct |
10 ms |
12820 KB |
Output is correct |
17 |
Correct |
10 ms |
12856 KB |
Output is correct |
18 |
Correct |
8 ms |
12752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12748 KB |
Output is correct |
3 |
Correct |
8 ms |
12748 KB |
Output is correct |
4 |
Correct |
9 ms |
12748 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12748 KB |
Output is correct |
7 |
Correct |
8 ms |
12856 KB |
Output is correct |
8 |
Correct |
9 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12732 KB |
Output is correct |
10 |
Correct |
9 ms |
12796 KB |
Output is correct |
11 |
Correct |
8 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12764 KB |
Output is correct |
13 |
Correct |
9 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12784 KB |
Output is correct |
15 |
Correct |
8 ms |
12748 KB |
Output is correct |
16 |
Correct |
10 ms |
12820 KB |
Output is correct |
17 |
Correct |
10 ms |
12856 KB |
Output is correct |
18 |
Correct |
8 ms |
12752 KB |
Output is correct |
19 |
Correct |
9 ms |
12748 KB |
Output is correct |
20 |
Correct |
8 ms |
12848 KB |
Output is correct |
21 |
Correct |
9 ms |
12840 KB |
Output is correct |
22 |
Correct |
9 ms |
12876 KB |
Output is correct |
23 |
Correct |
10 ms |
12876 KB |
Output is correct |
24 |
Correct |
10 ms |
12864 KB |
Output is correct |
25 |
Correct |
10 ms |
12860 KB |
Output is correct |
26 |
Correct |
10 ms |
12876 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
28 |
Correct |
10 ms |
12876 KB |
Output is correct |
29 |
Correct |
12 ms |
12876 KB |
Output is correct |
30 |
Correct |
10 ms |
12876 KB |
Output is correct |
31 |
Correct |
10 ms |
12828 KB |
Output is correct |
32 |
Correct |
10 ms |
12832 KB |
Output is correct |
33 |
Correct |
11 ms |
12880 KB |
Output is correct |
34 |
Correct |
10 ms |
12876 KB |
Output is correct |
35 |
Correct |
10 ms |
12832 KB |
Output is correct |
36 |
Correct |
10 ms |
12876 KB |
Output is correct |
37 |
Correct |
9 ms |
12876 KB |
Output is correct |
38 |
Correct |
10 ms |
12876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12748 KB |
Output is correct |
3 |
Correct |
8 ms |
12748 KB |
Output is correct |
4 |
Correct |
9 ms |
12748 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12748 KB |
Output is correct |
7 |
Correct |
8 ms |
12856 KB |
Output is correct |
8 |
Correct |
9 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12732 KB |
Output is correct |
10 |
Correct |
9 ms |
12796 KB |
Output is correct |
11 |
Correct |
8 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12764 KB |
Output is correct |
13 |
Correct |
9 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12784 KB |
Output is correct |
15 |
Correct |
8 ms |
12748 KB |
Output is correct |
16 |
Correct |
10 ms |
12820 KB |
Output is correct |
17 |
Correct |
10 ms |
12856 KB |
Output is correct |
18 |
Correct |
8 ms |
12752 KB |
Output is correct |
19 |
Correct |
252 ms |
19100 KB |
Output is correct |
20 |
Correct |
298 ms |
19012 KB |
Output is correct |
21 |
Correct |
249 ms |
19384 KB |
Output is correct |
22 |
Correct |
299 ms |
19796 KB |
Output is correct |
23 |
Correct |
222 ms |
19348 KB |
Output is correct |
24 |
Correct |
119 ms |
19124 KB |
Output is correct |
25 |
Correct |
262 ms |
21660 KB |
Output is correct |
26 |
Correct |
226 ms |
25664 KB |
Output is correct |
27 |
Correct |
295 ms |
25428 KB |
Output is correct |
28 |
Correct |
584 ms |
36280 KB |
Output is correct |
29 |
Correct |
657 ms |
35512 KB |
Output is correct |
30 |
Correct |
296 ms |
25284 KB |
Output is correct |
31 |
Correct |
330 ms |
25236 KB |
Output is correct |
32 |
Correct |
354 ms |
25460 KB |
Output is correct |
33 |
Correct |
567 ms |
24232 KB |
Output is correct |
34 |
Correct |
448 ms |
24184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12748 KB |
Output is correct |
3 |
Correct |
8 ms |
12748 KB |
Output is correct |
4 |
Correct |
9 ms |
12748 KB |
Output is correct |
5 |
Correct |
8 ms |
12748 KB |
Output is correct |
6 |
Correct |
8 ms |
12748 KB |
Output is correct |
7 |
Correct |
8 ms |
12856 KB |
Output is correct |
8 |
Correct |
9 ms |
12748 KB |
Output is correct |
9 |
Correct |
8 ms |
12732 KB |
Output is correct |
10 |
Correct |
9 ms |
12796 KB |
Output is correct |
11 |
Correct |
8 ms |
12748 KB |
Output is correct |
12 |
Correct |
8 ms |
12764 KB |
Output is correct |
13 |
Correct |
9 ms |
12748 KB |
Output is correct |
14 |
Correct |
8 ms |
12784 KB |
Output is correct |
15 |
Correct |
8 ms |
12748 KB |
Output is correct |
16 |
Correct |
10 ms |
12820 KB |
Output is correct |
17 |
Correct |
10 ms |
12856 KB |
Output is correct |
18 |
Correct |
8 ms |
12752 KB |
Output is correct |
19 |
Correct |
9 ms |
12748 KB |
Output is correct |
20 |
Correct |
8 ms |
12848 KB |
Output is correct |
21 |
Correct |
9 ms |
12840 KB |
Output is correct |
22 |
Correct |
9 ms |
12876 KB |
Output is correct |
23 |
Correct |
10 ms |
12876 KB |
Output is correct |
24 |
Correct |
10 ms |
12864 KB |
Output is correct |
25 |
Correct |
10 ms |
12860 KB |
Output is correct |
26 |
Correct |
10 ms |
12876 KB |
Output is correct |
27 |
Correct |
9 ms |
12876 KB |
Output is correct |
28 |
Correct |
10 ms |
12876 KB |
Output is correct |
29 |
Correct |
12 ms |
12876 KB |
Output is correct |
30 |
Correct |
10 ms |
12876 KB |
Output is correct |
31 |
Correct |
10 ms |
12828 KB |
Output is correct |
32 |
Correct |
10 ms |
12832 KB |
Output is correct |
33 |
Correct |
11 ms |
12880 KB |
Output is correct |
34 |
Correct |
10 ms |
12876 KB |
Output is correct |
35 |
Correct |
10 ms |
12832 KB |
Output is correct |
36 |
Correct |
10 ms |
12876 KB |
Output is correct |
37 |
Correct |
9 ms |
12876 KB |
Output is correct |
38 |
Correct |
10 ms |
12876 KB |
Output is correct |
39 |
Correct |
252 ms |
19100 KB |
Output is correct |
40 |
Correct |
298 ms |
19012 KB |
Output is correct |
41 |
Correct |
249 ms |
19384 KB |
Output is correct |
42 |
Correct |
299 ms |
19796 KB |
Output is correct |
43 |
Correct |
222 ms |
19348 KB |
Output is correct |
44 |
Correct |
119 ms |
19124 KB |
Output is correct |
45 |
Correct |
262 ms |
21660 KB |
Output is correct |
46 |
Correct |
226 ms |
25664 KB |
Output is correct |
47 |
Correct |
295 ms |
25428 KB |
Output is correct |
48 |
Correct |
584 ms |
36280 KB |
Output is correct |
49 |
Correct |
657 ms |
35512 KB |
Output is correct |
50 |
Correct |
296 ms |
25284 KB |
Output is correct |
51 |
Correct |
330 ms |
25236 KB |
Output is correct |
52 |
Correct |
354 ms |
25460 KB |
Output is correct |
53 |
Correct |
567 ms |
24232 KB |
Output is correct |
54 |
Correct |
448 ms |
24184 KB |
Output is correct |
55 |
Correct |
23 ms |
13528 KB |
Output is correct |
56 |
Correct |
26 ms |
13516 KB |
Output is correct |
57 |
Correct |
223 ms |
20124 KB |
Output is correct |
58 |
Correct |
57 ms |
21508 KB |
Output is correct |
59 |
Correct |
231 ms |
25636 KB |
Output is correct |
60 |
Correct |
799 ms |
37844 KB |
Output is correct |
61 |
Correct |
310 ms |
25408 KB |
Output is correct |
62 |
Correct |
425 ms |
26588 KB |
Output is correct |
63 |
Correct |
405 ms |
26556 KB |
Output is correct |
64 |
Correct |
873 ms |
26908 KB |
Output is correct |
65 |
Correct |
573 ms |
25388 KB |
Output is correct |
66 |
Correct |
776 ms |
33260 KB |
Output is correct |
67 |
Correct |
155 ms |
30132 KB |
Output is correct |
68 |
Correct |
442 ms |
26028 KB |
Output is correct |
69 |
Correct |
423 ms |
26164 KB |
Output is correct |
70 |
Correct |
422 ms |
25520 KB |
Output is correct |