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)

// ---------- begin Strongly Connected Components ----------
struct SCC {
    size: usize,
    edge: Vec<(u32, u32)>,
    id: Vec<usize>,
}

impl SCC {
    pub fn new(size: usize) -> Self {
        SCC {
            size: size,
            edge: vec![],
            id: vec![0; size],
        }
    }
    pub fn add_edge(&mut self, from: usize, to: usize) {
        assert!(from < self.size && to < self.size);
        self.edge.push((from as u32, to as u32));
    }
    fn fill_buffer(&self, buf: &mut [u32], cnt: &[u32]) {
        let mut index = vec![0; self.size];
        let mut sum = 0;
        for (index, c) in index.iter_mut().zip(cnt.iter()) {
            *index = sum;
            sum += *c;
        }
        for &(f, t) in self.edge.iter() {
            let f = f as usize;
            buf[index[f] as usize] = t;
            index[f] += 1;
        }
    }
    fn build_graph<'a>(&self, buf: &'a mut [u32]) -> Vec<&'a mut [u32]> {
        let size = self.size;
        let mut cnt = vec![0; size];
        for e in self.edge.iter() {
            cnt[e.0 as usize] += 1;
        }
        self.fill_buffer(buf, &cnt);
        let mut buf = Some(buf);
        let mut ans = Vec::with_capacity(size);
        for &len in cnt.iter() {
            let len = len as usize;
            let x = buf.take().unwrap();
            let (x, y) = x.split_at_mut(len);
            ans.push(x);
            buf = Some(y);
        }
        ans
    }
    pub fn build(&mut self) {
        let size = self.size;
        let mut buf = vec![0; self.edge.len()];
        let buf = buf.as_mut_slice();
        let graph = self.build_graph(buf);
        let empty = size as u32;
        let mut index = 0;
        let mut ord = vec![empty; size];
        let mut assigned = vec![false; size];
        let mut id = 0;
        let mut stack_s = vec![];
        let mut stack_p = vec![];
        let mut call = vec![];
        for i in 0..size {
            if ord[i] != empty {
                continue;
            }
            enum Operation {
                Call(usize),
                Iterate(usize, usize),
                Eval(usize),
            }
            call.push(Operation::Call(i));
            while let Some(op) = call.pop() {
                match op {
                    Operation::Call(v) => {
                        ord[v] = index;
                        index += 1;
                        stack_s.push(v);
                        stack_p.push(v);
                        call.push(Operation::Eval(v));
                        call.push(Operation::Iterate(v, 0));
                    },
                    Operation::Iterate(v, k) => {
                        for i in k..graph[v].len() {
                            let u = graph[v][i] as usize;
                            if ord[u] == empty {
                                call.push(Operation::Iterate(v, i + 1));
                                call.push(Operation::Call(u));
                                break;
                            } else if !assigned[u] {
                                while let Some(&x) = stack_p.last() {
                                    if ord[x] <= ord[u] {
                                        break;
                                    } else {
                                        stack_p.pop();
                                    }
                                }
                            }
                        }
                    },
                    Operation::Eval(v) => {
                        if *stack_p.last().unwrap() == v {
                            while let Some(u) = stack_s.pop() {
                                assigned[u] = true;
                                self.id[u] = id;
                                if u == v {
                                    break;
                                }
                            }
                            stack_p.pop();
                            id += 1;
                        }
                    },
                }
            }
        }
        for x in self.id.iter_mut() {
            *x = id - 1 - *x;
        }
    }
    pub fn get_array(&self) -> Vec<usize> {
        self.id.clone()
    }
}
// ---------- end Strongly Connected Components ----------

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);
    }
    scc.build();
    let id = scc.get_array();
    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();
}