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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AtCoderLibraryCSharp.Examples
{
    public static class Program
    {
        public static void Main()
        {
            var NM = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
            var (N, M) = (NM[0], NM[1]);
            var G = new StronglyConnectedComponent(N);

            for (var i = 0; i < M; i++)
            {
                var UV = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
                var (u, v) = (UV[0], UV[1]);
                G.AddEdge(u, v);
            }

            var scc = G.GetGraph();
            Console.WriteLine(scc.Count());
            foreach (var v in scc)
                Console.WriteLine($"{v.Count()} {string.Join(" ", v)}");
        }
    }
}

namespace AtCoderLibraryCSharp
{
    public class StronglyConnectedComponent
    {
        private readonly struct Edge
        {
            public readonly int To;
            public Edge(int to) => To = to;
        }

        private readonly int _n;
        private readonly List<(int, Edge)> _edges;

        public StronglyConnectedComponent(int n = 0)
        {
            _n = n;
            _edges = new List<(int, Edge)>();
        }

        public void AddEdge(int from, int to)
        {
            if (from < 0 || _n <= from) throw new IndexOutOfRangeException(nameof(from));
            if (to < 0 || _n <= to) throw new IndexOutOfRangeException(nameof(to));
            _edges.Add((from, new Edge(to)));
        }

        public (int, IEnumerable<int>) Ids()
        {
            var g = new CompressedSparseRow<Edge>(_n, _edges);
            var (nowOrd, groupNum) = (0, 0);
            var visited = new Stack<int>(_n);
            var low = new int[_n];
            var ord = Enumerable.Repeat(-1, _n).ToArray();
            var ids = new int[_n];

            void Dfs(int v)
            {
                low[v] = ord[v] = nowOrd++;
                visited.Push(v);
                for (var i = g.Start[v]; i < g.Start[v + 1]; i++)
                {
                    var to = g.Edges[i].To;
                    if (ord[to] == -1)
                    {
                        Dfs(to);
                        low[v] = System.Math.Min(low[v], low[to]);
                    }
                    else low[v] = System.Math.Min(low[v], ord[to]);
                }

                if (low[v] != ord[v]) return;
                while (true)
                {
                    var u = visited.Pop();
                    ord[u] = _n;
                    ids[u] = groupNum;
                    if (u == v) break;
                }

                groupNum++;
            }

            for (var i = 0; i < _n; i++)
                if (ord[i] == -1)
                    Dfs(i);

            for (var i = 0; i < _n; i++) ids[i] = groupNum - 1 - ids[i];

            return (groupNum, ids);
        }

        public IEnumerable<IEnumerable<int>> GetGraph()
        {
            var (groupNum, tmp) = Ids();
            var ids = tmp.ToArray();
            var groups = new List<int>[groupNum].Select(x => new List<int>()).ToArray();
            foreach (var (id, i) in ids.Select((x, i) => (x, i))) groups[id].Add(i);
            return groups;
        }
    }
}

namespace AtCoderLibraryCSharp
{
    public class CompressedSparseRow<T>
    {
        public int[] Start { get; }
        public T[] Edges { get; }

        public CompressedSparseRow(int n, IEnumerable<(int, T)> edges)
        {
            Start = new int[n + 1];
            var es = edges.ToArray();
            Edges = new T[es.Length];
            foreach (var e in es) Start[e.Item1 + 1]++;
            for (var i = 0; i < n; i++) Start[i + 1] += Start[i];
            var counter = Start.ToArray();
            foreach (var (i, t) in es) Edges[counter[i]++] = t;
        }
    }
}