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 Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

-- Strongly Connected Component Decomposition
local n, m = io.read("*n", "*n")
local edge = {}
local edge_asked = {}
local invedge = {}
local invedge_asked = {}
for i = 1, n do
  edge[i] = {}
  edge_asked[i] = 0
  invedge[i] = {}
  invedge_asked[i] = 0
end
local edgeinfo = {}
for i = 1, m do
  local a, b = io.read("*n", "*n")
  a, b = a + 1, b + 1
  edgeinfo[i] = {a, b}
  table.insert(edge[a], b)
  table.insert(invedge[b], a)
end

local asked = {}
local sccd_root = {}
for i = 1, n do
  asked[i] = false
  sccd_root[i] = 0
end

local function SCCD_dfs(spos, dfs_way)
  local tasks = {spos}
  while 0 < #tasks do
    local src = tasks[#tasks]
    asked[src] = true
    table.remove(tasks)
    if edge_asked[src] == #edge[src] then
      table.insert(dfs_way, src)
    else
      table.insert(tasks, src)
      edge_asked[src] = edge_asked[src] + 1
      local dst = edge[src][edge_asked[src]]
      if not asked[dst] then
        table.insert(tasks, dst)
      end
    end
  end
end

local function SCCD_invdfs(spos, rootid)
  local tasks = {spos}
  while 0 < #tasks do
    local src = tasks[#tasks]
    sccd_root[src] = rootid
    table.remove(tasks)
    while invedge_asked[src] < #invedge[src] do
      invedge_asked[src] = invedge_asked[src] + 1
      local dst = invedge[src][invedge_asked[src]]
      if asked[dst] and sccd_root[dst] == 0 then
        table.insert(tasks, src)
        table.insert(tasks, dst)
        break
      end
    end
  end
end

local function SCCD_make()
  for src = 1, n do
    if not asked[src] then
      local dfs_way = {}
      SCCD_dfs(src, dfs_way)
      for i = #dfs_way, 1, -1 do
        local src = dfs_way[i]
        if sccd_root[src] == 0 then
          SCCD_invdfs(src, src)
        end
      end
    end
  end
end

SCCD_make()

local gsize = {}
local gedge, ginvedge = {}, {}
local glen = {}
local gmember = {}
for i = 1, n do
  gsize[i] = 0
  gedge[i], ginvedge[i] = {}, {}
  glen[i] = 0
  gmember[i] = {}
end
for i = 1, n do
  local r = sccd_root[i]
  gsize[r] = gsize[r] + 1
  table.insert(gmember[r], i)
end
for i = 1, m do
  local a, b = edgeinfo[i][1], edgeinfo[i][2]
  local ra, rb = sccd_root[a], sccd_root[b]
  if ra ~= rb then
    table.insert(gedge[ra], rb)
    table.insert(ginvedge[rb], ra)
  end
end
local topo_tasks = {}
local topo_asked_cnt = {}
local retcnt = 0
for i = 1, n do
  topo_asked_cnt[i] = 0
  if 0 < gsize[i] then retcnt = retcnt + 1 end
  if 0 < gsize[i] and #ginvedge[i] == 0 then
    table.insert(topo_tasks, i)
  end
end
print(retcnt)
local topo_done = 0
while topo_done < #topo_tasks do
  topo_done = topo_done + 1
  local g = topo_tasks[topo_done]
  io.write(#gmember[g])
  for i = 1, #gmember[g] do
    io.write(" " .. gmember[g][i] - 1)
  end
  io.write("\n")
  for i = 1, #gedge[g] do
    local dst = gedge[g][i]
    topo_asked_cnt[dst] = topo_asked_cnt[dst] + 1
    if topo_asked_cnt[dst] == #ginvedge[dst] then
      table.insert(topo_tasks, dst)
    end
  end
end