Problem:
Score : 100 points

Problem Statement

You are given a directed graph with N vertices and M edges, not necessarily simple. The i-th edge is oriented from the vertex a_i to the vertex b_i.
Divide this graph into strongly connected components and print them in their topological order.

Constraints

1 \leq N \leq 500,000

1 \leq M \leq 500,000

0 \leq a_i, b_i < N

Input

Input is given from Standard Input in the following format:

N M
a_0 b_0
a_1 b_1
:
a_{M - 1} b_{M - 1}

Output

Print 1+K lines, where K is the number of strongly connected components.
Print K on the first line.
Print the information of each strongly connected component in next K lines in the following format, where l is the number of vertices in the strongly connected component and v_i is the index of the vertex in it.

l v_0 v_1 ... v_{l-1}

Here, for each edge (a_i, b_i), b_i should not appear in earlier line than a_i.

If there are multiple correct output, print any of them.

Sample Input 1

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

Sample Output 1

4
1 5
2 4 1
1 2
2 3 0

Solution in Rust (1.42.0) (judge verdict: Accepted)

use proconio::*;

// Compressed sparse row
struct CSR {
    start: Vec<usize>,
    elist: Vec<usize>,
}

impl CSR {
    fn new(n: usize, edge: &[(usize, usize)]) -> Self {
        let mut start = vec![0; n + 1];
        edge.iter().for_each(|&(s, _)| start[s + 1] += 1);
        for i in 1..=n {
            start[i] += start[i - 1];
        }
        let mut index = start.clone();
        let mut elist = vec![0; edge.len()];
        for &(s, t) in edge.iter() {
            elist[index[s]] = t;
            index[s] += 1;
        }
        CSR { start, elist }
    }
}

struct State {
    graph: CSR,
    ts: usize,
    scc_id: usize,
    low: Vec<usize>,
    vis: Vec<usize>,
    ids: Vec<usize>,
    stack: Vec<usize>,
}

impl State {
    fn new(n: usize, edges: &[(usize, usize)]) -> Self {
        Self {
            graph: CSR::new(n, edges),
            ts: 1,
            scc_id: 0,
            low: vec![0; n],
            vis: vec![0; n],
            ids: vec![n; n],
            stack: Vec::with_capacity(n),
        }
    }
}

struct SccGraph {
    n: usize,
    edge: Vec<(usize, usize)>,
}

impl SccGraph {
    fn new(n: usize) -> Self {
        Self { n, edge: vec![] }
    }
    fn add_edge(&mut self, s: usize, t: usize) {
        assert!(s < self.n);
        assert!(t < self.n);
        self.edge.push((s, t));
    }

    fn scc_ids(&self) -> (usize, Vec<usize>) {
        let mut state = State::new(self.n, &self.edge);

        fn dfs(v: usize, n: usize, state: &mut State) {
            state.low[v] = state.ts;
            state.vis[v] = state.ts;
            state.ts += 1;
            state.stack.push(v);
            for i in state.graph.start[v]..state.graph.start[v + 1] {
                let u = state.graph.elist[i];
                if state.vis[u] == 0 {
                    dfs(u, n, state);
                    state.low[v] = std::cmp::min(state.low[v], state.low[u]);
                } else if state.ids[u] == n {
                    state.low[v] = std::cmp::min(state.low[v], state.vis[u]);
                }
            }
            if state.low[v] == state.vis[v] {
                while let Some(u) = state.stack.pop() {
                    state.ids[u] = state.scc_id;
                    if u == v {
                        break;
                    }
                }
                state.scc_id += 1;
            }
        }

        for i in 0..self.n {
            if state.vis[i] == 0 {
                dfs(i, self.n, &mut state);
            }
        }

        for x in state.ids.iter_mut() {
            *x = state.scc_id - 1 - *x;
        }
        (state.scc_id, state.ids)
    }

    pub fn scc(&self) -> Vec<Vec<usize>> {
        let (scc_num, ids) = self.scc_ids();
        let mut groups = vec![vec![]; scc_num];
        for (i, &id) in ids.iter().enumerate() {
            groups[id].push(i);
        }
        groups
    }
}

fn main() {
    input! {
    n: usize,
    m: usize,
    e: [(usize, usize); m],
    }
    let mut scc = SccGraph::new(n);
    for (s, t) in e {
        scc.add_edge(s, t);
    }

    let groups = scc.scc();

    println!("{}", groups.len());
    for i in 0..groups.len() {
        print!("{}", groups[i].len());
        for j in &groups[i] {
            print!(" {}", j);
        }
        println!();
    }
}