QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#268322 | #5088. Two Choreographies | STnofarjo# | WA | 39ms | 23356kb | C++20 | 4.1kb | 2023-11-28 15:48:40 | 2023-11-28 15:48:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
vector<int> deg(n + 1);
for (int i = 0; i < (2 * n - 3); i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
deg[i] = adj[i].size();
}
vector<int> cc;
vector<int> vis(n + 1);
const function<void(int)> ff = [&](int v) -> void {
for (auto c : adj[v]) {
if (vis[c] > 0) continue;
vis[c]++;
cc.push_back(c);
ff(c);
}
};
const int B = 20;
vector<vector<int>> up(n + 1, vector<int>(B + 1));
vector<int> dep(n + 1);
auto lca = [&](int p, int q) -> int {
int dp = dep[p], dq = dep[q];
if (dp < dq) {
swap(p, q);
swap(dp, dq);
}
// cout << "start lca " << p << ' ' << q << ' ' << dep[p] << ' ' << dep[q] << '\n';
int need = dp - dq;
for (int i = B; i >= 0; i--) {
if (need >= (1 << i)) {
need -= (1 << i);
p = up[p][i];
}
}
if (p == q) return p;
// cout << "finding lca " << p << ' ' << q << ' ' << p << ' ' << q << '\n';
for (int i = B; i >= 0; i--) {
int a = up[p][i], b = up[q][i];
if (a != b) {
// cout << "finding lca " << i << ' ' << p << ' ' << q << ' ' << a << ' ' << b << '\n';
p = a, q = b;
}
}
// cout << "finding lcaxx " << p << ' ' << q << ' ' << p << ' ' << q << '\n';
// cout << "cek " << p << ' ' << up[p][0] << '\n';
return up[p][0];
};
auto get_distance = [&](int p, int q) -> int {
return dep[p] + dep[q] - 2 * dep[lca(p, q)];
};
auto print_cycle = [&](int x, int y) -> void {
int L = lca(x, y);
// cout << "lca " << x << ' ' << y << ' ' << L << '\n';
vector<int> xx, yy;
int cur_x = x, cur_y = y;
while (cur_x != L) {
xx.push_back(cur_x);
cur_x = up[cur_x][0];
}
while (cur_y != L) {
yy.push_back(cur_y);
cur_y = up[cur_y][0];
}
for (int i = 0; i < (int)xx.size(); i++) {
cout << xx[i] << ' ';
}
cout << L << ' ';
for (int i = (int)yy.size() - 1; i >= 0; i--) {
cout << yy[i] << ' ';
}
cout << '\n';
};
map<int, vector<pair<int, int>>> cyc;
vector<vector<int>> triangles;
for (int i = 1; i <= n; i++) {
if (vis[i] > 0) continue;
cyc.clear();
cc.clear();
cc.push_back(i);
vis[i]++;
ff(i);
int sz = cc.size();
int cnt_edges = 0;
for (auto c : cc) {
// cout << c << '\n';
cnt_edges += deg[c];
}
cnt_edges /= 2;
// cout << i << ' ' << sz << ' ' << cnt_edges << '\n';
if (sz <= 2) continue;
else if (sz == 3) {
if (cnt_edges <= 2) continue;
triangles.push_back(cc);
if (triangles.size() >= 2) {
cout << 3 << '\n';
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
cout << triangles[j][k] << ' ';
}
cout << '\n';
}
return 0;
}
} else if (sz >= 4) {
if (cnt_edges < 2 * sz - 3) {
continue;
}
int root = -1;
for (auto c : cc) {
if (deg[c] >= 3) {
root = c;
break;
}
}
assert(root != -1);
// cout << "root " << root << '\n';
vis[root]++;
queue<int> Q;
Q.push(root);
while (!Q.empty()) {
int v = Q.front();
Q.pop();
for (auto c : adj[v]) {
if (c == up[v][0]) continue;
if (vis[c] >= 2) {
//v ke c udah ada
int dis = get_distance(c, v);
// cout << "dis " << c << ' ' << v << ' ' << dis << '\n';
cyc[dis + 1].emplace_back(c, v);
if (cyc[dis + 1].size() >= 2) {
cout << dis + 1 << '\n';
for (int i = 0; i < 2; i++) {
// cout << "printing cycle " << cyc[dis + 1][i].first << ' ' << cyc[dis + 1][i].second << '\n';
print_cycle(cyc[dis + 1][i].first, cyc[dis + 1][i].second);
}
return 0;
}
continue;
}
// cerr << "hehe " << c << ' ' << v << '\n';
// cerr << "hehehe " << dep[v] << '\n';
dep[c] = dep[v] + 1;
vis[c]++;
up[c][0] = v;
for (int i = 1; i <= B; i++) {
up[c][i] = up[up[c][i - 1]][i - 1];
}
Q.push(c);
}
}
return 0;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3448kb
input:
4 1 2 1 3 1 4 2 3 2 4
output:
3 3 1 2 4 1 2
result:
ok
Test #2:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
5 1 2 1 3 1 4 1 5 2 3 2 5 3 4
output:
3 3 1 2 5 1 2
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3388kb
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:
3 5 1 2 6 1 2
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 3416kb
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:
3 19 1 16 36 1 16
result:
ok
Test #5:
score: 0
Accepted
time: 1ms
memory: 3480kb
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:
4 191 19 46 21 151 19 46 51
result:
ok
Test #6:
score: 0
Accepted
time: 3ms
memory: 4680kb
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:
6 917 7826 1 5680 5956 4731 2966 6074 2486 5680 5956 5072
result:
ok
Test #7:
score: -100
Wrong Answer
time: 39ms
memory: 23356kb
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:
8 1747 45075 65794 11261 1 44345 98476 30620 30620 98476 44345 1 11261 65794 45075 1747
result:
wrong answer Wrong output - Identical cycle.