グラフテンプレート
(graph/graph-template.hpp)
- View this file on GitHub
- Last update: 2024-05-03 23:21:26+09:00
- Include:
#include "graph/graph-template.hpp"
グラフテンプレート
構造体・型エイリアス
名前 | 説明 |
---|---|
edge<T> |
型T の重みが付いた辺 |
Edges<T> |
型T の重みが付いた辺のリスト |
WeightedGraph<T> |
型T の重みが付いた(有向/無向)グラフ |
UnweightedGraph |
重みなし(有向/無向)グラフ |
入力用関数
-
graph(int N, int M = -1, bool is_directed = false, bool is_1origin = true)
重みなしのグラフを標準入力から読み込み、
UnweightedGraph
型で返す。引数 説明 N
グラフの頂点の個数。 M
グラフの辺の個数。$-1$を設定すると$N-1$で置き換えられる。 is_directed
false
なら、入力された辺の逆辺も追加する。自己ループは$2$回追加されることになる。is_1origin
true
なら頂点番号は$1$-indexed、false
なら$0$-indexedとして入力を読み込む。どちらにしても返り値では頂点番号は$0$-originとする。以下の入力形式に対応する。
辺iは、頂点u[i]から頂点v[i]へ向かう。
u[0] v[0] u[1] v[1] : u[M-1] v[M-1]
-
wgraph<T>(int N, int M = -1, bool is_directed = false, bool is_1origin = true)
型
T
の重みが付いた辺を持つグラフを標準入力から読み込み、WeightedGraph
型で返す。引数 説明 N
グラフの頂点の個数。 M
グラフの辺の個数。$-1$を設定すると$N-1$で置き換えられる。 is_directed
false
なら、入力された辺の逆辺も追加する。自己ループは$2$回追加されることになる。is_1origin
true
なら頂点番号は$1$-indexed、false
なら$0$-indexedとして入力を読み込む。どちらにしても返り値では頂点番号は$0$-originとする。以下の入力形式に対応する。
辺iは、頂点u[i]から頂点v[i]へ向かい、その重みは c[i] である。
u[0] v[0] c[0] u[1] v[1] c[1] : u[M-1] v[M-1] c[M-1]
-
esgraph<T>(int N, int M = -1, bool is_weighted = false, bool is_1origin = true)
型
T
の重みが付いた辺のリストを標準入力から読み込み、Edges
型で返す。引数 説明 N
グラフの頂点の個数。 M
グラフの辺の個数。$-1$を設定すると$N-1$で置き換えられる。 is_weighted
true
のなら、重みを入力する。is_1origin
true
なら頂点番号は$1$-indexed、false
なら$0$-indexedとして入力を読み込む。どちらにしても返り値では頂点番号は$0$-originとする。以下の入力形式に対応する。
辺iは、頂点u[i]から頂点v[i]へ向かい、その重みは c[i] である。重みなしの場合は c[i] は空とする。
u[0] v[0] c[0] u[1] v[1] c[1] : u[M-1] v[M-1] c[M-1]
-
adjgraph<T>(int N, int M, T INF, bool is_weighted = true, bool is_directed = false, bool is_1origin = true)
型
T
の重みが付いた辺のリストを標準入力から読み込み、隣接行列としてvector<vector<T>>
型で返す。返り値をA
とすると、A[i][j]
は頂点i
から頂点j
へ向かう辺の重みである。入力されなかった辺の重みにはINF
が設定される。引数 説明 N
グラフの頂点の個数。 M
グラフの辺の個数。$-1$を設定すると$N-1$で置き換えられる。 INF
入力されなかった辺に設定される重み。 is_directed
false
なら、入力された辺の逆辺も追加する。is_weighted
true
なら、重みを入力する。false
なら、入力された辺の重みは1とする。is_1origin
true
なら頂点番号は$1$-indexed、false
なら$0$-indexedとして入力を読み込む。どちらにしても返り値では頂点番号は$0$-originとする。以下の入力形式に対応する。
辺iは、頂点u[i]から頂点v[i]へ向かい、その重みは c[i] である。重みを入力しない場合は c[i] は空とする。
u[0] v[0] c[0] u[1] v[1] c[1] : u[M-1] v[M-1] c[M-1]
同じ辺が複数回与えらえる場合、後から入力されるもので上書きされる。
Required by
二重頂点連結分解 (graph/biconnected-components.hpp)
閉路の検出 (graph/cycle-detection.hpp)
graph/functional-graph.hpp
グラフユーティリティ (graph/graph-utility.hpp)
graph/kruskal.hpp
graph/lowlink.hpp
graph/minimum-cost-arborescence.hpp
Functional Graph(なもりグラフ)の分解 (graph/namori.hpp)
graph/strongly-connected-components.hpp
graph/topological-sort.hpp
graph/two-edge-connected-components.hpp
Grundy Number (math/grundy-number.hpp)
shortest-path/bellman-ford.hpp
shortest-path/bfs01.hpp
ダイクストラ法(Radix Heap) (shortest-path/dijkstra-radix-heap.hpp)
ダイクストラ法(復元付き) (shortest-path/dijkstra-with-restore.hpp)
ダイクストラ法 (shortest-path/dijkstra.hpp)
牛ゲー(最短路問題の双対) (shortest-path/dual-of-shortest-path.hpp)
shortest-path/restore-shortest-path.hpp
shortest-path/warshall-floyd.hpp
Auxiliary Tree (tree/auxiliary-tree.hpp)
Block Cut Tree (tree/block-cut-tree.hpp)
Cartesian Tree (tree/cartesian-tree.hpp)
根付き木・逆辺からなる木への変換 (tree/convert-tree.hpp)
DSU on Tree(Guni) (tree/dsu-on-tree.hpp)
tree/dynamic-diameter.hpp
オイラーツアー (tree/euler-tour.hpp)
Heavy Light Decomposition(重軽分解) (tree/heavy-light-decomposition.hpp)
Rerooting(全方位木DP) (tree/rerooting.hpp)
tree/static-top-tree-edge-based.hpp
Static Top Tree (tree/static-top-tree-vertex-based.hpp)
木に対する一般的なクエリ (tree/tree-query.hpp)
Verified with
verify/verify-aoj-dsl/aoj-dsl-3-d-cartesiantree.test.cpp
verify/verify-aoj-grl/aoj-grl-1-a-radix-heap.test.cpp
verify/verify-aoj-grl/aoj-grl-1-a.test.cpp
verify/verify-aoj-grl/aoj-grl-1-b.test.cpp
verify/verify-aoj-grl/aoj-grl-1-c.test.cpp
verify/verify-aoj-grl/aoj-grl-2-a.test.cpp
verify/verify-aoj-grl/aoj-grl-3-a.test.cpp
verify/verify-aoj-grl/aoj-grl-3-b.test.cpp
verify/verify-aoj-grl/aoj-grl-3-c.test.cpp
verify/verify-aoj-grl/aoj-grl-4-a.test.cpp
verify/verify-aoj-grl/aoj-grl-4-b.test.cpp
verify/verify-aoj-grl/aoj-grl-5-a-rerooting.test.cpp
verify/verify-aoj-grl/aoj-grl-5-a.test.cpp
verify/verify-aoj-grl/aoj-grl-5-b.test.cpp
verify/verify-aoj-grl/aoj-grl-5-c.test.cpp
verify/verify-aoj-grl/aoj-grl-5-d.test.cpp
verify/verify-aoj-grl/aoj-grl-5-e.test.cpp
verify/verify-aoj-other/aoj-0304.test.cpp
verify/verify-aoj-other/aoj-2171-bigrational.test.cpp
verify/verify-aoj-other/aoj-2171.test.cpp
verify/verify-aoj-other/aoj-2821.test.cpp
verify/verify-aoj-other/aoj-2891-2.test.cpp
verify/verify-aoj-other/aoj-2891.test.cpp
verify/verify-aoj-other/aoj-2945-01bfs.test.cpp
verify/verify-aoj-other/aoj-2995-hashmap.test.cpp
verify/verify-aoj-other/aoj-2995.test.cpp
verify/verify-aoj-other/aoj-3022.test.cpp
verify/verify-aoj-other/aoj-3506.test.cpp
verify/verify-unit-test/dijkstra.test.cpp
verify/verify-unit-test/dynamic-diameter.test.cpp
verify/verify-unit-test/rerooting.test.cpp
verify/verify-unit-test/semiring.test.cpp
verify/verify-unit-test/tree-path.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-path-sum-euler-tour.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-path-sum.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-subtree-sum-dst-on-tree.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-subtree-sum-euler-tree.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-add-subtree-sum.test.cpp
verify/verify-yosupo-ds/yosupo-vertex-set-path-composite.test.cpp
verify/verify-yosupo-graph/yosupo-cartesian.test.cpp
verify/verify-yosupo-graph/yosupo-chromatic-number.test.cpp
verify/verify-yosupo-graph/yosupo-cycle-detection.test.cpp
verify/verify-yosupo-graph/yosupo-diameter.test.cpp
verify/verify-yosupo-graph/yosupo-directed-mst.test.cpp
verify/verify-yosupo-graph/yosupo-frequency-table-of-tree-distance.test.cpp
verify/verify-yosupo-graph/yosupo-jump-on-tree.test.cpp
verify/verify-yosupo-graph/yosupo-lowest-common-ancestor-euler-tour.test.cpp
verify/verify-yosupo-graph/yosupo-lowest-common-ancestor-tree-util.test.cpp
verify/verify-yosupo-graph/yosupo-lowest-common-ancestor.test.cpp
verify/verify-yosupo-graph/yosupo-point-set-tree-path-composite-sum-fixed-root.test.cpp
verify/verify-yosupo-graph/yosupo-shortest-path-2.test.cpp
verify/verify-yosupo-graph/yosupo-shortest-path-dijkstra-abstruct.test.cpp
verify/verify-yosupo-graph/yosupo-shortest-path.test.cpp
verify/verify-yosupo-graph/yosupo-strongly-connected-components.test.cpp
verify/verify-yosupo-graph/yosupo-tree-path-composite-sum.test.cpp
verify/verify-yosupo-graph/yosupo-two-edge-cc.test.cpp
verify/verify-yuki/yuki-0103.test.cpp
verify/verify-yuki/yuki-1254-2.test.cpp
verify/verify-yuki/yuki-1254.test.cpp
verify/verify-yuki/yuki-1320.test.cpp
verify/verify-yuki/yuki-1326.test.cpp
verify/verify-yuki/yuki-1777.test.cpp
verify/verify-yuki/yuki-1778.test.cpp
verify/verify-yuki/yuki-1789.test.cpp
verify/verify-yuki/yuki-2588.test.cpp
Code
#pragma once
template <typename T>
struct edge {
int src, to;
T cost;
edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;
// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
UnweightedGraph g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
if (is_1origin) x--, y--;
g[x].push_back(y);
if (!is_directed) g[y].push_back(x);
}
return g;
}
// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
WeightedGraph<T> g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
cin >> c;
if (is_1origin) x--, y--;
g[x].emplace_back(x, y, c);
if (!is_directed) g[y].emplace_back(y, x, c);
}
return g;
}
// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
es.emplace_back(x, y, c);
}
return es;
}
// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
bool is_directed = false, bool is_1origin = true) {
vector<vector<T>> d(N, vector<T>(N, INF));
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
d[x][y] = c;
if (!is_directed) d[y][x] = c;
}
return d;
}
/**
* @brief グラフテンプレート
* @docs docs/graph/graph-template.md
*/
#line 2 "graph/graph-template.hpp"
template <typename T>
struct edge {
int src, to;
T cost;
edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;
// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
UnweightedGraph g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
if (is_1origin) x--, y--;
g[x].push_back(y);
if (!is_directed) g[y].push_back(x);
}
return g;
}
// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
WeightedGraph<T> g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
cin >> c;
if (is_1origin) x--, y--;
g[x].emplace_back(x, y, c);
if (!is_directed) g[y].emplace_back(y, x, c);
}
return g;
}
// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
es.emplace_back(x, y, c);
}
return es;
}
// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
bool is_directed = false, bool is_1origin = true) {
vector<vector<T>> d(N, vector<T>(N, INF));
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
d[x][y] = c;
if (!is_directed) d[y][x] = c;
}
return d;
}
/**
* @brief グラフテンプレート
* @docs docs/graph/graph-template.md
*/