#include "stations.h"
#include <vector>
void rec(int index, int parent, int pre_order, int &number,
std::vector<std::vector<int>>& con, std::vector<int>& labels) {
if (pre_order) labels[index] = number++;
for (int i : con[index]) {
if (i == parent) continue;
rec(i, index, !pre_order, number, con, labels);
}
if (!pre_order) labels[index] = number++;
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
std::vector<int> labels(n);
std::vector<std::vector<int>> con(n, std::vector<int>());
for (int i = 0; i < u.size(); i++) {
con[u[i]].push_back(v[i]);
con[v[i]].push_back(u[i]);
}
int number = 0;
rec(0, -1, true, number, con, labels);
return labels;
}
int find_next_station(int s, int t, std::vector<int> c) {
if (c[0] > s) {
// Case 1: node s is in pre_order level.
// All the neighbors are higher than s.
if (t < s) {
// The target is smaller than the source.
// The target is definitely not in this subtree, go to parent.
return c.back();
}
if (t > c.back()) {
// The target is higher than the largest neighbor.
// The target cannot be in this subtree, go to parent.
return c.back();
}
// The target is in this subtree.
// Pick the smallest child that's at least the target.
int next = 0;
while (c[next] < t) next++;
return c[next];
}
// Case 2: node s is in the post_order level.
if (t < c[0]) {
// The target is smaller than the pre_order root c[0],
// thus not in this subtree, go to the root.
return c[0];
}
if (t > s) {
// The target is higher than this post_order value.
// Thus it's not in this subtree, go to the root.
return c[0];
}
int next = c.size() - 1;
while (c[next] > t) next--;
return c[next];
}
Compilation message
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:17:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | for (int i = 0; i < u.size(); i++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
647 ms |
988 KB |
Output is correct |
2 |
Correct |
437 ms |
1008 KB |
Output is correct |
3 |
Correct |
1253 ms |
672 KB |
Output is correct |
4 |
Correct |
706 ms |
884 KB |
Output is correct |
5 |
Correct |
745 ms |
640 KB |
Output is correct |
6 |
Correct |
625 ms |
968 KB |
Output is correct |
7 |
Correct |
669 ms |
1056 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
4 ms |
904 KB |
Output is correct |
10 |
Correct |
0 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
595 ms |
760 KB |
Output is correct |
2 |
Correct |
600 ms |
744 KB |
Output is correct |
3 |
Correct |
1011 ms |
748 KB |
Output is correct |
4 |
Correct |
676 ms |
740 KB |
Output is correct |
5 |
Correct |
709 ms |
732 KB |
Output is correct |
6 |
Correct |
596 ms |
808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
667 ms |
1016 KB |
Output is correct |
2 |
Correct |
583 ms |
992 KB |
Output is correct |
3 |
Correct |
1196 ms |
776 KB |
Output is correct |
4 |
Correct |
802 ms |
896 KB |
Output is correct |
5 |
Correct |
724 ms |
892 KB |
Output is correct |
6 |
Correct |
597 ms |
1024 KB |
Output is correct |
7 |
Correct |
485 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
800 KB |
Output is correct |
9 |
Correct |
4 ms |
768 KB |
Output is correct |
10 |
Correct |
1 ms |
776 KB |
Output is correct |
11 |
Correct |
781 ms |
768 KB |
Output is correct |
12 |
Correct |
538 ms |
1024 KB |
Output is correct |
13 |
Correct |
502 ms |
1024 KB |
Output is correct |
14 |
Correct |
527 ms |
768 KB |
Output is correct |
15 |
Correct |
73 ms |
744 KB |
Output is correct |
16 |
Correct |
104 ms |
896 KB |
Output is correct |
17 |
Correct |
106 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1124 ms |
800 KB |
Output is correct |
2 |
Correct |
692 ms |
768 KB |
Output is correct |
3 |
Correct |
760 ms |
640 KB |
Output is correct |
4 |
Correct |
3 ms |
672 KB |
Output is correct |
5 |
Correct |
4 ms |
932 KB |
Output is correct |
6 |
Correct |
2 ms |
816 KB |
Output is correct |
7 |
Correct |
845 ms |
640 KB |
Output is correct |
8 |
Correct |
1075 ms |
768 KB |
Output is correct |
9 |
Correct |
840 ms |
640 KB |
Output is correct |
10 |
Correct |
876 ms |
640 KB |
Output is correct |
11 |
Correct |
6 ms |
776 KB |
Output is correct |
12 |
Correct |
6 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
2 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
533 ms |
768 KB |
Output is correct |
17 |
Correct |
592 ms |
788 KB |
Output is correct |
18 |
Correct |
699 ms |
640 KB |
Output is correct |
19 |
Correct |
968 ms |
904 KB |
Output is correct |
20 |
Correct |
720 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
682 ms |
968 KB |
Output is correct |
2 |
Correct |
533 ms |
1016 KB |
Output is correct |
3 |
Correct |
1059 ms |
776 KB |
Output is correct |
4 |
Correct |
806 ms |
808 KB |
Output is correct |
5 |
Correct |
883 ms |
864 KB |
Output is correct |
6 |
Correct |
584 ms |
1024 KB |
Output is correct |
7 |
Correct |
605 ms |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
776 KB |
Output is correct |
9 |
Correct |
4 ms |
680 KB |
Output is correct |
10 |
Correct |
0 ms |
640 KB |
Output is correct |
11 |
Correct |
533 ms |
776 KB |
Output is correct |
12 |
Correct |
592 ms |
836 KB |
Output is correct |
13 |
Correct |
1109 ms |
860 KB |
Output is correct |
14 |
Correct |
795 ms |
640 KB |
Output is correct |
15 |
Correct |
923 ms |
768 KB |
Output is correct |
16 |
Correct |
555 ms |
760 KB |
Output is correct |
17 |
Correct |
771 ms |
768 KB |
Output is correct |
18 |
Correct |
738 ms |
1016 KB |
Output is correct |
19 |
Correct |
688 ms |
1008 KB |
Output is correct |
20 |
Correct |
474 ms |
916 KB |
Output is correct |
21 |
Correct |
76 ms |
640 KB |
Output is correct |
22 |
Correct |
104 ms |
916 KB |
Output is correct |
23 |
Correct |
140 ms |
904 KB |
Output is correct |
24 |
Correct |
9 ms |
640 KB |
Output is correct |
25 |
Correct |
11 ms |
904 KB |
Output is correct |
26 |
Correct |
7 ms |
928 KB |
Output is correct |
27 |
Correct |
5 ms |
904 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
555 ms |
800 KB |
Output is correct |
30 |
Correct |
557 ms |
640 KB |
Output is correct |
31 |
Correct |
641 ms |
732 KB |
Output is correct |
32 |
Correct |
555 ms |
904 KB |
Output is correct |
33 |
Correct |
541 ms |
640 KB |
Output is correct |
34 |
Correct |
450 ms |
768 KB |
Output is correct |
35 |
Correct |
456 ms |
1024 KB |
Output is correct |
36 |
Correct |
614 ms |
1024 KB |
Output is correct |
37 |
Correct |
521 ms |
736 KB |
Output is correct |
38 |
Correct |
575 ms |
1016 KB |
Output is correct |
39 |
Correct |
574 ms |
764 KB |
Output is correct |
40 |
Correct |
551 ms |
760 KB |
Output is correct |
41 |
Correct |
487 ms |
920 KB |
Output is correct |
42 |
Correct |
81 ms |
896 KB |
Output is correct |
43 |
Correct |
186 ms |
904 KB |
Output is correct |
44 |
Correct |
153 ms |
788 KB |
Output is correct |
45 |
Correct |
280 ms |
776 KB |
Output is correct |
46 |
Correct |
300 ms |
808 KB |
Output is correct |
47 |
Correct |
396 ms |
1056 KB |
Output is correct |
48 |
Correct |
78 ms |
844 KB |
Output is correct |
49 |
Correct |
79 ms |
748 KB |
Output is correct |