Algorithm-Library

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

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/yosupo/Graph/Strongly-Connected-Components.test.cpp

Depends on

Code

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

#include "../../../template/template.hpp"

#include "../../../graph/scc.hpp"

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m;
  cin >> n >> m;
  vvi root(n);
  rep(i, m){
    int a,b;
    cin >> a >> b;
    root[a].push_back(b);
  }
  scc<vvi> g(root);
  cout << g.group.size() << "\n";
  for(const vi &t : g.group){
    int si = t.size();
    cout << si << " ";
    per(i, si) cout << t[i] << " ";
    cout << "\n";
  }
}
#line 1 "test/yosupo/Graph/Strongly-Connected-Components.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/scc"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/yosupo/Graph/Strongly-Connected-Components.test.cpp"

#line 1 "graph/scc.hpp"
template <class graph>
struct scc {
  scc(const graph &g): root(g), n(g.size()){ init(); }
  void init(){
    rroot.resize(n);
    used.resize(n);
    ord.resize(n);
    comp.resize(n, -1);
    build();
  }
  void build(){
    for(int i = 0; i < n; i++){
      for(const auto &x : root[i])
        rroot[x].push_back(i);
    }
    for(int i = 0; i < n; i++) dfs(i);
    reverse(ord.begin(), ord.end());
    int cnt = 0;
    for(const auto &x : ord) if(comp[x] == -1){
      rdfs(x, cnt);
      cnt++;
    }
    dag.resize(cnt);
    group.resize(cnt);
    for(int i = 0; i < n; i++){
      for(const auto &x : root[i]){
        const int a = comp[i], b = comp[x];
        if(a == b) continue;
        dag[a].push_back(b);
      }
    }
    for(int i = 0; i < n; i++){
      group[comp[i]].push_back(i);
    }
  }
  int operator[](int i) const{
    return comp[i];
  }
  vector<vector<int>> group, dag;
  private:
  int n = 0;
  const graph &root;
  graph rroot; //rev

  vector<int> used, ord, comp;
  void dfs(int pos){
    if(used[pos]) return;
    used[pos] = 1;
    for(const auto &x : root[pos]) dfs(x);
    ord.push_back(pos);
  }
  void rdfs(int pos, const int cnt){
    if(comp[pos] != -1) return;
    comp[pos] = cnt;
    for(const auto &x : rroot[pos]) rdfs(x, cnt);
  }
};
#line 6 "test/yosupo/Graph/Strongly-Connected-Components.test.cpp"

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m;
  cin >> n >> m;
  vvi root(n);
  rep(i, m){
    int a,b;
    cin >> a >> b;
    root[a].push_back(b);
  }
  scc<vvi> g(root);
  cout << g.group.size() << "\n";
  for(const vi &t : g.group){
    int si = t.size();
    cout << si << " ";
    per(i, si) cout << t[i] << " ";
    cout << "\n";
  }
}
Back to top page