Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/library_checker/tree/rooted_tree_isomorphism_classification.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/rooted_tree_isomorphism_classification"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/tree/rooted_tree_hash.hpp"
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    rep(i, 1, n) {
        int p;
        cin >> p;
        g.add_edge(i, p);
    }
    vector<ll> h = rooted_tree_hash(g, 0);
    unordered_map<ll, int> mp;
    int idx = 0;
    rep(i, 0, n) {
        if(mp.find(h[i]) == mp.end()) {
            mp[h[i]] = idx++;
        }
    }
    cout << idx << '\n';
    rep(i, 0, n) {
        cout << mp[h[i]] << " \n"[i + 1 == n];
    }
}
#line 1 "verify/library_checker/tree/rooted_tree_isomorphism_classification.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/rooted_tree_isomorphism_classification"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 3 "src/template/modint_2_61m1.hpp"
struct Modint_2_61m1 {
    using mint = Modint_2_61m1;
    using u64 = uint64_t;
    using u128 = __uint128_t;
    static constexpr u64 mod() {
        return m;
    }
    static constexpr mint raw(const u64 v) {
        mint a;
        a._v = v;
        return a;
    }
    constexpr Modint_2_61m1()
        : _v(0) {}
    template <class T>
    constexpr Modint_2_61m1(const T& v) {
        static_assert(is_integral_v<T>);
        if constexpr(is_signed_v<T>) {
            int64_t x = int64_t(v % int64_t(m));
            if(x < 0) x += m;
            _v = u64(x);
        } else _v = u64(v % m);
    }
    constexpr u64 val() const {
        return _v;
    }
    constexpr mint& operator++() {
        return *this += 1;
    }
    constexpr mint& operator--() {
        return *this -= 1;
    }
    constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr mint& operator+=(mint rhs) {
        if(_v >= m - rhs._v) _v -= m;
        _v += rhs._v;
        return *this;
    }
    constexpr mint& operator-=(mint rhs) {
        if(_v < rhs._v) _v += m;
        _v -= rhs._v;
        return *this;
    }
    constexpr mint& operator*=(mint rhs) {
        return *this = *this * rhs;
    }
    constexpr mint& operator/=(mint rhs) {
        return *this *= rhs.inv();
    }
    constexpr mint operator+() const {
        return *this;
    }
    constexpr mint operator-() const {
        return mint{} - *this;
    }
    constexpr mint pow(u64 n) const {
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    constexpr mint inv() const {
        assert(_v);
        return pow(m - 2);
    }
    friend constexpr mint operator+(mint lhs, mint rhs) {
        return lhs += rhs;
    }
    friend constexpr mint operator-(mint lhs, mint rhs) {
        return lhs -= rhs;
    }
    friend constexpr mint operator*(mint lhs, mint rhs) {
        return raw(modulo(u128(lhs._v) * rhs._v));
    }
    friend constexpr mint operator/(mint lhs, mint rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(mint lhs, mint rhs) {
        return lhs._v == rhs._v;
    }
    friend constexpr bool operator!=(mint lhs, mint rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    static constexpr u64 m = (1ull << 61) - 1;
    u64 _v = 0;
    inline static constexpr u64 modulo(const u128& x) {
        const u64 val = (x >> 61) + (x & m);
        return val >= m ? val - m : val;
    }
};
#line 5 "src/tree/rooted_tree_hash.hpp"
template <typename T>
vector<ll> rooted_tree_hash(const Graph<T>& g, const int root = 0) {
    const int n = g.size();
    assert(0 <= root and root < n);
    static mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
    static vector<Modint_2_61m1> hash;
    while((int)hash.size() < n) {
        static uniform_int_distribution<unsigned long long> dist(0, Modint_2_61m1::mod() - 1);
        hash.emplace_back(dist(mt));
    }
    vector<ll> res(n);
    auto dfs = [&](const auto& dfs, const int cur, const int par) -> int {
        int depth = 0;
        for(const auto& e : g[cur]) {
            if(e.to == par) continue;
            depth = max(depth, dfs(dfs, e.to, cur) + 1);
        }
        Modint_2_61m1 h = 1, r = hash[depth];
        for(const auto& e : g[cur]) {
            if(e.to == par) continue;
            h *= (r + res[e.to]);
        }
        res[cur] = h.val();
        return depth;
    };
    dfs(dfs, root, -1);
    return res;
}
#line 5 "verify/library_checker/tree/rooted_tree_isomorphism_classification.test.cpp"
int main(void) {
    int n;
    cin >> n;
    Graph<int> g(n);
    rep(i, 1, n) {
        int p;
        cin >> p;
        g.add_edge(i, p);
    }
    vector<ll> h = rooted_tree_hash(g, 0);
    unordered_map<ll, int> mp;
    int idx = 0;
    rep(i, 0, n) {
        if(mp.find(h[i]) == mp.end()) {
            mp[h[i]] = idx++;
        }
    }
    cout << idx << '\n';
    rep(i, 0, n) {
        cout << mp[h[i]] << " \n"[i + 1 == n];
    }
}
Back to top page