C++ アルゴリズムとデータ構造のライブラリ
#include "library/graph/tree/tree_isomorphism.hpp"$O(N \log N)$
bool is_same = tree_isomorphism(t, g);
#pragma once
#include "library/graph/base/graph.hpp"
#include "library/graph/tree/centroid.hpp"
bool tree_isomorphism(const Graph &a, const Graph &b) {
if (a.size() != b.size()) return false;
const int N = (int)a.size();
using pvi = pair<vector<int>, vector<int>>;
auto get_uku = [&](const Graph &t, int e) {
stack<pair<int, int>> st;
st.emplace(e, -1);
vector<int> dep(N, -1), par(N);
while (!st.empty()) {
auto p = st.top();
if (dep[p.first] == -1ll) {
dep[p.first] = p.second == -1ll ? 0 : dep[p.second] + 1;
for (auto &&[from, to, cost, idx] : t[p.first]) {
if (to != p.second) st.emplace(to, p.first);
}
} else {
par[p.first] = p.second;
st.pop();
}
}
return make_pair(dep, par);
};
auto judge = [&](const pvi &latte, const pvi &malta) {
int d = *max_element(begin(latte.first), end(latte.first));
if (d != *max_element(begin(malta.first), end(malta.first))) {
return false;
}
vector<vector<int>> latte_d(d + 1), malta_d(d + 1), latte_key(N),
malta_key(N);
for (int i = 0; i < N; i++) latte_d[latte.first[i]].emplace_back(i);
for (int i = 0; i < N; i++) malta_d[malta.first[i]].emplace_back(i);
for (int i = d; i >= 0ll; i--) {
map<vector<int>, int> ord;
for (auto &idx : latte_d[i]) {
sort(begin(latte_key[idx]), end(latte_key[idx]));
ord[latte_key[idx]]++;
}
for (auto &idx : malta_d[i]) {
sort(begin(malta_key[idx]), end(malta_key[idx]));
if (--ord[malta_key[idx]] < 0ll) return false;
}
if (i == 0ll) return ord.size() == 1ll;
int ptr = 0;
for (auto &p : ord) {
if (p.second != 0ll) return false;
p.second = ptr++;
}
for (auto &idx : latte_d[i]) {
latte_key[latte.second[idx]].emplace_back(ord[latte_key[idx]]);
}
for (auto &idx : malta_d[i]) {
malta_key[malta.second[idx]].emplace_back(ord[malta_key[idx]]);
}
}
assert(0);
};
auto p = centroid(a), q = centroid(b);
if (p.size() != q.size()) return false;
auto a1 = get_uku(a, p[0]);
auto b1 = get_uku(b, q[0]);
if (judge(a1, b1)) return true;
if (p.size() == 1ll) return false;
auto a2 = get_uku(a, p[1]);
return judge(a2, b1);
}#line 2 "library/graph/base/edge.hpp"
struct Edge {
int from, to;
long long cost;
int idx;
Edge(int from, int to, long long cost = 1, int idx = -1)
: from(from), to(to), cost(cost), idx(idx) {}
};
#line 3 "library/graph/base/graph.hpp"
struct Graph {
int N;
vector<vector<Edge>> G;
int es;
Graph() = default;
Graph(int N) : N(N), G(N), es(0) {}
const vector<Edge> &operator[](int v) const { return G[v]; }
int size() const { return N; }
void add(int from, int to, long long cost = 1) {
G[from].push_back(Edge(from, to, cost, es++));
}
void add_both(int from, int to, long long cost = 1) {
G[from].push_back(Edge(from, to, cost, es));
G[to].push_back(Edge(to, from, cost, es++));
}
void read(int M, int padding = -1, bool weighted = false,
bool directed = false) {
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u += padding, v += padding;
long long cost = 1ll;
if (weighted) cin >> cost;
if (directed) {
add(u, v, cost);
} else {
add_both(u, v, cost);
}
}
}
};
#line 3 "library/graph/tree/centroid.hpp"
vector<int> centroid(const Graph &G) {
const int N = (int)G.size();
stack<pair<int, int>> st;
st.emplace(0ll, -1ll);
vector<int> sz(N), par(N);
while (!st.empty()) {
auto p = st.top();
if (sz[p.first] == 0ll) {
sz[p.first] = 1;
for (auto &&[from, to, cost, idx] : G[p.first]) {
if (to != p.second) st.emplace(to, p.first);
}
} else {
for (auto &&[from, to, cost, idx] : G[p.first]) {
if (to != p.second) sz[p.first] += sz[to];
}
par[p.first] = p.second;
st.pop();
}
}
vector<int> ret;
int size = N;
for (int i = 0; i < N; ++i) {
int val = N - sz[i];
for (auto &&[from, to, cost, idx] : G[i]) {
if (to != par[i]) val = max(val, sz[to]);
}
if (val < size) size = val, ret.clear();
if (val == size) ret.emplace_back(i);
}
return ret;
}
#line 4 "library/graph/tree/tree_isomorphism.hpp"
bool tree_isomorphism(const Graph &a, const Graph &b) {
if (a.size() != b.size()) return false;
const int N = (int)a.size();
using pvi = pair<vector<int>, vector<int>>;
auto get_uku = [&](const Graph &t, int e) {
stack<pair<int, int>> st;
st.emplace(e, -1);
vector<int> dep(N, -1), par(N);
while (!st.empty()) {
auto p = st.top();
if (dep[p.first] == -1ll) {
dep[p.first] = p.second == -1ll ? 0 : dep[p.second] + 1;
for (auto &&[from, to, cost, idx] : t[p.first]) {
if (to != p.second) st.emplace(to, p.first);
}
} else {
par[p.first] = p.second;
st.pop();
}
}
return make_pair(dep, par);
};
auto judge = [&](const pvi &latte, const pvi &malta) {
int d = *max_element(begin(latte.first), end(latte.first));
if (d != *max_element(begin(malta.first), end(malta.first))) {
return false;
}
vector<vector<int>> latte_d(d + 1), malta_d(d + 1), latte_key(N),
malta_key(N);
for (int i = 0; i < N; i++) latte_d[latte.first[i]].emplace_back(i);
for (int i = 0; i < N; i++) malta_d[malta.first[i]].emplace_back(i);
for (int i = d; i >= 0ll; i--) {
map<vector<int>, int> ord;
for (auto &idx : latte_d[i]) {
sort(begin(latte_key[idx]), end(latte_key[idx]));
ord[latte_key[idx]]++;
}
for (auto &idx : malta_d[i]) {
sort(begin(malta_key[idx]), end(malta_key[idx]));
if (--ord[malta_key[idx]] < 0ll) return false;
}
if (i == 0ll) return ord.size() == 1ll;
int ptr = 0;
for (auto &p : ord) {
if (p.second != 0ll) return false;
p.second = ptr++;
}
for (auto &idx : latte_d[i]) {
latte_key[latte.second[idx]].emplace_back(ord[latte_key[idx]]);
}
for (auto &idx : malta_d[i]) {
malta_key[malta.second[idx]].emplace_back(ord[malta_key[idx]]);
}
}
assert(0);
};
auto p = centroid(a), q = centroid(b);
if (p.size() != q.size()) return false;
auto a1 = get_uku(a, p[0]);
auto b1 = get_uku(b, q[0]);
if (judge(a1, b1)) return true;
if (p.size() == 1ll) return false;
auto a2 = get_uku(a, p[1]);
return judge(a2, b1);
}