Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: Enumerate Triangles(三角形全列挙) (graph/others/enumerate-triangles.hpp)

概要

グラフの triangle, つまり互いに辺で隣接している $3$ 頂点を列挙する.

triangle の個数は高々 $M \sqrt {2M}$ 個である. それぞれの辺を次数が小さい頂点から大きい頂点に向かうように向きづけすると, DAG になる. このときどの頂点の出次数も $\sqrt {2M}$ を超えないことを示せる. その上で, 各辺についてその始点と終点から同じ頂点に辺が出ているかどうか調べれば良い.

使い方

計算量

$M$: 辺の本数

Depends on

Verified with

Code

#pragma once

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

/**
 * @brief Enumerate Triangles(三角形全列挙)
 *
 */
template <typename T>
vector<tuple<int, int, int> > enumerate_triangles(const Graph<T> &g) {
  int N = (int)g.size();
  using pi = pair<int, int>;
  vector<pi> vp(N);
  for (int i = 0; i < N; i++) {
    vp[i] = {(int)g[i].size(), i};
  }
  vector<vector<int> > h(N);
  for (int i = 0; i < N; i++) {
    for (auto &j : g[i]) {
      if (vp[i] > vp[j]) {
        h[i].emplace_back(j);
      }
    }
  }
  vector<tuple<int, int, int> > triangle;
  vector<int> used(N);
  for (int x = 0; x < N; x++) {
    for (int z : h[x]) used[z] = true;
    for (int y : h[x]) {
      for (int z : h[y]) {
        if (used[z]) triangle.emplace_back(x, y, z);
      }
    }
    for (int z : h[x]) used[z] = false;
  }

  return triangle;
}
#line 2 "graph/others/enumerate-triangles.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 4 "graph/others/enumerate-triangles.hpp"

/**
 * @brief Enumerate Triangles(三角形全列挙)
 *
 */
template <typename T>
vector<tuple<int, int, int> > enumerate_triangles(const Graph<T> &g) {
  int N = (int)g.size();
  using pi = pair<int, int>;
  vector<pi> vp(N);
  for (int i = 0; i < N; i++) {
    vp[i] = {(int)g[i].size(), i};
  }
  vector<vector<int> > h(N);
  for (int i = 0; i < N; i++) {
    for (auto &j : g[i]) {
      if (vp[i] > vp[j]) {
        h[i].emplace_back(j);
      }
    }
  }
  vector<tuple<int, int, int> > triangle;
  vector<int> used(N);
  for (int x = 0; x < N; x++) {
    for (int z : h[x]) used[z] = true;
    for (int y : h[x]) {
      for (int z : h[y]) {
        if (used[z]) triangle.emplace_back(x, y, z);
      }
    }
    for (int z : h[x]) used[z] = false;
  }

  return triangle;
}
Back to top page