Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: Graph
(src/graph/graph_template.hpp)

Edge

グラフの辺の情報を保持する構造体です.

グラフの辺の重みを T とします.
重みなしグラフを扱う場合も,重み $1$ の重み付きグラフと考えてください.

コンストラクタ

(1) Edge<T = int> e()
(2) Edge<T = int> e(int from, int to, T cost = 1, int idx = -1)

計算量

メンバ変数

(1) int e.from
(2) int e.to
(3) T e.cost
(4) int e.idx

operator int()

int(e)

有向辺 eint 型でキャストすると, e.to を返します.

計算量

Tips

辺集合 vector<Edge<T>> を扱いたいときは, Edges というエイリアスが用意されています.

Graph

グラフを扱う構造体です.

グラフの辺の重みを T とします.
重みなしグラフを扱う場合も,重み $1$ の重み付きグラフと考えてください.

コンストラクタ

Graph<T = int> g(int n)

計算量

size

int g.size()

グラフ g の頂点数を返します.

計算量

edge_size

int g.edge_size()

グラフ g の辺数を返します.

計算量

add_edge

void g.add_edge(int from, int to, T cost = 1)

頂点 from と頂点 to を結ぶ重み cost の無向辺を追加します.

辺のラベルはグラフに追加した順番と一致します.
from → to の辺と to → from の辺のラベルは一致します.

また,メンバ変数の辺数は $1$ 本だけ増えます.

制約

計算量

add_directed_edge

void g.add_directed_edge(int from, int to, T cost = 1)

頂点 from から頂点 to への重み cost の有向辺を追加します.

辺のラベルはグラフに追加した順番と一致します.

また,メンバ変数の辺数は $1$ 本だけ増えます.

制約

計算量

operator []

vector<Edge<T>> g[int i]

頂点 $i$ に隣接する頂点集合を返します.

制約

計算量

Depends on

Required by

Verified with

Code

#pragma once
#include "../template/template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
Back to top page