QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#743203 | #5088. Two Choreographies | ucup-team5062# | WA | 57ms | 14100kb | C++20 | 5.0kb | 2024-11-13 18:34:27 | 2024-11-13 18:34:29 |
Judging History
answer
#include <set>
#include <array>
#include <queue>
#include <string>
#include <vector>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
string to_string(const vector<int>& arr) {
string res = "[";
for (int i = 0; i < int(arr.size()); i++) {
if (i != 0) {
res += ", ";
}
res += to_string(arr[i]);
}
res += "]";
return res;
}
pair<int, vector<int> > component(int N, const vector<vector<int> >& G) {
vector<int> res(N, -1);
int rank = 0;
auto dfs = [&](auto& self, int x) -> void {
res[x] = rank;
for (int i : G[x]) {
if (res[i] == -1) {
self(self, i);
}
}
};
for (int i = 0; i < N; i++) {
if (res[i] == -1) {
dfs(dfs, i);
rank++;
}
}
return {rank, res};
}
pair<vector<int>, vector<int> > solve_connected(int N, const vector<vector<int> >& G) {
int root = -1;
for (int i = 0; i < N; i++) {
if (G[i].size() >= 3) {
root = i;
break;
}
}
vector<bool> adj(N, false);
set<pair<int, int> > tree;
auto add_edge = [&](int a, int b) {
if (a > b) {
swap(a, b);
}
tree.insert({a, b});
};
for (int i = 1; i < G[root].size(); i++) {
adj[G[root][i]] = true;
add_edge(root, G[root][i]);
}
vector<bool> vis(N, false);
vector<int> d(N, 1), p(N, root);
p[root] = -1;
d[root] = 0;
auto dfs = [&](auto& self, int x) -> void {
vis[x] = true;
for (int i : G[x]) {
if (!adj[i]) {
if (!vis[i]) {
add_edge(x, i);
d[i] = d[x] + 1;
p[i] = x;
self(self, i);
}
}
}
};
dfs(dfs, root);
vector<array<int, 3> > edges;
for (int i = 0; i < N; i++) {
for (int j : G[i]) {
if (i < j && tree.find({i, j}) == tree.end()) {
int dist = (!adj[i] && !adj[j] ? d[j] - d[i] : d[j] + d[i]);
edges.push_back({dist, i, j});
}
}
}
sort(edges.begin(), edges.end());
pair<int, int> u1 = {-1, -1}, u2 = {-1, -1};
for (int i = 1; i < edges.size(); i++) {
if (edges[i - 1][0] == edges[i][0]) {
u1 = {edges[i - 1][1], edges[i - 1][2]};
u2 = {edges[i][1], edges[i][2]};
break;
}
}
auto get_path = [&](int x, int y) -> vector<int> {
vector<int> xv, yv;
while (x != y) {
if (d[x] >= d[y]) {
xv.push_back(x);
x = p[x];
} else {
yv.push_back(y);
y = p[y];
}
}
xv.push_back(x);
reverse(yv.begin(), yv.end());
xv.insert(xv.end(), yv.begin(), yv.end());
return xv;
};
return {get_path(u1.first, u1.second), get_path(u2.first, u2.second)};
}
pair<vector<int>, vector<int> > solve(int N, const vector<vector<int> >& G) {
// step #1. consider components
auto [Z, comp] = component(N, G);
vector<int> verts(Z), edges(Z);
for (int i = 0; i < N; i++) {
verts[comp[i]]++;
edges[comp[i]] += G[i].size();
}
for (int i = 0; i < Z; i++) {
edges[i] /= 2;
}
int id = -1;
for (int i = 0; i < Z; i++) {
if (verts[i] >= 4 && edges[i] >= 2 * verts[i] - 3) {
id = i;
break;
}
}
assert(id != -1);
// step #2. reduction
vector<int> idx(N, -1), revidx;
int cnt = 0;
for (int i = 0; i < N; i++) {
if (comp[i] == id) {
idx[i] = cnt++;
revidx.push_back(i);
}
}
vector<vector<int> > H(verts[id]);
for (int i = 0; i < N; i++) {
if (comp[i] == id) {
for (int j : G[i]) {
H[idx[i]].push_back(idx[j]);
}
}
}
pair<vector<int>, vector<int> > res = solve_connected(verts[id], H);
// step #3. restoration
pair<vector<int>, vector<int> > ans = res;
for (int& i : ans.first) {
i = revidx[i];
}
for (int& i : ans.second) {
i = revidx[i];
}
return ans;
}
int main() {
// step #1. input
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<vector<int> > G(N);
for (int i = 0; i < 2 * N - 3; i++) {
int a, b;
cin >> a >> b;
a--; b--;
G[a].push_back(b);
G[b].push_back(a);
}
pair<vector<int>, vector<int> > ans = solve(N, G);
int L = ans.first.size();
cout << L << '\n';
for (int i = 0; i < L; i++) {
cout << ans.first[i] + 1 << (i != L - 1 ? ' ' : '\n');
}
for (int i = 0; i < L; i++) {
cout << ans.second[i] + 1 << (i != L - 1 ? ' ' : '\n');
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
input:
4 1 2 1 3 1 4 2 3 2 4
output:
3 2 1 3 2 1 4
result:
ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3488kb
input:
5 1 2 1 3 1 4 1 5 2 3 2 5 3 4
output:
3 2 1 3 2 1 5
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3824kb
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 2 1 5 2 1 6
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 3556kb
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 4 1 19 16 1 19
result:
ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3624kb
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:
14 43 66 151 51 167 33 74 58 86 115 19 46 3 61 58 86 115 19 46 3 61 96 16 77 100 21 32 144
result:
ok
Test #6:
score: 0
Accepted
time: 4ms
memory: 4248kb
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:
777 256 1567 7552 3069 5933 2498 7182 5771 7641 7242 6027 2790 2034 5325 4414 3613 7427 3205 1179 4895 4317 666 4443 5159 4553 4239 3923 1490 6488 6185 2215 5037 2426 3183 1846 6312 3740 3626 7445 3951 7514 5285 4274 7703 6882 4888 6690 6988 7354 2162 3686 3707 4997 4640 5712 5818 6681 4382 3892 588...
result:
ok
Test #7:
score: 0
Accepted
time: 48ms
memory: 14100kb
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:
11124 8757 15176 42467 61734 61865 28569 85867 32679 81073 76695 92355 87742 62273 20523 71862 97919 11492 43438 14945 85425 89844 77937 10304 95416 35435 10359 2153 67974 66678 61519 21402 11345 37651 41993 80467 35070 74903 65494 93894 56497 32329 61625 21432 52004 33399 72000 95469 26976 40230 85...
result:
ok
Test #8:
score: 0
Accepted
time: 57ms
memory: 14016kb
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:
11324 12185 86163 77912 68628 57028 57831 84433 60841 70609 24261 91104 99743 3103 89970 31480 99784 20502 88133 71665 5864 95051 46466 57337 94979 99944 74645 87355 47637 88648 58174 84874 88489 99038 62432 67146 16163 47022 79727 54049 25650 77574 76654 78071 97679 96098 60959 46967 23786 12515 78...
result:
ok
Test #9:
score: -100
Wrong Answer
time: 1ms
memory: 3808kb
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:
3 4 1 5 5 1 6
result:
wrong answer Wrong output - Nonexisting edge.