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 <bits/stdc++.h>
using namespace std;
/*
Label v with tin[v] for even levels and tout[v] for odd levels.
This gives us labels of even numbers between [0, 2*n-2].
We can divide each label by 2, so we get all labels in [0, n-1]
Note: We can always restore [tin[v], tout[v]] by v and its neighbors' labels.
*/
void dfs(int v, int par, int level, int &timer, vector<int> &tin, vector<int> &tout, vector<int> &labels, vector<vector<int>> &g) {
tin[v] = timer++;
for (int to : g[v]) {
if (to != par) {
dfs(to, v, level + 1, timer, tin, tout, labels, g);
}
}
tout[v] = timer++;
if (level % 2 == 0) {
labels[v] = tin[v] / 2;
} else {
labels[v] = tout[v] / 2;
}
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
vector<vector<int>>g(n);
vector<int>tin(n), tout(n);
vector<int>labels(n);
int timer = 0;
for (int i = 0; i < n - 1; ++i) {
g[u[i]].emplace_back(v[i]);
g[v[i]].emplace_back(u[i]);
}
dfs(0, 0, 0, timer, tin, tout, labels, g);
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
if (c[0] > s) { /// s is labeled with tin[]
if (t < s) {
return c.back();
} else {
auto itr = lower_bound(c.begin(), c.end(), t);
if (itr == c.end()) --itr;
return (*itr);
}
} else { /// s is labeled with tout[]
if (t > s) {
return c[0];
} else {
auto itr = upper_bound(c.begin(), c.end(), t);
if (itr != c.begin()) --itr;
return (*itr);
}
}
}
# | 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... |