#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for(int i = x; i < y; ++i)
#define pb push_back
int ANS;
int K;
struct node{
set<pair<int, int>> e;
map<int, int> m;
int c = 0;
};
vector<node> g;
int cdfs(int start, int par, int tot){
for(auto nex: g[start].e){
if (nex.first != par and g[nex.first].c > tot/2){
return cdfs(nex.first, start, tot);
}
}
return start;
}
int countdfs(int start, int par){
g[start].c = 1;
for(auto nex: g[start].e){
if (nex.first != par){
g[start].c += countdfs(nex.first, start);
}
}
return g[start].c;
}
int centralise(int start){
// cout << "Finding centre of " << start << "\n";
countdfs(start, - 1);
return cdfs(start, -1, g[start].c);
}
void dfs2(int start, int par, int root, int length, int depth){
if (g[root].m.count(K - length) != 0){
ANS = min(ANS, depth + g[root].m[K - length]);
// if (depth + g[root].m[K - length] == 1){
// cout << start << " " << root << " " << depth << "\n";
// }
}
for(auto nex: g[start].e){
if (nex.first != par){
dfs2(nex.first, start, root, length + nex.second, depth + 1);
}
}
}
void dfs1(int start, int par, int root, int length, int depth){
if (g[root].m.count(length) == 0){
g[root].m[length] = depth;
}
// cout << "adding " << length << " " << depth << " " << root << "\n";
g[root].m[length] = min(g[root].m[length],depth);
// cout << "g[root].m[length] = " << g[root].m[length] << "\n";
for(auto nex: g[start].e){
if (nex.first != par){
dfs1(nex.first, start, root, length + nex.second, depth + 1);
}
}
}
void process(int start){
g[start].m[0] = 0;
//assumes start is a centroid
// cout << "processing: " << start << "\n";
vector<int> do_later;
for(auto edge: g[start].e){
g[edge.first].e.erase({start, edge.second});
dfs2(edge.first, start, start, edge.second, 1);
dfs1(edge.first, start, start, edge.second, 1);
// cout << "ANS = " << ANS << "\n";
// cout << "Looking into " << edge.first << "\n";
process(centralise(edge.first));
}
// cout << "Finished: " << start << "\n";
}
int best_path(int N, int k, int H[][2], int L[]){
g.resize(N);
K = k;
ANS = N+1;
FOR(i, 0, N - 1){
g[H[i][0]].e.insert({H[i][1], L[i]});
g[H[i][1]].e.insert({H[i][0], L[i]});
}
process(centralise(0));
if (ANS == N + 1){
ANS = -1;
}
return ANS;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
768 KB |
Output is correct |
22 |
Correct |
4 ms |
768 KB |
Output is correct |
23 |
Correct |
4 ms |
768 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
768 KB |
Output is correct |
29 |
Correct |
4 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
768 KB |
Output is correct |
32 |
Correct |
8 ms |
768 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
5 ms |
896 KB |
Output is correct |
35 |
Correct |
4 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
3 ms |
768 KB |
Output is correct |
38 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
530 ms |
35800 KB |
Output is correct |
20 |
Correct |
582 ms |
35832 KB |
Output is correct |
21 |
Correct |
553 ms |
34980 KB |
Output is correct |
22 |
Correct |
529 ms |
33912 KB |
Output is correct |
23 |
Correct |
684 ms |
54392 KB |
Output is correct |
24 |
Correct |
338 ms |
33016 KB |
Output is correct |
25 |
Correct |
518 ms |
33148 KB |
Output is correct |
26 |
Correct |
214 ms |
28792 KB |
Output is correct |
27 |
Correct |
708 ms |
76724 KB |
Output is correct |
28 |
Correct |
2090 ms |
202556 KB |
Output is correct |
29 |
Correct |
2116 ms |
198904 KB |
Output is correct |
30 |
Correct |
607 ms |
76664 KB |
Output is correct |
31 |
Correct |
682 ms |
76864 KB |
Output is correct |
32 |
Correct |
847 ms |
76524 KB |
Output is correct |
33 |
Correct |
1170 ms |
84448 KB |
Output is correct |
34 |
Correct |
1848 ms |
165880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
768 KB |
Output is correct |
22 |
Correct |
4 ms |
768 KB |
Output is correct |
23 |
Correct |
4 ms |
768 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
768 KB |
Output is correct |
29 |
Correct |
4 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
768 KB |
Output is correct |
32 |
Correct |
8 ms |
768 KB |
Output is correct |
33 |
Correct |
4 ms |
768 KB |
Output is correct |
34 |
Correct |
5 ms |
896 KB |
Output is correct |
35 |
Correct |
4 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
3 ms |
768 KB |
Output is correct |
38 |
Correct |
3 ms |
768 KB |
Output is correct |
39 |
Correct |
530 ms |
35800 KB |
Output is correct |
40 |
Correct |
582 ms |
35832 KB |
Output is correct |
41 |
Correct |
553 ms |
34980 KB |
Output is correct |
42 |
Correct |
529 ms |
33912 KB |
Output is correct |
43 |
Correct |
684 ms |
54392 KB |
Output is correct |
44 |
Correct |
338 ms |
33016 KB |
Output is correct |
45 |
Correct |
518 ms |
33148 KB |
Output is correct |
46 |
Correct |
214 ms |
28792 KB |
Output is correct |
47 |
Correct |
708 ms |
76724 KB |
Output is correct |
48 |
Correct |
2090 ms |
202556 KB |
Output is correct |
49 |
Correct |
2116 ms |
198904 KB |
Output is correct |
50 |
Correct |
607 ms |
76664 KB |
Output is correct |
51 |
Correct |
682 ms |
76864 KB |
Output is correct |
52 |
Correct |
847 ms |
76524 KB |
Output is correct |
53 |
Correct |
1170 ms |
84448 KB |
Output is correct |
54 |
Correct |
1848 ms |
165880 KB |
Output is correct |
55 |
Correct |
43 ms |
5232 KB |
Output is correct |
56 |
Correct |
23 ms |
3328 KB |
Output is correct |
57 |
Correct |
279 ms |
32316 KB |
Output is correct |
58 |
Correct |
118 ms |
22136 KB |
Output is correct |
59 |
Correct |
588 ms |
81416 KB |
Output is correct |
60 |
Correct |
2190 ms |
192864 KB |
Output is correct |
61 |
Correct |
711 ms |
82168 KB |
Output is correct |
62 |
Correct |
592 ms |
76408 KB |
Output is correct |
63 |
Correct |
746 ms |
76496 KB |
Output is correct |
64 |
Correct |
1868 ms |
132540 KB |
Output is correct |
65 |
Correct |
1679 ms |
124484 KB |
Output is correct |
66 |
Correct |
2069 ms |
204052 KB |
Output is correct |
67 |
Correct |
449 ms |
45432 KB |
Output is correct |
68 |
Correct |
1075 ms |
86520 KB |
Output is correct |
69 |
Correct |
1089 ms |
87032 KB |
Output is correct |
70 |
Correct |
1002 ms |
82112 KB |
Output is correct |