#include "stations.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
void dfs(int src, int parent, int depth, std::vector<std::vector<int>>& adj,
int& nextLabel, std::vector<int>& labels) {
if (depth % 2 == 0) {
labels[src] = nextLabel++;
}
for (int dst : adj[src]) {
if (dst == parent) continue;
dfs(dst, src, depth + 1, adj, nextLabel, labels);
}
if (depth % 2 == 1) {
labels[src] = nextLabel++;
}
}
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
assert(n <= k);
auto adj = std::vector<std::vector<int>>(n, std::vector<int>());
assert(u.size() == v.size());
for (int index = 0; index < u.size(); ++index) {
adj[u[index]].push_back(v[index]);
adj[v[index]].push_back(u[index]);
}
auto labels = std::vector<int>(n, -1);
int nextLabel = 0;
dfs(0, -1, 0, adj, nextLabel, labels);
for (auto &x : labels) std::cout << x << ", "; std::cout << std::endl;
return labels;
}
int find_next_station(int s, int t, std::vector<int> c) {
if (s == t) return t;
for (int neighbour : c) if (neighbour == t) return t;
std::sort(c.begin(), c.end());
auto rc = std::vector<int>(c.begin(), c.end());
std::reverse(rc.begin(), rc.end());
if (s == 0) { // we are at the root => there's no parent node
for (int neighbour : c) if (t <= neighbour) return neighbour;
} else {
if (s < c[0]) { // even depth
if (t > s && t < c.back()) { // go to a child
for (int neighbour : c) if (t <= neighbour) return neighbour;
} else { // go to parent
return c.back();
}
} else { // odd depth
assert(c.back() < s);
if (t > c[0] && t < s) { // go to a child
for (int neighbour : rc) if (t >= neighbour) return neighbour;
} else { // go to parent
return c[0];
}
}
}
assert(false);
}
Compilation message
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:25:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int index = 0; index < u.size(); ++index) {
| ~~~~~~^~~~~~~~~~
stations.cpp:32:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
32 | for (auto &x : labels) std::cout << x << ", "; std::cout << std::endl;
| ^~~
stations.cpp:32:52: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
32 | for (auto &x : labels) std::cout << x << ", "; std::cout << std::endl;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
364 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
364 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
364 KB |
Execution killed with signal 13 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |