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;
const int mxN=1000;
int n, tin[mxN], tout[mxN], t, d[mxN];
vector<int> adj[mxN];
void dfs(int u=0, int p=-1) {
tin[u]=t++;
for (int v : adj[u])
if (v!=p) {
d[v]=d[u]+1;
dfs(v, u);
}
tout[u]=t++;
}
vector<int> label(int _n, int _k, vector<int> u, vector<int> v) {
n=_n;
for (int i=0; i<n; ++i)
adj[i].clear();
for (int i=0; i<n-1; i++) {
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
}
dfs();
vector<int> inds(n), labels(n);
iota(inds.begin(), inds.end(), 0);
sort(inds.begin(), inds.end(), [](int a, int b) { return (d[a]%2?tout[a]:tin[a])<(d[b]%2?tout[b]:tin[b]); });
for (int i=0; i<n; ++i)
labels[inds[i]]=i;
for (int i : labels)
cout << i << " ";
cout << endl;
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
if (c.back()>s) { // s is tin[s]
if (!s||s<=t&&t<=c.end()[-2]) { // t is in subtree of s
for (int i=0; i+(s>0)<c.size(); ++i)
if (t<=c[i])
return c[i];
assert(0);
} else
return c.back();
} else {
if (c.size()>1&&c[1]<=t&&t<=s) { // again in subtree
for (int i=c.size()-1; i; --i)
if (t>=c[i])
return c[i];
assert(0);
} else
return c[0];
}
}
Compilation message (stderr)
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:41:15: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
41 | if (!s||s<=t&&t<=c.end()[-2]) { // t is in subtree of s
stations.cpp:42:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i=0; i+(s>0)<c.size(); ++i)
| ~~~~~~~^~~~~~~~~
# | 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... |