#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
const int lim = 1e5 + 5;
int subtree[lim];
bool vis[lim];
vector<int> edges[lim];
void get_subtree(int nd) {
vis[nd] = 1;
subtree[nd] = 1;
for(auto i : edges[nd]) {
if(!vis[i]) {
get_subtree(i);
subtree[nd] += subtree[i];
}
}
//cout << nd << " " << subtree[nd] << endl;
}
vector<int> ret;
void dfs(int nd, int l, int r, bool pos) {
vis[nd] = 1;
if(!pos) {
ret[nd] = l, ++l;
}
else {
ret[nd] = r, --r;
}
//cout << nd << " " << l << " " << r << " " << ret[nd] << endl;
for(auto i : edges[nd]) {
if(!vis[i]) {
dfs(i, l, l + subtree[i] - 1, pos ^ 1);
//assert(l + subtree[i] - 1 <= r);
l += subtree[i];
}
}
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
for(int i = 0; i < n; ++i)
edges[i].clear();
for(int i = 0; i < u.size(); ++i) {
edges[u[i]].push_back(v[i]);
edges[v[i]].push_back(u[i]);
}
memset(vis, 0, sizeof(vis));
memset(subtree, 0, sizeof(subtree));
get_subtree(0);
ret.resize(n, -1);
memset(vis, 0, sizeof(vis));
dfs(0, 0, n - 1, 0);
// alternate tiap depth
// ada yg node merupakan r ada yg node merupakan l
// jadi nanti bisa cari l/r di depth tiap node
// determine root?
// always set root to 0 and l
// depth 1 is set to right
// depth 2 is set to left and so on
// 0 child ke border kanan
// 1 child ke border kiri
// gimana klo border kiri/kanan conflict dengan child?
// childnya ngalah
// jika di l
// maka child dan par semua di kanan
// par otomatis merupakan yang paling besar labelnya
// jika di r
// maka child dan par semua di kiri
// par otomatis merupakan yang paling kecil
return ret;
}
int find_next_station(int s, int t, vector<int> c) {
// cek c nya
// kalo s 0 itu base case
// berarti nanti cari childnya
// kalo misal bukan, cek c nya itu lebih dari s atau semua kurang dari s
if(s == 0) {
// semua childnya r
for(int i = 0; i < c.size(); ++i) {
if(t <= c[i])
return c[i];
}
}
bool small = c[0] < s;
if(small) {
// if c < s berarti s ini r terus sisanya l
// yang paling kecil itu par
// childnya indicate left
vector<pair<int, pair<int, int>>> seg;
int pr = s - 1;
for(int i = c.size() - 1; i >= 1; --i) {
seg.push_back(make_pair(c[i], make_pair(c[i], pr)));
pr = c[i] - 1;
}
for(int i = 0; i < seg.size(); ++i) {
if(t >= seg[i].second.first && t <= seg[i].second.second)
return seg[i].first;
}
return c.front();
}
else {
// s ini l sisanya r
// yg par yang paling besar
vector<pair<int, pair<int, int>>> seg;
int pr = s + 1;
for(int i = 0; i < c.size() - 1; ++i) {
seg.push_back(make_pair(c[i], make_pair(pr, c[i])));
pr = c[i] + 1;
}
for(int i = 0; i < seg.size(); ++i) {
if(t >= seg[i].second.first && t <= seg[i].second.second)
return seg[i].first;
}
return c.back();
}
}
Compilation message
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:40:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < u.size(); ++i) {
| ~~^~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:77:20: 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(); ++i) {
| ~~^~~~~~~~~~
stations.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i = 0; i < seg.size(); ++i) {
| ~~^~~~~~~~~~~~
stations.cpp:104:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i = 0; i < c.size() - 1; ++i) {
| ~~^~~~~~~~~~~~~~
stations.cpp:108:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for(int i = 0; i < seg.size(); ++i) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
436 ms |
5892 KB |
Output is correct |
2 |
Correct |
447 ms |
5924 KB |
Output is correct |
3 |
Correct |
850 ms |
5668 KB |
Output is correct |
4 |
Correct |
509 ms |
5668 KB |
Output is correct |
5 |
Correct |
586 ms |
5672 KB |
Output is correct |
6 |
Correct |
496 ms |
5792 KB |
Output is correct |
7 |
Correct |
379 ms |
5788 KB |
Output is correct |
8 |
Correct |
4 ms |
5748 KB |
Output is correct |
9 |
Correct |
5 ms |
5744 KB |
Output is correct |
10 |
Correct |
4 ms |
5736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
466 ms |
5932 KB |
Output is correct |
2 |
Correct |
533 ms |
5804 KB |
Output is correct |
3 |
Correct |
830 ms |
5672 KB |
Output is correct |
4 |
Correct |
657 ms |
5664 KB |
Output is correct |
5 |
Correct |
579 ms |
5800 KB |
Output is correct |
6 |
Correct |
448 ms |
5796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
472 ms |
5920 KB |
Output is correct |
2 |
Correct |
468 ms |
5792 KB |
Output is correct |
3 |
Correct |
922 ms |
5796 KB |
Output is correct |
4 |
Correct |
640 ms |
5664 KB |
Output is correct |
5 |
Correct |
546 ms |
5668 KB |
Output is correct |
6 |
Correct |
441 ms |
5804 KB |
Output is correct |
7 |
Correct |
416 ms |
5796 KB |
Output is correct |
8 |
Correct |
5 ms |
5760 KB |
Output is correct |
9 |
Correct |
5 ms |
5748 KB |
Output is correct |
10 |
Correct |
4 ms |
5692 KB |
Output is correct |
11 |
Correct |
602 ms |
5644 KB |
Output is correct |
12 |
Correct |
506 ms |
5860 KB |
Output is correct |
13 |
Correct |
472 ms |
5784 KB |
Output is correct |
14 |
Correct |
371 ms |
5792 KB |
Output is correct |
15 |
Correct |
55 ms |
5764 KB |
Output is correct |
16 |
Correct |
59 ms |
5796 KB |
Output is correct |
17 |
Correct |
111 ms |
5800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
742 ms |
5668 KB |
Output is correct |
2 |
Correct |
670 ms |
5668 KB |
Output is correct |
3 |
Correct |
565 ms |
5676 KB |
Output is correct |
4 |
Correct |
5 ms |
5752 KB |
Output is correct |
5 |
Correct |
6 ms |
5748 KB |
Output is correct |
6 |
Correct |
4 ms |
5748 KB |
Output is correct |
7 |
Correct |
498 ms |
5676 KB |
Output is correct |
8 |
Correct |
879 ms |
5656 KB |
Output is correct |
9 |
Correct |
588 ms |
5652 KB |
Output is correct |
10 |
Correct |
582 ms |
5676 KB |
Output is correct |
11 |
Correct |
7 ms |
5748 KB |
Output is correct |
12 |
Correct |
8 ms |
5756 KB |
Output is correct |
13 |
Correct |
7 ms |
5756 KB |
Output is correct |
14 |
Correct |
7 ms |
5748 KB |
Output is correct |
15 |
Correct |
4 ms |
5748 KB |
Output is correct |
16 |
Correct |
514 ms |
5692 KB |
Output is correct |
17 |
Correct |
427 ms |
5736 KB |
Output is correct |
18 |
Correct |
509 ms |
5660 KB |
Output is correct |
19 |
Correct |
541 ms |
5664 KB |
Output is correct |
20 |
Correct |
473 ms |
5664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
467 ms |
5800 KB |
Output is correct |
2 |
Correct |
337 ms |
5796 KB |
Output is correct |
3 |
Correct |
799 ms |
5656 KB |
Output is correct |
4 |
Correct |
568 ms |
5652 KB |
Output is correct |
5 |
Correct |
451 ms |
5668 KB |
Output is correct |
6 |
Correct |
474 ms |
5796 KB |
Output is correct |
7 |
Correct |
399 ms |
5808 KB |
Output is correct |
8 |
Correct |
4 ms |
5756 KB |
Output is correct |
9 |
Correct |
6 ms |
5752 KB |
Output is correct |
10 |
Correct |
4 ms |
5744 KB |
Output is correct |
11 |
Correct |
456 ms |
5748 KB |
Output is correct |
12 |
Correct |
539 ms |
5820 KB |
Output is correct |
13 |
Correct |
790 ms |
5668 KB |
Output is correct |
14 |
Correct |
618 ms |
5668 KB |
Output is correct |
15 |
Correct |
481 ms |
5660 KB |
Output is correct |
16 |
Correct |
448 ms |
5808 KB |
Output is correct |
17 |
Correct |
545 ms |
5652 KB |
Output is correct |
18 |
Correct |
440 ms |
5788 KB |
Output is correct |
19 |
Correct |
446 ms |
5804 KB |
Output is correct |
20 |
Correct |
473 ms |
5792 KB |
Output is correct |
21 |
Correct |
58 ms |
5796 KB |
Output is correct |
22 |
Correct |
55 ms |
5876 KB |
Output is correct |
23 |
Correct |
78 ms |
5828 KB |
Output is correct |
24 |
Correct |
7 ms |
5756 KB |
Output is correct |
25 |
Correct |
7 ms |
5752 KB |
Output is correct |
26 |
Correct |
6 ms |
5752 KB |
Output is correct |
27 |
Correct |
6 ms |
5756 KB |
Output is correct |
28 |
Correct |
4 ms |
5748 KB |
Output is correct |
29 |
Correct |
489 ms |
5664 KB |
Output is correct |
30 |
Correct |
459 ms |
5664 KB |
Output is correct |
31 |
Correct |
391 ms |
5672 KB |
Output is correct |
32 |
Correct |
451 ms |
5664 KB |
Output is correct |
33 |
Correct |
386 ms |
5668 KB |
Output is correct |
34 |
Correct |
286 ms |
5780 KB |
Output is correct |
35 |
Correct |
341 ms |
5920 KB |
Output is correct |
36 |
Correct |
452 ms |
6016 KB |
Output is correct |
37 |
Correct |
481 ms |
5780 KB |
Output is correct |
38 |
Correct |
443 ms |
5924 KB |
Output is correct |
39 |
Correct |
403 ms |
5812 KB |
Output is correct |
40 |
Correct |
447 ms |
5824 KB |
Output is correct |
41 |
Correct |
446 ms |
5816 KB |
Output is correct |
42 |
Correct |
59 ms |
5856 KB |
Output is correct |
43 |
Correct |
111 ms |
5852 KB |
Output is correct |
44 |
Correct |
146 ms |
5792 KB |
Output is correct |
45 |
Correct |
168 ms |
5844 KB |
Output is correct |
46 |
Correct |
278 ms |
5796 KB |
Output is correct |
47 |
Correct |
252 ms |
5796 KB |
Output is correct |
48 |
Correct |
67 ms |
5792 KB |
Output is correct |
49 |
Correct |
55 ms |
5960 KB |
Output is correct |