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)

struct SCCInternalGraph {
    start: Vec<usize>,
    elist: Vec<usize>,
}

impl SCCInternalGraph {
    fn build(size: usize, edge: &[(usize, usize)]) -> Self {
        let mut start = vec![0; size + 1];
        edge.iter().for_each(|&(s, _)| start[s + 1] += 1);
        for i in 1..=size {
            start[i] += start[i - 1];
        }
        let mut index = start.clone();
        let mut elist = vec![0; edge.len()];
        for &(s, t) in edge.iter() {
            let po = &mut index[s];
            elist[*po] = t;
            *po += 1;
        }
        SCCInternalGraph { start, elist }
    }
    fn neighbor(&self, v: usize) -> &[usize] {
        assert!(v + 1 < self.start.len());
        &self.elist[self.start[v]..self.start[v + 1]]
    }
}

struct SCC {
    size: usize,
    edge: Vec<(usize, usize)>,
}

impl SCC {
    fn new(size: usize) -> Self {
        SCC { size, edge: vec![] }
    }
    fn add_edge(&mut self, a: usize, b: usize) {
        assert!(a < self.size);
        assert!(b < self.size);
        self.edge.push((a, b));
    }
    fn build(&self) -> Vec<usize> {
        let graph = SCCInternalGraph::build(self.size, &self.edge);
        let size = self.size;
        let mut ord = vec![size; size];
        let mut assigned = vec![false; size];
        let mut stack_s = vec![];
        let mut stack_p = vec![];
        let mut call = vec![];
        let mut now_ord = 0;
        let mut res = vec![0; size];
        let mut id = 0;
        enum Operation {
            Call(usize),
            Iter(usize, usize),
            Eval(usize),
        }
        for i in 0..size {
            if ord[i] != size {
                continue;
            }
            call.push(Operation::Call(i));
            while let Some(op) = call.pop() {
                match op {
                    Operation::Call(v) => {
                        ord[v] = now_ord;
                        now_ord += 1;
                        stack_s.push(v);
                        stack_p.push(v);
                        call.push(Operation::Eval(v));
                        call.push(Operation::Iter(v, 0));
                    },
                    Operation::Iter(v, k) => {
                        let g = graph.neighbor(v);
                        for i in k..g.len() {
                            let u = g[i];
                            if ord[u] == size {
                                call.push(Operation::Iter(v, i + 1));
                                call.push(Operation::Call(u));
                                break;
                            } else if !assigned[u] {
                                while ord[*stack_p.last().unwrap()] > ord[u] {
                                    stack_p.pop();
                                }
                            }
                        }
                    },
                    Operation::Eval(v) => {
                        if *stack_p.last().unwrap() == v {
                            while let Some(u) = stack_s.pop() {
                                res[u] = id;
                                assigned[u] = true;
                                if u == v {
                                    break;
                                }
                            }
                            stack_p.pop();
                            id += 1;
                        }
                    },
                }
            }
        }
        res.iter_mut().for_each(|v| *v = id - 1 - *v);
        res
    }
}

use proconio::*;
 
#[fastout]
fn run() {
    input! {
        n: usize,
        m: usize,
        e: [(usize, usize); m],
    }
    let mut scc = SCC::new(n);
    for (a, b) in e {
        scc.add_edge(a, b);
    }
    let id = scc.build();
    let k = *id.iter().max().unwrap() + 1;
    let mut a = vec![vec![]; k];
    for (i, id) in id.iter().enumerate() {
        a[*id].push(i);
    }
    println!("{}", k);
    for a in a.iter() {
        print!("{}", a.len());
        for a in a {
            print!(" {}", *a);
        }
        println!();
    }
}
 
fn main() {
    run();
}