QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#252631 | #5088. Two Choreographies | SamNguyen05 | WA | 59ms | 26500kb | C++20 | 2.3kb | 2023-11-15 23:00:16 | 2023-11-15 23:00:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int LOG_MAX = 20;
const int MAX = 1e5 + 7;
int N;
vector<pair<int, int>> edges;
vector<int> adj[MAX];
void input() {
cin >> N;
for (int t = 2 * N - 3; t--; ) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
edges.emplace_back(u, v);
}
}
vector<int> tree[MAX];
int up[LOG_MAX][MAX], tin[MAX], tout[MAX], depth[MAX], COUNTER;
void dfs(int u, int p = 0) {
tin[u] = ++COUNTER;
depth[u] = depth[p] + 1;
up[0][u] = p;
for (int j = 1; j < LOG_MAX; j++)
up[j][u] = up[j - 1][up[j - 1][u]];
for (int v : adj[u]) if (tin[v] == 0) {
tree[u].push_back(v);
dfs(v, u);
}
tout[u] = COUNTER;
}
inline bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] and tout[v] <= tout[u];
}
inline int lca(int u, int v) {
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int j = LOG_MAX - 1; j >= 0; j--) {
if (up[j][v] == 0)
continue;
if (is_ancestor(up[j][v], u))
continue;
v = up[j][v];
}
return up[0][v];
}
void init() {
COUNTER = 0;
depth[0] = 0;
memset(up, 0, sizeof up);
memset(tin, 0, sizeof tin);
memset(tout, 0, sizeof tout);
for (int u = 1; u <= N; u++) if (tin[u] == 0)
dfs(u);
}
inline int dist(int u, int v) {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
void print_path(pair<int, int> e) {
int u, v;
tie(u, v) = e;
int z = lca(u, v);
vector<int> path_u, path_v;
for (; u != z; u = up[0][u])
path_u.push_back(u);
for (; v != z; v = up[0][v])
path_v.push_back(v);
reverse(path_v.begin(), path_v.end());
for (int u : path_u)
cout << u << " ";
cout << z << " ";
for (int v : path_v)
cout << v << " ";
cout << endl;
}
pair<int, int> back_edges[MAX];
void solve() {
for (int d = 0; d <= N; d++)
back_edges[d] = make_pair(-1, -1);
for (const auto &e : edges) {
int u, v;
tie(u, v) = e;
if (up[0][u] == v or up[0][v] == u)
continue;
int d = dist(u, v);
if (back_edges[d].first != -1) {
cout << d + 1 << endl;
print_path(e);
print_path(back_edges[d]);
return;
}
back_edges[d] = e;
}
cout << -1;
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
input();
init();
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 18096kb
input:
4 1 2 1 3 1 4 2 3 2 4
output:
3 1 2 4 1 2 3
result:
ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 16840kb
input:
5 1 2 1 3 1 4 1 5 2 3 2 5 3 4
output:
3 1 2 5 1 2 3
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 17888kb
input:
7 1 2 3 4 5 6 5 2 3 1 6 1 4 2 4 5 2 6 3 6 1 5
output:
5 4 3 6 5 2 3 6 5 2 1
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 17080kb
input:
40 1 16 1 40 2 4 2 16 2 36 3 25 3 38 4 1 4 13 5 11 5 27 6 4 7 5 7 11 8 10 8 14 8 24 9 34 10 20 12 35 13 2 14 10 14 20 15 18 15 28 15 31 16 6 16 13 17 5 17 11 17 27 18 9 19 1 19 4 19 16 20 24 21 12 21 33 21 35 22 38 23 12 23 21 25 28 25 31 25 34 25 38 26 14 26 20 27 7 27 11 28 3 28 31 29 16 29 19 30 ...
output:
4 5 11 7 27 4 2 16 1
result:
ok
Test #5:
score: 0
Accepted
time: 3ms
memory: 17968kb
input:
201 1 7 1 114 1 119 2 49 2 93 4 197 5 139 6 1 6 27 7 39 7 121 8 127 9 130 9 145 11 106 11 136 11 193 12 2 12 116 13 55 13 69 13 105 13 187 13 196 14 144 14 177 15 127 15 134 15 145 15 155 15 184 15 199 16 96 16 177 17 20 21 100 22 68 22 71 22 81 22 142 23 148 23 190 24 12 24 81 24 89 25 158 25 159 2...
output:
23 13 55 126 98 38 83 78 122 44 37 28 62 101 183 67 69 146 76 92 118 84 94 196 7 39 34 31 153 97 95 82 50 48 109 119 176 163 35 25 158 123 175 11 106 36 121
result:
ok
Test #6:
score: 0
Accepted
time: 3ms
memory: 18496kb
input:
8000 2 1508 2 3068 3 5268 3 5501 6 266 6 2737 6 3197 6 5863 6 6697 7 3492 9 427 9 794 9 3114 9 5509 10 2257 10 4348 11 1479 11 1957 11 2230 11 2500 11 3182 11 4399 11 5051 11 7727 12 7669 13 1403 13 5753 14 2871 14 6956 14 7959 15 6902 17 1630 17 3155 17 5950 18 7232 19 125 19 3280 19 5648 20 6879 2...
output:
614 71 115 910 604 675 431 2874 1031 2937 3886 2200 6551 68 403 5289 1339 6402 2696 2530 1782 1211 7982 1516 443 1154 381 2039 2540 5135 3242 1374 7192 2144 844 113 1481 721 3235 5019 1007 7594 2456 3902 1072 2042 286 5089 1119 5031 1121 3258 2997 6000 6999 3780 3150 3347 4482 2345 748 1192 6038 403...
result:
ok
Test #7:
score: 0
Accepted
time: 49ms
memory: 26408kb
input:
99999 1 11261 1 21544 2 9017 2 63063 2 97990 3 11995 3 42473 4 19846 5 38099 6 35872 6 80509 7 73231 8 12356 9 35384 10 45091 12 86727 13 4938 13 48917 14 62383 14 89846 15 28458 15 44277 15 51725 15 84522 16 93258 17 13934 17 42238 18 19000 19 11278 19 23672 19 61502 19 78791 20 85057 20 88080 21 2...
output:
4045 467 30909 42458 34024 69805 58085 40831 29925 22809 30773 49872 36323 76884 25831 63967 39234 18007 91214 25558 49193 52988 35092 31583 54665 48031 28356 37901 52992 21888 73745 21767 63776 16086 2203 16838 23718 77357 62130 43140 85662 31445 71319 8173 1566 94933 39021 18207 21409 41750 79194 ...
result:
ok
Test #8:
score: 0
Accepted
time: 59ms
memory: 26500kb
input:
100000 1 68531 2 97359 4 68578 4 83098 4 98443 5 8053 5 30270 5 86617 6 7074 6 12266 6 69396 7 52675 7 78316 7 90757 7 92242 8 32677 8 41353 8 41457 8 74508 9 44234 10 4973 10 38390 10 96049 11 28007 11 68620 13 3016 14 36748 15 8147 15 25110 15 28489 15 72947 15 99347 16 70760 17 12774 17 68407 17 ...
output:
10016 38 42351 47944 8318 62618 96195 58059 31941 27067 1690 32276 59576 16389 4558 51721 6314 87190 44810 8913 21447 19323 15317 6848 46277 75083 2032 55525 38323 46637 24034 6846 66337 43581 17975 40619 32191 29283 13945 813 58709 27184 75753 13371 6745 10657 48493 35916 3628 20850 41369 12638 475...
result:
ok
Test #9:
score: -100
Wrong Answer
time: 3ms
memory: 16816kb
input:
7 1 2 2 3 3 4 4 5 5 6 6 7 7 5 1 4 7 3 1 6 7 1
output:
-1
result:
wrong answer Integer -1 violates the range [3, 7]