#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
using int64 = long long;
using ld = long double;
constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
template<typename T>
class CoordinateCompression {
vector<T> a;
public:
void add(const T x) {
a.push_back(x);
}
void add(const vector<T> &v) {
a.insert(a.end(), v.begin(), v.end());
}
vector<T> solve() {
vector<T> v = a;
sort(a.begin(), a.end());
for (int &x : v) {
x = distance(a.begin(), lower_bound(a.begin(), a.end(), x));
}
return v;
}
};
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
g[u[i]].push_back(v[i]);
g[v[i]].push_back(u[i]);
}
vector<int> depth(n);
vector<int> tin(n, -1), tout(n, -1);
function<int(int, int)> dfs = [&](int x, int p)-> int {
int time = tin[x] + 1;
for (const int y : g[x]) {
if (y == p) continue;
tin[y] = time;
depth[y] = 1 - depth[x];
time = dfs(y, x) + 1;
}
return tout[x] = time;
};
depth[0] = 0;
tin[0] = tout[0] = 0;
dfs(0, -1);
vector<int> labels(n);
for (int x = 0; x < n; x++) {
labels[x] = (depth[x] == 0 ? tin[x] : tout[x]);
}
CoordinateCompression<int> cc;
cc.add(labels);
return cc.solve();
}
int find_next_station(int s, int t, vector<int> c) {
if (c[0] > s) {
sort(c.begin(), c.end());
for (int i = 0; i < c.size() - 1; i++) {
if (s < t and t <= c[i]) return c[i];
}
return c.back();
} else {
sort(c.rbegin(), c.rend());
for (int i = 0; i < c.size() - 1; i++) {
if (c[i] <= t and t < s) return c[i];
}
return c.back();
}
}
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < c.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~
stations.cpp:83:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int i = 0; i < c.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
676 KB |
Output is correct |
2 |
Correct |
437 ms |
612 KB |
Output is correct |
3 |
Correct |
902 ms |
416 KB |
Output is correct |
4 |
Correct |
716 ms |
420 KB |
Output is correct |
5 |
Correct |
552 ms |
548 KB |
Output is correct |
6 |
Correct |
362 ms |
624 KB |
Output is correct |
7 |
Correct |
407 ms |
628 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
508 KB |
Output is correct |
2 |
Correct |
604 ms |
508 KB |
Output is correct |
3 |
Correct |
770 ms |
416 KB |
Output is correct |
4 |
Correct |
655 ms |
420 KB |
Output is correct |
5 |
Correct |
556 ms |
508 KB |
Output is correct |
6 |
Correct |
446 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
570 ms |
672 KB |
Output is correct |
2 |
Correct |
461 ms |
684 KB |
Output is correct |
3 |
Correct |
796 ms |
420 KB |
Output is correct |
4 |
Correct |
684 ms |
424 KB |
Output is correct |
5 |
Correct |
560 ms |
500 KB |
Output is correct |
6 |
Correct |
529 ms |
672 KB |
Output is correct |
7 |
Correct |
467 ms |
660 KB |
Output is correct |
8 |
Correct |
3 ms |
500 KB |
Output is correct |
9 |
Correct |
4 ms |
496 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
504 ms |
428 KB |
Output is correct |
12 |
Correct |
462 ms |
832 KB |
Output is correct |
13 |
Correct |
493 ms |
756 KB |
Output is correct |
14 |
Correct |
428 ms |
500 KB |
Output is correct |
15 |
Correct |
39 ms |
480 KB |
Output is correct |
16 |
Correct |
56 ms |
624 KB |
Output is correct |
17 |
Correct |
96 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
919 ms |
424 KB |
Output is correct |
2 |
Correct |
648 ms |
548 KB |
Output is correct |
3 |
Correct |
540 ms |
416 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
556 KB |
Output is correct |
7 |
Correct |
612 ms |
508 KB |
Output is correct |
8 |
Correct |
850 ms |
428 KB |
Output is correct |
9 |
Correct |
633 ms |
420 KB |
Output is correct |
10 |
Correct |
597 ms |
420 KB |
Output is correct |
11 |
Correct |
4 ms |
500 KB |
Output is correct |
12 |
Correct |
5 ms |
508 KB |
Output is correct |
13 |
Correct |
5 ms |
492 KB |
Output is correct |
14 |
Correct |
5 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
500 KB |
Output is correct |
16 |
Correct |
510 ms |
416 KB |
Output is correct |
17 |
Correct |
494 ms |
416 KB |
Output is correct |
18 |
Correct |
505 ms |
424 KB |
Output is correct |
19 |
Correct |
433 ms |
508 KB |
Output is correct |
20 |
Correct |
417 ms |
420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
567 ms |
676 KB |
Output is correct |
2 |
Correct |
487 ms |
676 KB |
Output is correct |
3 |
Correct |
809 ms |
484 KB |
Output is correct |
4 |
Correct |
627 ms |
416 KB |
Output is correct |
5 |
Correct |
603 ms |
416 KB |
Output is correct |
6 |
Correct |
501 ms |
628 KB |
Output is correct |
7 |
Correct |
433 ms |
544 KB |
Output is correct |
8 |
Correct |
2 ms |
500 KB |
Output is correct |
9 |
Correct |
4 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
479 ms |
492 KB |
Output is correct |
12 |
Correct |
500 ms |
512 KB |
Output is correct |
13 |
Correct |
767 ms |
416 KB |
Output is correct |
14 |
Correct |
681 ms |
484 KB |
Output is correct |
15 |
Correct |
442 ms |
416 KB |
Output is correct |
16 |
Correct |
439 ms |
544 KB |
Output is correct |
17 |
Correct |
556 ms |
504 KB |
Output is correct |
18 |
Correct |
397 ms |
712 KB |
Output is correct |
19 |
Correct |
501 ms |
756 KB |
Output is correct |
20 |
Correct |
466 ms |
548 KB |
Output is correct |
21 |
Correct |
63 ms |
432 KB |
Output is correct |
22 |
Correct |
76 ms |
600 KB |
Output is correct |
23 |
Correct |
101 ms |
552 KB |
Output is correct |
24 |
Correct |
5 ms |
492 KB |
Output is correct |
25 |
Correct |
5 ms |
492 KB |
Output is correct |
26 |
Correct |
4 ms |
496 KB |
Output is correct |
27 |
Correct |
3 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
531 ms |
500 KB |
Output is correct |
30 |
Correct |
468 ms |
416 KB |
Output is correct |
31 |
Correct |
411 ms |
420 KB |
Output is correct |
32 |
Correct |
497 ms |
508 KB |
Output is correct |
33 |
Correct |
494 ms |
556 KB |
Output is correct |
34 |
Correct |
323 ms |
672 KB |
Output is correct |
35 |
Correct |
407 ms |
624 KB |
Output is correct |
36 |
Correct |
403 ms |
672 KB |
Output is correct |
37 |
Correct |
419 ms |
504 KB |
Output is correct |
38 |
Correct |
459 ms |
588 KB |
Output is correct |
39 |
Correct |
471 ms |
664 KB |
Output is correct |
40 |
Correct |
448 ms |
600 KB |
Output is correct |
41 |
Correct |
484 ms |
636 KB |
Output is correct |
42 |
Correct |
57 ms |
572 KB |
Output is correct |
43 |
Correct |
92 ms |
668 KB |
Output is correct |
44 |
Correct |
132 ms |
544 KB |
Output is correct |
45 |
Correct |
153 ms |
616 KB |
Output is correct |
46 |
Correct |
280 ms |
544 KB |
Output is correct |
47 |
Correct |
292 ms |
632 KB |
Output is correct |
48 |
Correct |
48 ms |
548 KB |
Output is correct |
49 |
Correct |
51 ms |
664 KB |
Output is correct |