QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#630516 | #8700. Tree Automorphisms | hos_lyric | WA | 0ms | 4104kb | C++14 | 4.3kb | 2024-10-11 18:58:33 | 2024-10-11 18:58:35 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
int N;
vector<int> A, B;
vector<vector<int>> graph;
vector<int> sz;
void dfsSz(int u, int p) {
for (const int v : graph[u]) if (p != v) {
dfsSz(v, u);
sz[u] += sz[v];
}
}
int H;
map<vector<int>, int> ids;
vector<int> dp;
vector<vector<int>> subtree;
vector<vector<int>> ans;
vector<int> identity() {
vector<int> perm(N);
for (int u = 0; u < N; ++u) perm[u] = u;
return perm;
}
void sw(vector<int> &perm, int u, int v) {
assert(subtree[u].size() == subtree[v].size());
for (int j = 0; j < (int)subtree[u].size(); ++j) {
swap(perm[subtree[u][j]], perm[subtree[v][j]]);
}
}
void dfs(int u, int p) {
vector<pair<int, int>> hvs;
for (const int v : graph[u]) if (p != v) {
dfs(v, u);
hvs.emplace_back(dp[v], v);
}
sort(hvs.begin(), hvs.end());
const int hvsLen = hvs.size();
// cerr<<u<<" "<<p<<": "<<hvs<<endl;
for (int j = 0, k = 0; j < hvsLen; j = k) {
for (; k < hvsLen && hvs[j].first == hvs[k].first; ++k) {}
// permute us[j, k)
if (k - j >= 2) {
auto perm = identity();
sw(perm, hvs[j].second, hvs[j + 1].second);
ans.push_back(perm);
}
if (k - j >= 3) {
auto perm = identity();
for (int l = j; l < k - 1; ++l) sw(perm, hvs[l].second, hvs[l + 1].second);
ans.push_back(perm);
}
}
vector<int> hs(hvsLen);
for (int j = 0; j < hvsLen; ++j) hs[j] = hvs[j].first;
if (!ids.count(hs)) ids[hs] = H++;
dp[u] = ids[hs];
subtree[u] = {u};
for (const auto &hv : hvs) {
const int v = hv.second;
subtree[u].insert(subtree[u].end(), subtree[v].begin(), subtree[v].end());
}
}
int main() {
for (; ~scanf("%d", &N); ) {
A.resize(N - 1);
B.resize(N - 1);
for (int i = 0; i < N - 1; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
graph.assign(N, {});
for (int i = 0; i < N - 1; ++i) {
graph[A[i]].push_back(B[i]);
graph[B[i]].push_back(A[i]);
}
sz.assign(N, 1);
dfsSz(0, -1);
H = 0;
ids.clear();
dp.assign(N, -1);
subtree.assign(N, {});
ans.clear();
for (int u = 0; ; ) {
int vm = -1;
for (const int v : graph[u]) if (!~vm || sz[vm] < sz[v]) {
vm = v;
}
if (~vm && 2 * sz[vm] > sz[u]) {
sz[u] -= sz[vm];
sz[vm] += sz[u];
u = vm;
} else if (~vm && 2 * sz[vm] == sz[u]) {
dfs(u, vm);
dfs(vm, u);
if (dp[u] == dp[vm]) {
auto perm = identity();
swap(perm[u], perm[vm]);
ans.push_back(perm);
}
break;
} else {
dfs(u, -1);
break;
}
}
// cerr<<"subtree = "<<subtree<<endl;
if (!ans.size()) {
ans.push_back(identity());
}
printf("%d\n", (int)ans.size());
for (const auto &perm : ans) {
for (int u = 0; u < N; ++u) {
if (u) printf(" ");
printf("%d", perm[u] + 1);
}
puts("");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4060kb
input:
2 1 2
output:
1 2 1
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 4104kb
input:
3 1 2 1 3
output:
1 1 3 2
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
4 1 2 1 3 1 4
output:
2 1 3 2 4 1 3 4 2
result:
ok ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
5 1 2 1 3 3 4 4 5
output:
1 4 5 3 1 2
result:
ok ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
5 1 2 1 3 1 4 4 5
output:
1 1 3 2 4 5
result:
ok ok
Test #6:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
10 1 2 1 3 1 4 1 9 2 6 2 7 3 10 4 5 6 8
output:
1 1 2 4 3 10 6 7 8 9 5
result:
ok ok
Test #7:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
15 1 2 1 5 1 6 1 7 1 8 2 3 3 4 6 12 6 14 8 9 9 10 9 11 11 13 14 15
output:
1 1 2 3 4 7 6 5 8 9 10 11 12 13 14 15
result:
ok ok
Test #8:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
20 1 2 1 11 1 12 2 3 2 5 2 6 2 14 3 4 3 16 4 7 4 8 4 18 5 13 5 17 6 10 8 9 8 19 9 15 17 20
output:
2 1 2 3 4 5 6 7 8 9 10 12 11 13 14 15 16 17 18 19 20 1 2 3 4 5 6 18 8 9 10 11 12 13 14 15 16 17 7 19 20
result:
ok ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
50 1 2 1 3 1 4 1 13 1 14 1 21 1 27 1 45 2 6 3 23 3 33 4 5 4 36 5 7 5 10 5 35 6 9 6 40 7 8 7 11 7 16 8 48 9 12 10 42 11 17 12 19 12 29 12 47 13 20 13 25 14 15 14 31 15 18 15 28 17 22 17 26 19 38 19 39 20 24 20 30 21 44 23 41 28 34 30 32 33 43 33 50 35 37 38 46 42 49
output:
5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 47 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 29 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 50 44 45 46 47 48 49 43 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #10:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
50 1 2 2 3 3 4 4 5 5 6 5 7 5 13 6 9 6 11 6 42 7 8 7 12 8 10 9 16 10 18 11 14 11 49 12 15 12 21 13 17 13 27 14 32 15 19 15 35 15 45 16 23 16 24 16 37 16 48 18 20 18 22 18 25 19 34 19 38 20 30 20 33 23 26 24 28 24 39 27 29 28 36 30 31 31 43 35 44 38 40 38 41 39 50 40 47 42 46
output:
3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 39 29 30 31 32 33 34 35 50 37 38 28 40 41 42 43 44 45 46 47 48 49 36 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 48 38 39 40 41 42 43 44 45 46 47 37 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #11:
score: 0
Accepted
time: 0ms
memory: 4076kb
input:
50 1 2 1 19 1 22 1 26 2 3 2 5 2 29 2 34 3 4 3 8 3 20 3 30 4 7 4 16 4 18 4 47 5 6 5 39 5 48 6 11 6 33 6 36 6 38 7 10 8 9 8 12 8 17 8 24 8 37 8 49 10 13 12 15 13 14 13 44 15 21 16 28 19 23 19 31 20 42 21 27 23 25 23 41 26 43 27 32 33 35 34 45 36 50 39 40 43 46
output:
7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 41 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 25 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 38 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 11 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #12:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
50 1 2 2 3 3 4 4 5 4 6 6 7 7 8 7 10 8 9 8 11 11 12 11 13 12 14 14 15 14 16 16 17 16 18 16 19 17 20 20 21 21 22 21 24 22 23 22 25 23 26 24 28 25 31 25 32 26 27 27 29 28 30 31 33 32 34 33 40 33 42 34 35 35 36 35 37 35 38 36 39 38 41 38 47 39 50 40 43 40 44 44 45 44 48 45 46 45 49
output:
3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 49 47 48 46 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #13:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
50 1 2 1 7 2 3 2 4 2 5 2 20 2 37 3 12 3 34 5 6 5 8 5 9 5 11 5 13 5 45 6 10 9 14 9 26 9 44 10 25 10 32 12 17 12 31 13 16 13 40 14 15 14 38 15 18 15 46 16 47 18 19 18 28 19 30 20 21 20 22 21 23 21 35 23 24 23 27 23 41 25 29 26 43 28 33 30 39 30 42 31 36 32 48 40 49 47 50
output:
7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 27 25 26 24 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 27 25 26 41 28 29 30 31 32 33 34 35 36 37 38 39 40 24 42 43 44 45 46 47 48 49 50 1 2 3 37 5 6 7 8...
result:
ok ok
Test #14:
score: 0
Accepted
time: 0ms
memory: 3812kb
input:
50 1 2 1 4 1 6 1 10 1 18 1 27 2 3 2 5 2 16 3 17 4 7 4 8 4 26 5 9 5 14 6 11 6 15 7 12 8 19 9 23 9 32 10 22 10 44 12 13 13 25 13 36 14 21 15 20 16 34 17 31 17 35 17 41 18 43 18 46 18 48 19 45 20 24 23 28 25 29 25 30 26 42 29 33 30 37 34 38 35 39 39 40 39 50 45 47 47 49
output:
6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 50 41 42 43 44 45 46 47 48 49 40 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 41 32 33 34 35 36 37 38 39 40 31 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #15:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
50 1 2 1 9 2 3 2 7 2 10 3 4 3 8 3 13 3 18 3 23 3 30 4 5 4 12 4 32 5 6 7 17 9 11 9 25 10 49 11 45 12 14 12 22 12 26 12 35 13 15 13 33 15 16 15 21 15 44 16 19 17 20 17 40 17 47 17 50 18 29 21 27 21 34 22 42 23 24 24 28 24 31 25 41 28 37 28 38 30 43 32 48 34 36 37 39 37 46
output:
8 1 2 3 4 5 6 7 8 9 10 25 12 13 14 15 16 17 18 19 20 21 22 23 24 11 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 45 42 43 44 41 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 40 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 20 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #16:
score: 0
Accepted
time: 0ms
memory: 3700kb
input:
50 1 2 1 4 1 6 1 13 1 27 2 3 2 8 2 9 2 26 2 46 3 7 4 5 4 29 4 34 5 12 5 20 5 23 5 40 6 10 6 21 6 35 8 14 8 18 9 15 9 17 10 11 10 39 13 44 14 16 14 33 14 43 14 50 15 30 16 25 16 32 17 19 17 31 18 22 21 24 21 28 22 45 24 37 24 41 26 47 29 38 29 42 34 36 46 48 47 49
output:
10 1 2 3 4 5 6 7 8 9 10 11 20 13 14 15 16 17 18 19 12 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 20 13 14 15 16 17 18 19 23 21 22 40 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 12 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8...
result:
ok ok
Test #17:
score: 0
Accepted
time: 0ms
memory: 3816kb
input:
50 1 2 1 3 1 7 1 9 2 5 2 14 3 4 3 20 3 22 3 25 3 35 4 6 4 17 4 29 5 32 5 34 5 49 6 13 6 15 6 26 7 8 7 10 8 11 8 12 8 18 8 33 8 47 10 16 10 31 11 23 13 19 13 42 18 21 18 36 20 24 20 28 20 30 22 45 26 27 26 39 27 37 28 38 28 50 32 44 33 40 36 41 37 46 37 48 39 43
output:
10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 49 35 36 37 38 39 40 41 42 43 44 45 46 47 48 34 50 1 2 3 4 5 6 7 8 9 10 11 47 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 12 48 49 50 1 2 3 4 5 6 7 8...
result:
ok ok
Test #18:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
50 1 2 1 3 1 7 2 6 2 13 3 4 3 10 3 12 3 42 4 5 4 9 4 11 5 23 6 8 6 48 7 26 7 30 7 40 8 17 11 15 12 14 12 28 13 19 13 27 14 18 14 32 15 16 15 21 15 33 16 24 16 37 18 20 18 22 18 46 20 43 21 35 23 25 24 31 25 29 25 34 25 39 26 47 27 38 30 45 31 41 35 36 35 50 37 44 45 49
output:
6 1 2 3 4 5 13 7 27 9 10 11 12 6 14 15 16 38 18 48 20 21 22 23 24 25 26 8 28 29 30 31 32 33 34 35 36 37 17 39 40 41 42 43 44 45 46 47 19 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 34 30 31 32 33 29 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #19:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
50 22 46 27 48 19 44 2 47 50 43 39 16 50 42 9 36 25 35 22 24 25 14 2 15 50 20 19 40 25 13 16 10 32 34 45 20 12 42 6 41 34 9 40 45 26 11 7 36 33 3 5 22 15 38 33 8 11 18 1 50 6 16 7 43 23 4 28 30 29 10 2 36 24 31 28 46 50 27 27 39 17 23 18 32 15 35 18 46 10 33 49 37 21 4 37 31 17 44
output:
2 1 2 8 4 5 6 7 3 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
result:
ok ok
Test #20:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
50 22 8 34 36 14 5 32 44 14 47 24 29 24 36 50 46 9 41 46 19 11 47 12 37 20 47 4 38 50 4 5 12 23 24 8 9 43 31 10 46 27 22 39 14 35 28 48 40 48 25 33 22 15 30 21 25 1 38 1 17 42 24 24 27 2 9 44 2 43 6 10 16 7 50 20 15 22 45 18 35 3 13 35 50 36 17 11 48 15 26 11 8 43 29 3 34 21 49
output:
5 1 2 3 4 5 31 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 6 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 28 19 20 21 22 23 24 25 26 27 18 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 ...
result:
ok ok
Test #21:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
50 19 25 38 43 22 1 29 19 38 49 10 1 33 46 3 27 48 11 28 3 42 37 6 45 8 2 13 36 2 7 41 13 20 49 50 33 45 19 47 10 48 34 47 4 46 14 9 47 17 44 1 45 23 24 27 36 44 39 39 19 40 46 12 15 37 26 34 30 48 35 31 18 48 49 32 47 9 8 16 4 37 5 29 46 20 24 13 26 27 31 12 34 2 21 21 28 48 33
output:
3 1 2 3 4 5 6 7 8 9 10 35 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 11 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 1 2 3 4 5 6 7 8 9 10 11 12 13 40 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 14 41 42 43 44 45 46 47 48 49 50 1 2 3 4 42 6 7 8...
result:
ok ok
Test #22:
score: -100
Wrong Answer
time: 0ms
memory: 3816kb
input:
50 38 26 23 10 3 11 12 48 3 22 39 50 43 8 40 19 22 25 45 29 47 28 14 42 2 49 15 1 1 48 36 24 5 13 45 13 37 9 18 6 7 24 23 37 10 32 32 30 36 21 20 38 35 42 26 35 19 34 25 15 27 4 31 16 21 5 17 43 33 39 29 40 44 41 28 16 8 30 7 9 46 50 14 4 2 17 31 41 20 12 6 49 11 33 47 27 34 46
output:
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 50 47 48 49 46
result:
wrong answer Not an automorphism