#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int L = 10;
int par[N][L], dep[N];
std::vector<int> G[N];
void dfs_init(int u, int p) {
par[u][0] = p;
for (int i = 1; i < L; ++i)
if (par[u][i - 1] != -1)
par[u][i] = par[par[u][i - 1]][i - 1];
for (int v : G[u])
if (v != p)
dep[v] = dep[u] + 1, dfs_init(v, u);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = L - 1; ~i; --i)
if (par[u][i] != -1 && dep[par[u][i]] >= dep[v])
u = par[u][i];
if (u == v) return u;
for (int i = L - 1; ~i; --i)
if (par[u][i] != par[v][i])
u = par[u][i], v = par[v][i];
return (u == v ? u : par[u][0]);
}
int n;
std::vector<int> vec;
std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
::n = n;
for (int i = 0; i < n - 1; ++i) {
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
}
int root = 0;
for (int i = 0; i < n; ++i) {
if (G[i].size() == 1) {
root = i;
}
}
function<void(int, int)> Dfs = [&](int u, int p) {
vec.push_back(u);
for (int v : G[u]) {
if (v == p) continue;
Dfs(v, u);
}
};
Dfs(root, -1);
//dfs_init(0, -1);
std::vector<int> l(n);
iota(l.begin(), l.end(), 0);
return l;
}
int find_next_station(int s, int t, std::vector<int> c) {
//assert(s < n && t < n);
/*std::vector<int> path;
int l = lca(s, t);
while (s != -1 && dep[s] >= dep[l]) {
path.push_back(s);
s = par[s][0];
}
while (t != -1 && dep[t] >= dep[l]) {
path.push_back(t);
t = par[t][0];
}
int node = 0;
for (int i : path) for (int j : c)
if (i == j) node = j;
return node;*/
int pos_s = 0, pos_t = 0;
for (int i = 0; i < vec.size(); i++) {
if (vec[i] == s) pos_s = i;
if (vec[i] == t) pos_t = i;
}
std::vector<int> path;
for (int i = pos_s; i <= pos_t; i++) {
path.push_back(vec[i]);
}
for (int i = pos_t; i <= pos_s; i++) {
path.push_back(vec[i]);
}
int node = 0;
for (int i : path) for (int j : c)
if (i == j) node = j;
return node;
}
Compilation message
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < vec.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1319 ms |
2097156 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3135 ms |
1051252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1315 ms |
2097156 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
768 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2082 ms |
2097156 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |