QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#630491 | #8700. Tree Automorphisms | hos_lyric | WA | 0ms | 3840kb | C++14 | 3.8kb | 2024-10-11 18:50:04 | 2024-10-11 18:50:04 |
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>> ans;
vector<int> identity() {
vector<int> perm(N);
for (int u = 0; u < N; ++u) perm[u] = u;
return perm;
}
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();
swap(perm[hvs[j].second], perm[hvs[j + 1].second]);
ans.push_back(perm);
}
if (k - j >= 3) {
auto perm = identity();
for (int l = j; l < k - 1; ++l) swap(perm[hvs[l].second], perm[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];
}
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);
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;
}
}
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3792kb
input:
2 1 2
output:
1 2 1
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
3 1 2 1 3
output:
1 1 3 2
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3748kb
input:
4 1 2 1 3 1 4
output:
2 1 3 2 4 1 3 4 2
result:
ok ok
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 3804kb
input:
5 1 2 1 3 3 4 4 5
output:
1 4 2 3 1 5
result:
wrong answer Not an automorphism