firiexp's Library

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

View the Project on GitHub firiexp/library

:heavy_check_mark: test/yosupo_biconnected_components.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/biconnected_components"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#include "../graph/biconnected_components.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    BiconnectedComponents G(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        G.add_edge(a, b);
    }
    G.build();
    auto res = G.bcc_vertices;
    for (auto &&v : res) sort(v.begin(), v.end());
    sort(res.begin(), res.end());
    cout << res.size() << "\n";
    for (auto &&v : res) {
        cout << v.size();
        for (auto &&x : v) cout << " " << x;
        cout << "\n";
    }
    return 0;
}
#line 1 "test/yosupo_biconnected_components.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/biconnected_components"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

#line 1 "graph/biconnected_components.cpp"
class BiconnectedComponents {
    vector<int> st;
    void dfs(int i, int pe, int &pos){
        ord[i] = low[i] = pos++;
        for (auto &&e : G[i]) {
            int j = e.first, id = e.second;
            if(id == pe) continue;
            if(ord[j] < ord[i]) st.emplace_back(id);
            if(~ord[j]){
                low[i] = min(low[i], ord[j]);
                continue;
            }
            par[j] = i;
            dfs(j, id, pos);
            low[i] = min(low[i], low[j]);
            if(ord[i] <= low[j]){
                bcc_edges.emplace_back();
                while(true){
                    int k = st.back();
                    st.pop_back();
                    bcc_edges.back().emplace_back(min(edges[k].first, edges[k].second), max(edges[k].first, edges[k].second));
                    if(k == id) break;
                }
            }
        }
    }
public:
    vector<int> ord, low, par;
    vector<pair<int, int>> edges;
    vector<vector<pair<int, int>>> G;
    vector<vector<pair<int, int>>> bcc_edges;
    vector<vector<int>> bcc_vertices;
    explicit BiconnectedComponents(int n): ord(n, -1), low(n), par(n, -1), G(n){}

    void add_edge(int u, int v){
        if(u != v){
            int id = edges.size();
            edges.emplace_back(u, v);
            G[u].emplace_back(v, id);
            G[v].emplace_back(u, id);
        }
    }

    int build(){
        int n = G.size(), pos = 0;
        fill(ord.begin(), ord.end(), -1);
        fill(par.begin(), par.end(), -1);
        bcc_edges.clear();
        bcc_vertices.clear();
        st.clear();
        for (int i = 0; i < n; ++i) {
            if(ord[i] < 0) dfs(i, -1, pos);
        }
        vector<int> seen(n, -1);
        bcc_vertices.reserve(bcc_edges.size());
        for (int i = 0; i < (int)bcc_edges.size(); ++i) {
            vector<int> now;
            for (auto &&e : bcc_edges[i]) {
                if(seen[e.first] != i){
                    seen[e.first] = i;
                    now.emplace_back(e.first);
                }
                if(seen[e.second] != i){
                    seen[e.second] = i;
                    now.emplace_back(e.second);
                }
            }
            bcc_vertices.emplace_back(now);
        }
        for (int i = 0; i < n; ++i) {
            if(G[i].empty()){
                bcc_edges.emplace_back();
                bcc_vertices.push_back({i});
            }
        }
        return bcc_vertices.size();
    }
};

/**
 * @brief 二重連結成分分解(Biconnected Components)
 */
#line 21 "test/yosupo_biconnected_components.test.cpp"

int main() {
    int n, m;
    cin >> n >> m;
    BiconnectedComponents G(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        G.add_edge(a, b);
    }
    G.build();
    auto res = G.bcc_vertices;
    for (auto &&v : res) sort(v.begin(), v.end());
    sort(res.begin(), res.end());
    cout << res.size() << "\n";
    for (auto &&v : res) {
        cout << v.size();
        for (auto &&x : v) cout << " " << x;
        cout << "\n";
    }
    return 0;
}
Back to top page