cpl

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

View the Project on GitHub Forestedf/cpl

:heavy_check_mark: graph/test/two_edge_connected_components.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/two_edge_connected_components"

#define FAST_IO

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

int main() {
    i32 n, m;
    cin >> n >> m;
    Graph<i32> g(n);
    REP(i, m) {
        i32 u, v;
        cin >> u >> v;
        g.add_undirected_edge(u, v);
    }
    TwoEdgeConnectedComponents cc(g);
    Vec<Vec<i32>> groups = cc.groups();
    cout << groups.size() << '\n';
    for (const Vec<i32> &c : groups) {
        cout << c.size();
        for (i32 v : c) {
            cout << ' ' << v;
        }
        cout << '\n';
    }
}
#line 1 "graph/test/two_edge_connected_components.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/two_edge_connected_components"

#define FAST_IO

#line 1 "template/template.hpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i)
#define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;
using f64 = double;
using f80 = long double;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

istream &operator>>(istream &is, i128 &x) {
    i64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, i128 x) {
    os << (i64) x;
    return os;
}
istream &operator>>(istream &is, u128 &x) {
    u64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, u128 x) {
    os << (u64) x;
    return os;
}

[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
#line 2 "graph/graph.hpp"

#line 7 "graph/graph.hpp"

template <typename Edge>
class Graph {
    std::vector<std::vector<Edge>> edges;

public:
    Graph() : edges() {}
    Graph(int v) : edges(v) {
        assert(v >= 0);
    }
    
    std::vector<int> add_vertices(int n) {
        int v = (int) edges.size();
        std::vector<int> idx(n);
        std::iota(idx.begin(), idx.end(), v);
        edges.resize(edges.size() + n);
        return idx;
    }

    template <typename... T>
    void add_directed_edge(int from, int to, T &&...val) {
        assert(from >= 0 && from < (int) edges.size());
        assert(to >= 0 && to < (int) edges.size());
        edges[from].emplace_back(Edge(to, std::forward<T>(val)...));
    }

    template <typename... T>
    void add_undirected_edge(int u, int v, const T &...val) {
        assert(u >= 0 && u < (int) edges.size());
        assert(v >= 0 && v < (int) edges.size());
        edges[u].emplace_back(Edge(v, val...));
        edges[v].emplace_back(Edge(u, val...));
    }

    int size() const {
        return (int) edges.size();
    }

    const std::vector<Edge> &operator[](int v) const {
        assert(v >= 0 && v < (int) edges.size());
        return edges[v];
    }

    std::vector<Edge> &operator[](int v) {
        assert(v >= 0 && v < (int) edges.size());
        return edges[v];
    }
};

struct UnweightedEdge {
    int to;

    UnweightedEdge(int t) : to(t) {}
    
    explicit operator int() const {
        return to;
    }

    using Weight = int;
    Weight weight() const {
        return 1;
    }
};

template <typename T>
struct WeightedEdge {
    int to;
    T wt;

    WeightedEdge(int t, const T &w) : to(t), wt(w) {}

    explicit operator int() const {
        return to;
    }

    using Weight = T;
    Weight weight() const {
        return wt;
    }
};

#line 2 "graph/two_edge_connected_components.hpp"

#line 6 "graph/two_edge_connected_components.hpp"

class TwoEdgeConnectedComponents {
    int comp_num;
    std::vector<int> comp;
    
public:
    template <typename G>
    TwoEdgeConnectedComponents(const G &g) : comp_num(0), comp(g.size(), -1) {
        std::vector<int> ord(g.size(), -1);
        std::vector<int> low(g.size(), -1);
        std::vector<int> found(g.size(), 0);
        const auto dfs0 = [&](const auto &dfs0, int v, int p, int &t) -> void {
            ord[v] = t++;
            low[v] = ord[v];
            bool par = false;
            for (const auto &e : g[v]) {
                int u = (int) e;
                if (!found[u]) {
                    found[u] = 1;
                    dfs0(dfs0, u, v, t);
                    low[v] = std::min(low[v], low[u]);
                }
                bool back = ord[u] < ord[v];
                if (u == p) {
                    if (!par) {
                        back = false;
                        par = true;
                    }
                }
                if (back) {
                    low[v] = std::min(low[v], ord[u]);
                }
            }
        };
        int t = 0;
        for (int v = 0; v < (int) g.size(); ++v) {
            if (!found[v]) {
                found[v] = 1;
                dfs0(dfs0, v, -1, t);
            }
        }
        const auto dfs1 = [&](const auto &dfs1, i32 v, i32 k) -> void {
            comp[v] = k;
            for (const auto &e : g[v]) {
                int u = (int) e;
                if (comp[u] == -1) {
                    if (low[u] > ord[v]) {
                        dfs1(dfs1, u, comp_num++);
                    } else {
                        dfs1(dfs1, u, k);
                    }
                }
            }
        };
        for (int v = 0; v < (int) g.size(); ++v) {
            if (comp[v] == -1) {
                dfs1(dfs1, v, comp_num++);
            }
        }
    }
    
    int operator[](int v) const {
        return comp[v];
    }
    int compc() const {
        return comp_num;
    }
    
    std::vector<std::vector<int>> groups() const {
        std::vector<std::vector<int>> gs(comp_num);
        for (int i = 0; i < (int) comp.size(); ++i) {
            gs[comp[i]].push_back(i);
        }
        return gs;
    }
};
#line 8 "graph/test/two_edge_connected_components.test.cpp"

int main() {
    i32 n, m;
    cin >> n >> m;
    Graph<i32> g(n);
    REP(i, m) {
        i32 u, v;
        cin >> u >> v;
        g.add_undirected_edge(u, v);
    }
    TwoEdgeConnectedComponents cc(g);
    Vec<Vec<i32>> groups = cc.groups();
    cout << groups.size() << '\n';
    for (const Vec<i32> &c : groups) {
        cout << c.size();
        for (i32 v : c) {
            cout << ' ' << v;
        }
        cout << '\n';
    }
}
Back to top page