This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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) {
| ~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |