This documentation is automatically generated by competitive-verifier/competitive-verifier
#include "graph/others/enumerate-triangles.hpp"グラフの triangle、つまり互いに辺で隣接している $3$ 頂点を列挙する。
triangle の個数は高々 $M \sqrt {2M}$ 個である。それぞれの辺を次数が小さい頂点から大きい頂点に向かうように向きづけすると、DAG になる。このときどの頂点の出次数も $\sqrt {2M}$ を超えないことを示せる。その上で、各辺についてその始点と終点から同じ頂点に辺が出ているかどうか調べれば良い。
vector<tuple<int, int, int> > enumerate_triangles(const Graph<T>& g)
グラフ g の triangle を全て返す。
$M$: 辺の本数
#pragma once
#include <tuple>
#include <utility>
#include <vector>
#include "../graph-template.hpp"
/**
* @brief Enumerate Triangles(三角形全列挙)
*
*/
template <typename T>
std::vector<std::tuple<int, int, int> > enumerate_triangles(const Graph<T>& g) {
int N = (int)g.size();
using pi = std::pair<int, int>;
std::vector<pi> vp(N);
for (int i = 0; i < N; i++) {
vp[i] = {(int)g[i].size(), i};
}
std::vector<std::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);
}
}
}
std::vector<std::tuple<int, int, int> > triangle;
std::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"
#include <tuple>
#include <utility>
#include <vector>
#line 2 "graph/graph-template.hpp"
#include <cstddef>
#include <iostream>
#line 6 "graph/graph-template.hpp"
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 {
std::vector<std::vector<Edge<T> > > g;
int es;
Graph() = default;
explicit Graph(int n) : g(n), es(0) {}
std::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;
std::cin >> a >> b;
a += padding;
b += padding;
T c = T(1);
if (weighted) std::cin >> c;
if (directed)
add_directed_edge(a, b, c);
else
add_edge(a, b, c);
}
}
inline std::vector<Edge<T> >& operator[](const int& k) { return g[k]; }
inline const std::vector<Edge<T> >& operator[](const int& k) const {
return g[k];
}
};
template <typename T = int>
using Edges = std::vector<Edge<T> >;
#line 8 "graph/others/enumerate-triangles.hpp"
/**
* @brief Enumerate Triangles(三角形全列挙)
*
*/
template <typename T>
std::vector<std::tuple<int, int, int> > enumerate_triangles(const Graph<T>& g) {
int N = (int)g.size();
using pi = std::pair<int, int>;
std::vector<pi> vp(N);
for (int i = 0; i < N; i++) {
vp[i] = {(int)g[i].size(), i};
}
std::vector<std::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);
}
}
}
std::vector<std::tuple<int, int, int> > triangle;
std::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;
}