QOJ.ac

QOJ

実行時間制限: 5 s メモリ制限: 512 MB 満点: 100 ハック可能 ✓

#906. 强连通分量

統計

给定一个具有 $N$ 个顶点和 $M$ 条边的有向图。第 $i$ 条边为 $(a_i, b_i)$。该图不一定是简单图。

请将该图分解为强连通分量(SCC),并按拓扑顺序输出它们。

数据范围

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

输入格式

第一行包含 $N$ 和 $M$。 接下来的 $M$ 行,每行包含 $a_i$ 和 $b_i$。

输出格式

第一行输出强连通分量的个数 $K$。 接下来的 $K$ 行,每行输出一个强连通分量的信息,格式如下: $l$ $v_0$ $v_1$ ... $v_{l-1}$ 其中 $l$ 是该强连通分量中顶点的数量,$v_i$ 是顶点的编号。

对于每条边 $(a_i, b_i)$,包含 $b_i$ 的行不能出现在包含 $a_i$ 的行之前。

如果存在多种合法的输出,输出其中任意一种即可。

样例

输入 1

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

输出 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.