This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/scc"
#define FAST_IO
#include "../../template/template.hpp"
#include "../../graph/graph.hpp"
#include "../../graph/strongly_connected_components.hpp"
int main() {
i32 n, m;
cin >> n >> m;
Graph<i32> g(n);
REP(e, m) {
i32 u, v;
cin >> u >> v;
g.add_directed_edge(u, v);
}
StronglyConnectedComponents<Graph<i32>> scc(g);
Vec<Vec<i32>> groups = scc.groups();
cout << groups.size() << '\n';
for (const Vec<i32> &group : groups) {
cout << group.size();
for (i32 v : group) {
cout << ' ' << v;
}
cout << '\n';
}
}#line 1 "graph/test/scc.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/scc"
#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/strongly_connected_components.hpp"
#line 4 "graph/strongly_connected_components.hpp"
template <typename G>
class StronglyConnectedComponents {
std::vector<int> comp_id;
int comp_num;
public:
StronglyConnectedComponents(const G &g) : comp_id(g.size(), -1), comp_num(0) {
int now = 0;
std::vector<int> vs;
std::vector<int> ord(g.size(), -1);
std::vector<int> low(g.size(), -1);
const auto dfs = [&](const auto &dfs, int v) -> void {
vs.push_back(v);
ord[v] = now++;
low[v] = ord[v];
for (int u : g[v]) {
if (comp_id[u] != -1) {
continue;
}
if (ord[u] == -1) {
dfs(dfs, u);
}
low[v] = std::min(low[v], low[u]);
}
if (low[v] == ord[v]) {
while (true) {
int u = vs.back();
vs.pop_back();
comp_id[u] = comp_num;
if (u == v) {
break;
}
}
++comp_num;
}
};
for (int v = 0; v < (int) g.size(); ++v) {
if (ord[v] == -1) {
dfs(dfs, v);
}
}
for (int v = 0; v < (int) g.size(); ++v) {
comp_id[v] = comp_num - 1 - comp_id[v];
}
}
int comps() const {
return comp_num;
}
int operator[](int v) const {
assert(v >= 0 && v < (int) comp_id.size());
return comp_id[v];
}
std::vector<std::vector<int>> groups() const {
std::vector<std::vector<int>> ret(comp_num);
for (int v = 0; v < (int) comp_id.size(); ++v) {
ret[comp_id[v]].push_back(v);
}
return ret;
}
};
#line 8 "graph/test/scc.test.cpp"
int main() {
i32 n, m;
cin >> n >> m;
Graph<i32> g(n);
REP(e, m) {
i32 u, v;
cin >> u >> v;
g.add_directed_edge(u, v);
}
StronglyConnectedComponents<Graph<i32>> scc(g);
Vec<Vec<i32>> groups = scc.groups();
cout << groups.size() << '\n';
for (const Vec<i32> &group : groups) {
cout << group.size();
for (i32 v : group) {
cout << ' ' << v;
}
cout << '\n';
}
}