#include "stations.h"
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MX = 1e3+5;
int cnt, in[MX], out[MX];
vector<int> labels, G[MX];
void dfs(int u, int p, int d) {
in[u] = cnt++;
for (int v : G[u]) if (v != p) dfs(v, u, d+1);
out[u] = cnt++;
labels[u] = (d%2 == 0 ? in[u] : out[u]);
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
for (int i = 0; i < n; ++i) G[i].clear();
for (int i = 0; i < n-1; ++i)
G[u[i]].push_back(v[i]), G[v[i]].push_back(u[i]);
labels.resize(n, 0);
cnt = 1;
dfs(0, -1, 0);
// for (int x : labels) cout << x << '\n';
vector<int> tmp(begin(labels), end(labels));
sort(begin(tmp), end(tmp));
for (int i = 0; i < labels.size(); ++i)
labels[i] = lower_bound(begin(tmp), end(tmp), labels[i])-begin(tmp);
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
if (c.size() == 1) return c[0];
unordered_map<int, int> in, out;
int m = *min_element(begin(c), end(c));
if (s < m) {
in[s] = s;
for (int i = 0; i < c.size(); ++i) out[c[i]] = c[i];
if (in[s] == 0) {
// s is root
for (int i = 0; i < c.size(); ++i) {
if (t <= out[c[i]]) return c[i];
}
}
else {
int p = *max_element(begin(c), end(c));
c.erase(find(begin(c), end(c), p));
in[c[0]] = in[s]+1;
for (int i = 1; i < c.size(); ++i) in[c[i]] = out[c[i-1]]+1;
out[s] = out[c.back()]+1;
for (int i = 0; i < c.size(); ++i) {
if (t > in[c[i]] && t < out[c[i]]) return c[i];
}
return p;
}
}
else {
out[s] = s;
for (int i = 0; i < c.size(); ++i) in[c[i]] = c[i];
int p = m;
c.erase(find(begin(c), end(c), p));
in[s] = in[c[0]]-1;
for (int i = 0; i < c.size()-1; ++i) out[c[i]] = in[c[i+1]]-1;
out[c.back()] = out[s]-1;
for (int i = 0; i < c.size(); ++i) {
if (t > in[c[i]] && t < out[c[i]]) return c[i];
}
return p;
}
// cout << s << ' ' << t;
// for (int x : c) cout << ' ' << x;
// cout << '\n';
// return c.back();
}
Compilation message
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:30:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < labels.size(); ++i)
| ~~^~~~~~~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:41:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < c.size(); ++i) out[c[i]] = c[i];
| ~~^~~~~~~~~~
stations.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < c.size(); ++i) {
| ~~^~~~~~~~~~
stations.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 1; i < c.size(); ++i) in[c[i]] = out[c[i-1]]+1;
| ~~^~~~~~~~~~
stations.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < c.size(); ++i) {
| ~~^~~~~~~~~~
stations.cpp:62:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < c.size(); ++i) in[c[i]] = c[i];
| ~~^~~~~~~~~~
stations.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i = 0; i < c.size()-1; ++i) out[c[i]] = in[c[i+1]]-1;
| ~~^~~~~~~~~~~~
stations.cpp:68:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i < c.size(); ++i) {
| ~~^~~~~~~~~~
stations.cpp:37:26: warning: control reaches end of non-void function [-Wreturn-type]
37 | unordered_map<int, int> in, out;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
561 ms |
1060 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
459 ms |
872 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
527 ms |
864 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1002 ms |
1012 KB |
Output is correct |
2 |
Incorrect |
843 ms |
864 KB |
Wrong query response. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
617 ms |
1128 KB |
Wrong query response. |
2 |
Halted |
0 ms |
0 KB |
- |