#include "stations.h"
#include <bits/stdc++.h>
#define N 1005
using namespace std;
vector<int> tree[N];
int dfs(int nd, int par, int cur, bool pre, vector<int> &labels) {
if (pre) labels[nd] = cur++;
for (int child : tree[nd]) {
if (child == par) continue;
cur = dfs(child, nd, cur, !pre, labels);
}
if (!pre) labels[nd] = cur++;
return cur;
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
for (int i=0; i<n; i++) tree[i].clear();
for (int i=0; i<u.size(); i++) {
tree[u[i]].push_back(v[i]);
tree[v[i]].push_back(u[i]);
}
vector<int> labels(n);
dfs(0, -1, 0, 0, labels);
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
sort(c.begin(), c.end());
int sz = c.size();
if (s == t) return s;
if (sz == 1) return c[0];
if (s < c[0]) { // s is preorder
if (t < s || t > c[sz-2]) return c[sz-1]; // parent
for (int i=0; i<sz; i++) {
if (t <= c[i]) return c[i];
}
} else { // s is postorder
if (t < c[1] || t > s) return c[0]; // parent
for (int i=2; i<sz; i++) {
if (t < c[i]) return c[i-1];
}
return c[sz-1];
}
return -1337; // it should not come here
}
Compilation message
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i=0; i<u.size(); i++) {
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
487 ms |
1096 KB |
Output is correct |
2 |
Correct |
470 ms |
992 KB |
Output is correct |
3 |
Correct |
932 ms |
952 KB |
Output is correct |
4 |
Correct |
806 ms |
952 KB |
Output is correct |
5 |
Correct |
601 ms |
864 KB |
Output is correct |
6 |
Correct |
484 ms |
1100 KB |
Output is correct |
7 |
Correct |
472 ms |
1108 KB |
Output is correct |
8 |
Correct |
3 ms |
952 KB |
Output is correct |
9 |
Correct |
6 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
473 ms |
864 KB |
Output is correct |
2 |
Correct |
593 ms |
864 KB |
Output is correct |
3 |
Correct |
955 ms |
736 KB |
Output is correct |
4 |
Correct |
654 ms |
952 KB |
Output is correct |
5 |
Correct |
574 ms |
992 KB |
Output is correct |
6 |
Correct |
494 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
577 ms |
992 KB |
Output is correct |
2 |
Correct |
476 ms |
1076 KB |
Output is correct |
3 |
Correct |
989 ms |
864 KB |
Output is correct |
4 |
Correct |
768 ms |
992 KB |
Output is correct |
5 |
Correct |
699 ms |
864 KB |
Output is correct |
6 |
Correct |
420 ms |
864 KB |
Output is correct |
7 |
Correct |
536 ms |
864 KB |
Output is correct |
8 |
Correct |
3 ms |
992 KB |
Output is correct |
9 |
Correct |
5 ms |
736 KB |
Output is correct |
10 |
Correct |
2 ms |
736 KB |
Output is correct |
11 |
Correct |
577 ms |
736 KB |
Output is correct |
12 |
Correct |
441 ms |
1248 KB |
Output is correct |
13 |
Correct |
460 ms |
1120 KB |
Output is correct |
14 |
Correct |
433 ms |
864 KB |
Output is correct |
15 |
Correct |
55 ms |
952 KB |
Output is correct |
16 |
Correct |
74 ms |
736 KB |
Output is correct |
17 |
Correct |
110 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
896 ms |
864 KB |
Output is correct |
2 |
Correct |
656 ms |
864 KB |
Output is correct |
3 |
Correct |
609 ms |
736 KB |
Output is correct |
4 |
Correct |
2 ms |
952 KB |
Output is correct |
5 |
Correct |
4 ms |
960 KB |
Output is correct |
6 |
Correct |
1 ms |
736 KB |
Output is correct |
7 |
Correct |
793 ms |
992 KB |
Output is correct |
8 |
Correct |
995 ms |
864 KB |
Output is correct |
9 |
Correct |
740 ms |
736 KB |
Output is correct |
10 |
Correct |
545 ms |
736 KB |
Output is correct |
11 |
Correct |
5 ms |
844 KB |
Output is correct |
12 |
Correct |
7 ms |
864 KB |
Output is correct |
13 |
Correct |
6 ms |
736 KB |
Output is correct |
14 |
Correct |
3 ms |
960 KB |
Output is correct |
15 |
Correct |
2 ms |
736 KB |
Output is correct |
16 |
Correct |
470 ms |
736 KB |
Output is correct |
17 |
Correct |
528 ms |
992 KB |
Output is correct |
18 |
Correct |
660 ms |
1088 KB |
Output is correct |
19 |
Correct |
631 ms |
864 KB |
Output is correct |
20 |
Correct |
537 ms |
864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
677 ms |
1020 KB |
Output is correct |
2 |
Correct |
413 ms |
864 KB |
Output is correct |
3 |
Correct |
853 ms |
1080 KB |
Output is correct |
4 |
Correct |
642 ms |
736 KB |
Output is correct |
5 |
Correct |
575 ms |
864 KB |
Output is correct |
6 |
Correct |
427 ms |
864 KB |
Output is correct |
7 |
Correct |
527 ms |
864 KB |
Output is correct |
8 |
Correct |
3 ms |
864 KB |
Output is correct |
9 |
Correct |
5 ms |
736 KB |
Output is correct |
10 |
Correct |
2 ms |
864 KB |
Output is correct |
11 |
Correct |
527 ms |
1020 KB |
Output is correct |
12 |
Correct |
519 ms |
936 KB |
Output is correct |
13 |
Correct |
991 ms |
1120 KB |
Output is correct |
14 |
Correct |
822 ms |
992 KB |
Output is correct |
15 |
Correct |
599 ms |
952 KB |
Output is correct |
16 |
Correct |
541 ms |
904 KB |
Output is correct |
17 |
Correct |
636 ms |
992 KB |
Output is correct |
18 |
Correct |
493 ms |
1104 KB |
Output is correct |
19 |
Correct |
570 ms |
1056 KB |
Output is correct |
20 |
Correct |
509 ms |
904 KB |
Output is correct |
21 |
Correct |
72 ms |
908 KB |
Output is correct |
22 |
Correct |
73 ms |
936 KB |
Output is correct |
23 |
Correct |
108 ms |
864 KB |
Output is correct |
24 |
Correct |
7 ms |
736 KB |
Output is correct |
25 |
Correct |
7 ms |
864 KB |
Output is correct |
26 |
Correct |
5 ms |
736 KB |
Output is correct |
27 |
Correct |
4 ms |
736 KB |
Output is correct |
28 |
Correct |
2 ms |
960 KB |
Output is correct |
29 |
Correct |
661 ms |
1108 KB |
Output is correct |
30 |
Correct |
536 ms |
952 KB |
Output is correct |
31 |
Correct |
558 ms |
864 KB |
Output is correct |
32 |
Correct |
488 ms |
952 KB |
Output is correct |
33 |
Correct |
665 ms |
1016 KB |
Output is correct |
34 |
Correct |
418 ms |
1096 KB |
Output is correct |
35 |
Correct |
417 ms |
1000 KB |
Output is correct |
36 |
Correct |
504 ms |
1120 KB |
Output is correct |
37 |
Correct |
508 ms |
1112 KB |
Output is correct |
38 |
Correct |
435 ms |
1112 KB |
Output is correct |
39 |
Correct |
484 ms |
1352 KB |
Output is correct |
40 |
Correct |
477 ms |
1064 KB |
Output is correct |
41 |
Correct |
555 ms |
1124 KB |
Output is correct |
42 |
Correct |
65 ms |
908 KB |
Output is correct |
43 |
Correct |
118 ms |
736 KB |
Output is correct |
44 |
Correct |
152 ms |
880 KB |
Output is correct |
45 |
Correct |
159 ms |
876 KB |
Output is correct |
46 |
Correct |
332 ms |
1032 KB |
Output is correct |
47 |
Correct |
347 ms |
992 KB |
Output is correct |
48 |
Correct |
64 ms |
1096 KB |
Output is correct |
49 |
Correct |
54 ms |
864 KB |
Output is correct |