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: Wrong Answer)

use proconio::*;

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

impl SCCInternalGraph {
    fn build(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;
        }
        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 SCCGraph {
    n: usize,
    edge: Vec<(usize, usize)>,
}

impl SCCGraph {
    fn new(n: usize) -> Self {
        Self { n, edge: vec![] }
    }
    fn add_edge(&mut self, a: usize, b: usize) {
        assert!(a < self.n);
        assert!(b < self.n);
        self.edge.push((a, b));
    }
    fn build(&self) -> Vec<usize> {
        let graph = SCCInternalGraph::build(self.n, &self.edge);
        let n = self.n;
        let mut ord = vec![n; n];
        let mut assigned = vec![false; n];
        let mut stack_s = vec![];
        let mut stack_p = vec![];
        let mut call = vec![];
        let mut ts = 0;
        let mut res = vec![0; n];
        let mut ids = vec![n; n];
        let mut gid = 0;
        enum Operation {
            Call(usize),
            Iter(usize, usize),
            Eval(usize),
        }
        for i in 0..n {
            if ord[i] != n {
                continue;
            }
            call.push(Operation::Call(i));
            while let Some(op) = call.pop() {
                match op {
                    Operation::Call(v) => {
                        ord[v] = ts;
                        ts += 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] == n {
                                call.push(Operation::Iter(v, i + 1));
                                call.push(Operation::Call(u));
                                break;
                            } else if ids[u] == n {
                                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() {
                                ids[u] = gid;
                                // res[u] = gid;
                                // assigned[u] = true;
                                if u == v {
                                    break;
                                }
                            }
                            stack_p.pop();
                            gid += 1;
                        }
                    }
                }
            }
        }
        ids
        // res.iter_mut().for_each(|v| *v = gid - 1 - *v);
        // res
    }
}

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 ids = scc.build();

    let k = *ids.iter().max().unwrap() + 1;
    let mut node = vec![vec![]; k];
    for (i, &id) in ids.iter().enumerate() {
        node[id].push(i);
    }
    println!("{}", k);
    for i in 0..k {
        print!("{}", node[i].len());
        for j in &node[i] {
            print!(" {}", j);
        }
        println!();
    }

    // println!("{:?}", ids);
}