Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: Tree-Isomorphism(木の同型性判定)
(graph/tree/tree-isomorphism.hpp)

Depends on

Verified with

Code

#pragma once

#include "../graph-template.hpp"
#include "./centroid.hpp"

/**
 * @brief Tree-Isomorphism(木の同型性判定)
 */
template< typename T >
bool tree_isomorphism(const Graph< T > &a, const Graph< T > &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 > &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] == -1) {
        dep[p.first] = p.second == -1 ? 0 : dep[p.second] + 1;
        for(auto &to : 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 solve = [&](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 >= 0; 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]] < 0) return false;
      }
      if(i == 0) return ord.size() == 1;

      int ptr = 0;
      for(auto &p : ord) {
        if(p.second != 0) 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(solve(a1, b1)) return true;
  if(p.size() == 1) return false;
  auto a2 = get_uku(a, p[1]);
  return solve(a2, b1);
}
#line 2 "graph/tree/tree-isomorphism.hpp"

#line 2 "graph/graph-template.hpp"

/**
 * @brief Graph Template(グラフテンプレート)
 */
template< typename T = int >
struct Edge {
  int from, to;
  T cost;
  int idx;

  Edge() = default;

  Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}

  operator int() const { return to; }
};

template< typename T = int >
struct Graph {
  vector< vector< Edge< T > > > g;
  int es;

  Graph() = default;

  explicit Graph(int n) : g(n), es(0) {}

  size_t size() const {
    return g.size();
  }

  void add_directed_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es++);
  }

  void add_edge(int from, int to, T cost = 1) {
    g[from].emplace_back(from, to, cost, es);
    g[to].emplace_back(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 a, b;
      cin >> a >> b;
      a += padding;
      b += padding;
      T c = T(1);
      if(weighted) cin >> c;
      if(directed) add_directed_edge(a, b, c);
      else add_edge(a, b, c);
    }
  }

  inline vector< Edge< T > > &operator[](const int &k) {
    return g[k];
  }

  inline const vector< Edge< T > > &operator[](const int &k) const {
    return g[k];
  }
};

template< typename T = int >
using Edges = vector< Edge< T > >;
#line 2 "graph/tree/centroid.hpp"

#line 4 "graph/tree/centroid.hpp"

/**
 * @brief Centroid(木の重心)
 * @docs docs/centroid.md
 */
template< typename T >
vector< int > centroid(const Graph< T > &g) {
  const int N = (int) g.size();

  stack< pair< int, int > > st;
  st.emplace(0, -1);
  vector< int > sz(N), par(N);
  while(!st.empty()) {
    auto p = st.top();
    if(sz[p.first] == 0) {
      sz[p.first] = 1;
      for(auto &to : g[p.first]) if(to != p.second) st.emplace(to, p.first);
    } else {
      for(auto &to : 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 &to : 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 5 "graph/tree/tree-isomorphism.hpp"

/**
 * @brief Tree-Isomorphism(木の同型性判定)
 */
template< typename T >
bool tree_isomorphism(const Graph< T > &a, const Graph< T > &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 > &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] == -1) {
        dep[p.first] = p.second == -1 ? 0 : dep[p.second] + 1;
        for(auto &to : 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 solve = [&](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 >= 0; 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]] < 0) return false;
      }
      if(i == 0) return ord.size() == 1;

      int ptr = 0;
      for(auto &p : ord) {
        if(p.second != 0) 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(solve(a1, b1)) return true;
  if(p.size() == 1) return false;
  auto a2 = get_uku(a, p[1]);
  return solve(a2, b1);
}
Back to top page