QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#759768 | #5088. Two Choreographies | gugugaga# | WA | 46ms | 13916kb | C++20 | 5.3kb | 2024-11-18 11:57:11 | 2024-11-18 11:57:12 |
Judging History
answer
/*************************************
* author: marvinthang *
* created: 18.11.2024 10:41:51 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left___
#define right ___right___
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#ifdef LOCAL
#include "debug.h"
#else
#define DB(...)
#define db(...) ""
#define debug(...)
#endif
namespace std {
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream &print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class...U> print_op(tuple <U...>) { return print_tuple_utils<0, tuple <U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))>typename enable_if <!is_same<Con, string>::value, ostream &>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class T> print_op(stack <T>) { vector <T> v; stack <T> st = u; while (!st.empty()) v.push_back(st.top()), st.pop(); reverse(v.begin(), v.end()); return out << v; }
template <class T> print_op(queue <T>) { queue <T> q = u; out << '{'; while (!q.empty()) { out << q.front(); q.pop(); if (!q.empty()) out << ", "; } out << '}'; return out; }
template <class T, class X, class Y> print_op(priority_queue <T, X, Y>) { priority_queue <T, X, Y> pq = u; out << '{'; while (!pq.empty()) { out << pq.top(); pq.pop(); if (!pq.empty()) out << ", "; } out << '}'; return out; }
template <class Fun> class y_combinator_result { Fun fun_; public: template <class T> explicit y_combinator_result(T &&fun): fun_(forward<T>(fun)) {} template <class...Args> decltype(auto)operator()(Args &&...args) { return fun_(ref(*this), forward<Args>(args)...); } };
template <class Fun> decltype(auto)y_combinator(Fun &&fun) { return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun)); }
template <typename T, int D> struct Vec: public vector <Vec<T, D - 1>> { static_assert(D >= 1, "Vector dimension must be greater than zero!"); template <typename ...Args> Vec(int n = 0, Args ...args): vector <Vec<T, D - 1>>(n, Vec<T, D - 1>(args...)) {} };
template <typename T> struct Vec<T, 1>: public vector<T>{ Vec(int n = 0, const T &val = T()): vector<T>(n, val) {} };
#if __cplusplus < 202002L
template <class T> int ssize(const T &a) { return a.size(); }
#endif
}
void process(void) {
int n; cin >> n;
Vec <int, 2> adj(n);
vector<int> deg(n);
for (int i = 0; i < 2 * n - 3; ++i) {
int u, v; cin >> u >> v; --u; --v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
deg[u]++, deg[v]++;
}
vector<int> vis(n);
vector<int> depth(n), par(n, -1);
queue<int> q;
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int u, int v) { return deg[u] > deg[v]; });
for (int root : ord) {
if (vis[root]) continue;
vis[root] = 1;
q.emplace(root);
while (q.size()) {
int u = q.front(); q.pop();
for (int v: adj[u]) {
if (vis[v]) continue;
vis[v] = 1;
depth[v] = depth[u] + 1;
par[v] = u;
q.push(v);
}
}
}
auto extract_cycle = [&](int x, int y) {
vector<int> l, r;
while (depth[x] > depth[y]) {
l.push_back(x);
x = par[x];
}
while (depth[y] > depth[x]) {
r.push_back(y);
y = par[y];
}
while (x != y) {
l.push_back(x);
r.push_back(y);
x = par[x];
y = par[y];
}
l.push_back(x);
reverse(r.begin(), r.end());
for (int v: r) l.push_back(v);
return l;
};
vector<vector<int>> cycle(n + 1);
for (int i = 0; i < n; i++) {
for (int j : adj[i]) {
if (par[i] == j || par[j] == i) continue;
auto&& c = extract_cycle(i, j);
if (cycle[c.size()].empty()) {
cycle[c.size()] = c;
} else {
cout << cycle[c.size()].size() << '\n';
for (int v: cycle[c.size()]) cout << v + 1 << ' ';
cout << '\n';
for (int v: c) cout << v + 1 << ' ';
cout << '\n';
return;
}
}
}
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
file("test");
// int t; cin >> t; while (t--)
process();
return (0^0);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
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: 3480kb
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: 3768kb
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: 3776kb
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 1 16 40 1 16 19
result:
ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3528kb
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:
6 1 114 153 97 164 6 2 52 89 71 22 49
result:
ok
Test #6:
score: 0
Accepted
time: 3ms
memory: 4000kb
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:
9 5 6230 1652 6625 3460 1974 1422 3327 144 6 3197 6577 4406 338 7325 5714 6617 266
result:
ok
Test #7:
score: 0
Accepted
time: 37ms
memory: 13916kb
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:
12 1 44345 16942 77380 70500 25709 68619 36910 42714 27892 85329 11261 3 11995 48939 9926 41594 71692 9424 50668 36525 60074 69888 42473
result:
ok
Test #8:
score: 0
Accepted
time: 46ms
memory: 13840kb
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:
14 1 68531 75521 56222 73321 86944 25711 17624 24176 33667 29357 97496 32927 15640 1 68531 75521 56222 73321 86944 25711 17624 54802 15036 33317 23822 97734 66039
result:
ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3580kb
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:
4 3 2 1 4 3 2 1 7
result:
ok
Test #10:
score: 0
Accepted
time: 27ms
memory: 13440kb
input:
100000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 5...
output:
4 1 100000 5 4 1 100000 7 6
result:
ok
Test #11:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
8 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 6 1 4 8 4 8 3 8 2 1 8
output:
3 1 8 2 1 8 4
result:
ok
Test #12:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
9 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 1 3 1 4 9 5 9 4 1 7 9 2 1 9
output:
3 2 1 3 2 1 9
result:
ok
Test #13:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 8 1 4 10 6 1 6 10 4 10 3 1 9 10 1
output:
3 1 10 4 1 10 6
result:
ok
Test #14:
score: -100
Wrong Answer
time: 1ms
memory: 3692kb
input:
1000 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52 53 ...
output:
3 2 1 3 3 1 2
result:
wrong answer Wrong output - Identical cycle.