#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
/*
Date: 2023/05/14 14:06
Problem Link:
Topic(s):
Reflection:
Solution Notes:
We want to determine for two nodes (A, B), if A is an ancestor of B.
Normally, an Euler tour can accomplish this.
To encode the in and out times into one single number, we would need somewhere around 1000^2 + 1000 ~= 10^6 numbers.
The next logical step is how to not encode the in and out times.
Consider not storing BOTH the in and out for each node. Since we are given the neighbors, let p be the parent and c be some children,
we can still determine if a node A within the subtree of p by checking the range [in[p], max(out[c])] .
Consider storing everything an even depth away from the root to be in, and odd to be out.
We know whether a node has in vs. out time by checking if it is even or odd.
Now, we can compress down to 2*N states.
We can reduce to N states by compressing the values, because only their relative sizes matter.
We can determine if a node has the in time or out time by determining if:
max(label of neighbor) > label[current] -> current has in time
max(label of neighbor) < label[current] -> current has out time
label[current] == 0 -> the root
*/
const int MAXN = 1000+5, INF = 1e9;
int n, in[MAXN], out[MAXN], dep[MAXN], timer = 0;
vector<int> arr[MAXN];
void dfs(int at, int p, int d){
dep[at] = d;
in[at] = timer++;
for (int u : arr[at]){
if (u != p){
dfs(u, at, d+1);
}
}
out[at] = timer++;
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
for (int i = 0; i<n; i++) arr[i].clear();
vector<int> labels(n);
for (int i=0; i<n-1; i++){
arr[u[i]].push_back(v[i]);
arr[v[i]].push_back(u[i]);
}
dfs(0, -1, 0);
vector<int> all_labels;
for (int i=0; i<n; i++){
if (dep[i] % 2 == 0){
all_labels.push_back(in[i]);
labels[i] = in[i];
}else{
all_labels.push_back(out[i]);
labels[i] = out[i];
}
}
sort(all(all_labels));
for (int i=0; i<n; i++){
labels[i] = lower_bound(all(all_labels), labels[i]) - all_labels.begin();
}
return labels;
}
int find_next_station(int s, int t, std::vector<int> c) {
if (sz(c) == 1) return c[0];
if (s < c[0]){
// s = in
for (int i=0; i<(s == 0 ? sz(c) : sz(c) - 1); i++){
if ((i == 0 ? s + 1 : c[i-1] + 1) <= t && c[i] >= t){
return c[i];
}
}
return c[sz(c)-1]; // go to parent
}else{
// s = out
for (int i=1; i<sz(c); i++){
if (t >= c[i] && t <= (i != sz(c) - 1 ? c[i+1]-1 : s - 1)){
return c[i];
}
}
return c[0];
}
}
// int main(){
// cin.tie(0); ios_base::sync_with_stdio(0);
// freopen("file.in", "r", stdin);
// // freopen("file.out", "w", stdout);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
541 ms |
684 KB |
Output is correct |
2 |
Correct |
445 ms |
536 KB |
Output is correct |
3 |
Correct |
899 ms |
528 KB |
Output is correct |
4 |
Correct |
624 ms |
528 KB |
Output is correct |
5 |
Correct |
598 ms |
528 KB |
Output is correct |
6 |
Correct |
495 ms |
520 KB |
Output is correct |
7 |
Correct |
527 ms |
532 KB |
Output is correct |
8 |
Correct |
3 ms |
628 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
0 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
532 KB |
Output is correct |
2 |
Correct |
576 ms |
572 KB |
Output is correct |
3 |
Correct |
951 ms |
420 KB |
Output is correct |
4 |
Correct |
698 ms |
528 KB |
Output is correct |
5 |
Correct |
571 ms |
524 KB |
Output is correct |
6 |
Correct |
418 ms |
548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
527 ms |
544 KB |
Output is correct |
2 |
Correct |
531 ms |
652 KB |
Output is correct |
3 |
Correct |
971 ms |
528 KB |
Output is correct |
4 |
Correct |
858 ms |
524 KB |
Output is correct |
5 |
Correct |
492 ms |
544 KB |
Output is correct |
6 |
Correct |
483 ms |
628 KB |
Output is correct |
7 |
Correct |
493 ms |
508 KB |
Output is correct |
8 |
Correct |
4 ms |
624 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
416 KB |
Output is correct |
11 |
Correct |
728 ms |
520 KB |
Output is correct |
12 |
Correct |
472 ms |
652 KB |
Output is correct |
13 |
Correct |
492 ms |
780 KB |
Output is correct |
14 |
Correct |
506 ms |
548 KB |
Output is correct |
15 |
Correct |
55 ms |
592 KB |
Output is correct |
16 |
Correct |
63 ms |
548 KB |
Output is correct |
17 |
Correct |
97 ms |
704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
822 ms |
548 KB |
Output is correct |
2 |
Correct |
707 ms |
536 KB |
Output is correct |
3 |
Correct |
591 ms |
532 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
4 ms |
496 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
490 ms |
536 KB |
Output is correct |
8 |
Correct |
907 ms |
516 KB |
Output is correct |
9 |
Correct |
595 ms |
532 KB |
Output is correct |
10 |
Correct |
539 ms |
528 KB |
Output is correct |
11 |
Correct |
5 ms |
628 KB |
Output is correct |
12 |
Correct |
6 ms |
628 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
4 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
632 KB |
Output is correct |
16 |
Correct |
471 ms |
432 KB |
Output is correct |
17 |
Correct |
522 ms |
420 KB |
Output is correct |
18 |
Correct |
615 ms |
420 KB |
Output is correct |
19 |
Correct |
512 ms |
544 KB |
Output is correct |
20 |
Correct |
497 ms |
548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
553 ms |
544 KB |
Output is correct |
2 |
Correct |
491 ms |
660 KB |
Output is correct |
3 |
Correct |
830 ms |
532 KB |
Output is correct |
4 |
Correct |
524 ms |
548 KB |
Output is correct |
5 |
Correct |
593 ms |
528 KB |
Output is correct |
6 |
Correct |
412 ms |
544 KB |
Output is correct |
7 |
Correct |
408 ms |
528 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
505 ms |
528 KB |
Output is correct |
12 |
Correct |
561 ms |
544 KB |
Output is correct |
13 |
Correct |
897 ms |
544 KB |
Output is correct |
14 |
Correct |
701 ms |
544 KB |
Output is correct |
15 |
Correct |
540 ms |
548 KB |
Output is correct |
16 |
Correct |
403 ms |
536 KB |
Output is correct |
17 |
Correct |
522 ms |
544 KB |
Output is correct |
18 |
Correct |
462 ms |
784 KB |
Output is correct |
19 |
Correct |
399 ms |
648 KB |
Output is correct |
20 |
Correct |
433 ms |
544 KB |
Output is correct |
21 |
Correct |
48 ms |
572 KB |
Output is correct |
22 |
Correct |
69 ms |
704 KB |
Output is correct |
23 |
Correct |
111 ms |
656 KB |
Output is correct |
24 |
Correct |
4 ms |
628 KB |
Output is correct |
25 |
Correct |
5 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
4 ms |
496 KB |
Output is correct |
28 |
Correct |
1 ms |
620 KB |
Output is correct |
29 |
Correct |
542 ms |
528 KB |
Output is correct |
30 |
Correct |
526 ms |
544 KB |
Output is correct |
31 |
Correct |
517 ms |
544 KB |
Output is correct |
32 |
Correct |
580 ms |
528 KB |
Output is correct |
33 |
Correct |
488 ms |
420 KB |
Output is correct |
34 |
Correct |
325 ms |
636 KB |
Output is correct |
35 |
Correct |
372 ms |
672 KB |
Output is correct |
36 |
Correct |
474 ms |
756 KB |
Output is correct |
37 |
Correct |
460 ms |
636 KB |
Output is correct |
38 |
Correct |
480 ms |
656 KB |
Output is correct |
39 |
Correct |
438 ms |
884 KB |
Output is correct |
40 |
Correct |
469 ms |
668 KB |
Output is correct |
41 |
Correct |
390 ms |
660 KB |
Output is correct |
42 |
Correct |
52 ms |
620 KB |
Output is correct |
43 |
Correct |
114 ms |
676 KB |
Output is correct |
44 |
Correct |
133 ms |
548 KB |
Output is correct |
45 |
Correct |
168 ms |
548 KB |
Output is correct |
46 |
Correct |
298 ms |
528 KB |
Output is correct |
47 |
Correct |
346 ms |
528 KB |
Output is correct |
48 |
Correct |
52 ms |
772 KB |
Output is correct |
49 |
Correct |
62 ms |
676 KB |
Output is correct |