algo

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

View the Project on GitHub dnx04/algo

:heavy_check_mark: tests/Biconnected_Components.test.cpp

Depends on

Code

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

#include "../misc/macros.h"
#include "../graph/2CC.h"

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vi> g(n);
  for(int i= 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].eb(v), g[v].eb(u);
  }
  BCC t(g);
  cout << sz(t.blks) << '\n';
  for(auto blk: t.blks) {
    cout << sz(blk) << ' ';
    for(auto u: blk) cout << u << ' ';
    cout << '\n';
  }
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  solve();
  return 0;
}
#line 1 "tests/Biconnected_Components.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/biconnected_components"

#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops")       // unroll long, simple loops
// #pragma GCC target("avx2,fma")                   // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2")  // for fast bitset operation

#include <bits/extc++.h>
#include <tr2/dynamic_bitset>

using namespace std;
using namespace __gnu_pbds;  // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope

// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;

// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {  // customize hash function for gp_hash_table
  int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;

/* ordered set
    find_by_order(k): returns an iterator to the k-th element (0-based)
    order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*  rope
    rope <int> cur = v.substr(l, r - l + 1);
    v.erase(l, r - l + 1);
    v.insert(v.mutable_begin(), cur);
*/
#line 1 "graph/2CC.h"
struct BCC {
  const vector<vi>& g;
  int n, ti = 0;
  vector<vi> tree, blks;
  vi tin, low, st;
  void dfs(int u, int p = -1) {
    tin[u] = low[u] = ++ti;
    st.pb(u);
    for (int v : g[u]) if (v != p) {
      if (tin[v]) low[u] = min(low[u], tin[v]);
      else {
        dfs(v, u);
        low[u] = min(low[u], low[v]);
        if (low[v] >= tin[u]) {
          blks.pb({u});
          for (int x = -1; x != v; st.pop_back()) blks.back().pb(x = st.back());
        }
      }
    }
  }
  BCC(const vector<vi>& G) : g(G), n(sz(G)), tin(n), low(n) {
    for (int i = 0; i < n; ++i) if (!tin[i]) {
      dfs(i);
      if (g[i].empty()) blks.pb({i});
    }
    tree.assign(n + sz(blks), {});
    for (int i = 0; i < sz(blks); ++i) {
      int bid = n + i;
      for (int u : blks[i]) tree[bid].pb(u), tree[u].pb(bid);
    }
  }
};

struct ECC {
  const vector<vi>& g;
  int n, ti = 0;
  vector<vi> tree, comps;
  vi tin, low, id, st;
  void dfs(int u, int p = -1) {
    tin[u] = low[u] = ++ti;
    st.pb(u);
    bool skipped = false; // Fix multiple edges
    for (int v : g[u]) {
      if (v == p && !skipped) { skipped = true; continue; }
      if (tin[v]) low[u] = min(low[u], tin[v]);
      else {
        dfs(v, u);
        low[u] = min(low[u], low[v]);
      }
    }
    if (low[u] == tin[u]) { // Component root
      comps.pb({});
      for (int x = -1; x != u; st.pop_back()) {
        id[x = st.back()] = sz(comps) - 1;
        comps.back().pb(x);
      }
    }
  }
  ECC(const vector<vi>& G) : g(G), n(sz(G)), tin(n), low(n), id(n) {
    for (int i = 0; i < n; ++i) if (!tin[i]) dfs(i);
    tree.assign(sz(comps), {});
    for (int u = 0; u < n; ++u) for (int v : g[u])
      if (id[u] != id[v]) tree[id[u]].pb(id[v]);
    for (auto& adj : tree) {
      sort(all(adj)); adj.erase(unique(all(adj)), adj.end());
    }
  }
};
#line 5 "tests/Biconnected_Components.test.cpp"

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vi> g(n);
  for(int i= 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].eb(v), g[v].eb(u);
  }
  BCC t(g);
  cout << sz(t.blks) << '\n';
  for(auto blk: t.blks) {
    cout << sz(blk) << ' ';
    for(auto u: blk) cout << u << ' ';
    cout << '\n';
  }
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
  solve();
  return 0;
}
Back to top page