QOJ.ac

QOJ

时间限制: 5 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#906. Strongly Connected Components

统计

Given a directed graph with $N$ vertices and $M$ edges. The $i$-th edge is $(a_i, b_i)$. This graph may not be simple.

Decompose this graph into SCCs and print them in topological order.

Constraints

  • $1 \leq N \leq 500,000$
  • $1 \leq M \leq 500,000$
  • $0 \leq a_i, b_i < N$

Input

$N$ $M$ $a_0$ $b_0$ $a_1$ $b_1$ : $a_{M - 1}$ $b_{M - 1}$

Output

The first line contains the number of SCCs $K$. The following $K$ lines contain the information for each SCC. $l$ is the number of vertices in the SCC, and $v_i$ are the vertex indices.

$l$ $v_0$ $v_1$ ... $v_{l-1}$

For each edge $(a_i, b_i)$, the line containing $b_i$ must not appear before the line containing $a_i$.

If there are multiple solutions, you may print any of them.

Examples

Input 1

6 7
1 4
5 2
3 0
5 5
4 1
0 3
4 2

Output 1

4
1 5
2 4 1
1 2
2 3 0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.