This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/rooted_tree_isomorphism_classification"
#include "../tree_isomorphism.hpp"
#include "../../utilities/groupby.hpp"
#include "../../modint.hpp"
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
int n;
cin >> n;
tree_isomorphism<ModInt998244353> tree(n);
for (int i = 1, p = -1; i < n; ++i) tree.add_edge(i, (cin >> p, p));
tree.build_hash(0, 15319159, 181263479);
vector<pair<tree_isomorphism<ModInt998244353>::DoubleHash, int>> info;
for (int i = 0; i < n; ++i) info.emplace_back(tree.hash_subtree.at(i), i);
const auto gs = groupby(info);
vector<int> ret(n);
for (int g = 0; g < int(gs.size()); ++g) {
for (int i : gs.at(g).second) ret.at(i) = g;
}
cout << gs.size() << '\n';
for (int i = 0; i < n; ++i) cout << ret.at(i) << (i + 1 == n ? '\n' : ' ');
}
#line 1 "tree/test/subtree_isomorphism.lc.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/rooted_tree_isomorphism_classification"
#line 2 "tree/tree_isomorphism.hpp"
#include <chrono>
#include <utility>
#include <vector>
// Tree isomorphism with hashing (ハッシュによる木の同型判定)
// Dependence: ModInt or ModIntRuntime
// Reference: https://snuke.hatenablog.com/entry/2017/02/03/054210
// Verified: https://atcoder.jp/contests/nikkei2019-2-final/submissions/9044698 (ModInt)
// https://atcoder.jp/contests/nikkei2019-2-final/submissions/9044745 (ModIntRuntime)
template <typename ModInt> struct tree_isomorphism {
using DoubleHash = std::pair<ModInt, ModInt>;
using Edges = std::vector<std::vector<int>>; // vector<set<int>>;
int V;
Edges e;
tree_isomorphism(int v) : V(v), e(v) {}
void add_edge(int u, int v) {
e[u].emplace_back(v);
e[v].emplace_back(u);
}
static uint64_t splitmix64(uint64_t x) {
// https://codeforces.com/blog/entry/62393 http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
DoubleHash get_hash(DoubleHash x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return {splitmix64(x.first.val() + FIXED_RANDOM), splitmix64(x.second.val() + FIXED_RANDOM)};
}
static void add_hash(DoubleHash &l, const DoubleHash &r) {
l.first += r.first, l.second += r.second;
}
static DoubleHash subtract_hash(const DoubleHash &l, const DoubleHash &r) {
return {l.first - r.first, l.second - r.second};
}
std::vector<DoubleHash> hash; // hash of the tree, each node regarded as root
std::vector<DoubleHash> hash_subtree; // hash of the subtree
std::vector<DoubleHash> hash_par; // hash of the subtree whose root is parent[i], not containing i
DoubleHash hash_p; // \in [1, hmod), should be set randomly
DoubleHash hash_dfs1_(int now, int prv) {
hash_subtree[now] = hash_p;
for (auto nxt : e[now]) {
if (nxt != prv) add_hash(hash_subtree[now], hash_dfs1_(nxt, now));
}
return get_hash(hash_subtree[now]);
}
void hash_dfs2_(int now, int prv) {
add_hash(hash[now], hash_subtree[now]);
if (prv >= 0) hash_par[now] = subtract_hash(hash[prv], get_hash(hash_subtree[now]));
for (auto nxt : e[now])
if (nxt != prv) {
DoubleHash tmp = subtract_hash(hash[now], get_hash(hash_subtree[nxt]));
add_hash(hash[nxt], get_hash(tmp));
hash_dfs2_(nxt, now);
}
}
void build_hash(int root, int p1, int p2) {
hash_p = std::make_pair(p1, p2);
hash.resize(V), hash_subtree.resize(V), hash_par.resize(V);
hash_dfs1_(root, -1);
hash_dfs2_(root, -1);
}
};
#line 2 "utilities/groupby.hpp"
#include <algorithm>
#line 5 "utilities/groupby.hpp"
// Key class must be comparable
// Example: [(2, 3), (1, 5), (2, 2), (2, 3), (1, 4)] -> [(1, [5, 4]), (2, [3, 2, 3])]
// Verified: https://atcoder.jp/contests/abc259/tasks/abc259_h
template <class Key, class Value>
std::vector<std::pair<Key, std::vector<Value>>> groupby(std::vector<std::pair<Key, Value>> xs) {
using KV = std::pair<Key, Value>;
std::stable_sort(
xs.begin(), xs.end(), [&](const KV &l, const KV &r) { return l.first < r.first; });
std::vector<std::pair<Key, std::vector<Value>>> ret;
for (const auto &x : xs) {
if (ret.empty() or ret.back().first != x.first) {
ret.emplace_back(x.first, std::vector<Value>());
}
ret.back().second.push_back(x.second);
}
return ret;
}
#line 2 "modint.hpp"
#include <cassert>
#include <iostream>
#include <set>
#line 6 "modint.hpp"
template <int md> struct ModInt {
using lint = long long;
constexpr static int mod() { return md; }
static int get_primitive_root() {
static int primitive_root = 0;
if (!primitive_root) {
primitive_root = [&]() {
std::set<int> fac;
int v = md - 1;
for (lint i = 2; i * i <= v; i++)
while (v % i == 0) fac.insert(i), v /= i;
if (v > 1) fac.insert(v);
for (int g = 1; g < md; g++) {
bool ok = true;
for (auto i : fac)
if (ModInt(g).pow((md - 1) / i) == 1) {
ok = false;
break;
}
if (ok) return g;
}
return -1;
}();
}
return primitive_root;
}
int val_;
int val() const noexcept { return val_; }
constexpr ModInt() : val_(0) {}
constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
constexpr ModInt(lint v) { _setval(v % md + md); }
constexpr explicit operator bool() const { return val_ != 0; }
constexpr ModInt operator+(const ModInt &x) const {
return ModInt()._setval((lint)val_ + x.val_);
}
constexpr ModInt operator-(const ModInt &x) const {
return ModInt()._setval((lint)val_ - x.val_ + md);
}
constexpr ModInt operator*(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.val_ % md);
}
constexpr ModInt operator/(const ModInt &x) const {
return ModInt()._setval((lint)val_ * x.inv().val() % md);
}
constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
constexpr bool operator<(const ModInt &x) const {
return val_ < x.val_;
} // To use std::map<ModInt, T>
friend std::istream &operator>>(std::istream &is, ModInt &x) {
lint t;
return is >> t, x = ModInt(t), is;
}
constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
return os << x.val_;
}
constexpr ModInt pow(lint n) const {
ModInt ans = 1, tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
static constexpr int cache_limit = std::min(md, 1 << 21);
static std::vector<ModInt> facs, facinvs, invs;
constexpr static void _precalculation(int N) {
const int l0 = facs.size();
if (N > md) N = md;
if (N <= l0) return;
facs.resize(N), facinvs.resize(N), invs.resize(N);
for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
facinvs[N - 1] = facs.back().pow(md - 2);
for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
}
constexpr ModInt inv() const {
if (this->val_ < cache_limit) {
if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return invs[this->val_];
} else {
return this->pow(md - 2);
}
}
constexpr ModInt fac() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facs[this->val_];
}
constexpr ModInt facinv() const {
while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
return facinvs[this->val_];
}
constexpr ModInt doublefac() const {
lint k = (this->val_ + 1) / 2;
return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
: ModInt(k).fac() * ModInt(2).pow(k);
}
constexpr ModInt nCr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv() * ModInt(r).facinv();
}
constexpr ModInt nPr(int r) const {
if (r < 0 or this->val_ < r) return ModInt(0);
return this->fac() * (*this - r).facinv();
}
static ModInt binom(int n, int r) {
static long long bruteforce_times = 0;
if (r < 0 or n < r) return ModInt(0);
if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r);
r = std::min(r, n - r);
ModInt ret = ModInt(r).facinv();
for (int i = 0; i < r; ++i) ret *= n - i;
bruteforce_times += r;
return ret;
}
// Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
// Complexity: O(sum(ks))
template <class Vec> static ModInt multinomial(const Vec &ks) {
ModInt ret{1};
int sum = 0;
for (int k : ks) {
assert(k >= 0);
ret *= ModInt(k).facinv(), sum += k;
}
return ret * ModInt(sum).fac();
}
// Catalan number, C_n = binom(2n, n) / (n + 1)
// C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
// https://oeis.org/A000108
// Complexity: O(n)
static ModInt catalan(int n) {
if (n < 0) return ModInt(0);
return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv();
}
ModInt sqrt() const {
if (val_ == 0) return 0;
if (md == 2) return val_;
if (pow((md - 1) / 2) != 1) return 0;
ModInt b = 1;
while (b.pow((md - 1) / 2) == 1) b += 1;
int e = 0, m = md - 1;
while (m % 2 == 0) m >>= 1, e++;
ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
x *= (*this);
ModInt z = b.pow(m);
while (y != 1) {
int j = 0;
ModInt t = y;
while (t != 1) j++, t *= t;
z = z.pow(1LL << (e - j - 1));
x *= z, z *= z, y *= z;
e = j;
}
return ModInt(std::min(x.val_, md - x.val_));
}
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};
using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 8 "tree/test/subtree_isomorphism.lc.test.cpp"
using namespace std;
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
int n;
cin >> n;
tree_isomorphism<ModInt998244353> tree(n);
for (int i = 1, p = -1; i < n; ++i) tree.add_edge(i, (cin >> p, p));
tree.build_hash(0, 15319159, 181263479);
vector<pair<tree_isomorphism<ModInt998244353>::DoubleHash, int>> info;
for (int i = 0; i < n; ++i) info.emplace_back(tree.hash_subtree.at(i), i);
const auto gs = groupby(info);
vector<int> ret(n);
for (int g = 0; g < int(gs.size()); ++g) {
for (int i : gs.at(g).second) ret.at(i) = g;
}
cout << gs.size() << '\n';
for (int i = 0; i < n; ++i) cout << ret.at(i) << (i + 1 == n ? '\n' : ' ');
}