QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 2048 MB 満点: 100

#2657. 观鸟

統計

Kiara 正在研究一种奇特的鸟类,它们的迁徙方式非常独特。它们的移动可以用图论来解释:存在一个有向图 $\mathcal{G}$,其中节点代表树木,鸟类仅能在 $(T_a, T_b)$ 为 $\mathcal{G}$ 的一条边时,从树 $T_a$ 飞往 $T_b$。

Kiara 不知道控制这些鸟类飞行的真实图 $\mathcal{G}$,但在之前的实地考察中,她收集了许多鸟类的迁徙数据。利用这些数据,她构建了一个图 $\mathcal{P}$ 来解释它们的移动方式。Kiara 花了大量时间观察,她确信如果一只鸟可以直接从 $a$ 飞到 $b$,那么她至少目击过一次这样的飞行。然而,也有可能鸟类是从 $a$ 飞到 $b$ 再飞到 $c$,但她只观察到了起点 $a$ 和终点 $c$,并据此在 $\mathcal{P}$ 中添加了边 $(a, c)$。同样,鸟类也可能从 $a$ 飞到 $b$ 再到 $c$ 再到 $d$,而她只观察到了 $a$ 和 $d$,并添加了边 $(a, d)$,以此类推。总之,她知道 $\mathcal{P}$ 包含了 $\mathcal{G}$ 的所有边,并且 $\mathcal{P}$ 可能还包含一些其他边 $(a, b)$,这些边在 $\mathcal{G}$ 中对应着一条从 $a$ 到 $b$ 的路径(注意 $\mathcal{P}$ 可能并不包含所有这样的路径对应的边)。

在下一次实地考察中,Kiara 决定将基地设在某棵给定的树 $T$ 旁边。为了预警鸟类到达 $T$,她还想在鸟类可能飞来的树上安装探测器(即存在边 $(T', T)$ 的树 $T'$)。由于探测器价格昂贵,她只想在那些她确信 $(T', T)$ 属于 $\mathcal{G}$ 的树 $T'$ 上安装探测器。

Kiara 确信边 $(a, b)$ 属于 $\mathcal{G}$ 的条件是:$(a, b)$ 是 $\mathcal{P}$ 中的一条边,且 $\mathcal{P}$ 中所有从 $a$ 出发并以 $b$ 结尾的路径都经过边 $(a, b)$。Kiara 请你计算出所有满足她确信 $(T', T)$ 是 $\mathcal{G}$ 中一条边的树 $T'$ 的集合 $S(T)$。

输入格式

输入描述了图 $\mathcal{P}$。第一行包含三个空格分隔的整数 $N, M, T$:$N$ 是 $\mathcal{P}$ 的节点数,$M$ 是 $\mathcal{P}$ 的边数,$T$ 是 Kiara 建立基地的树对应的节点编号。

接下来的 $M$ 行描述了图 $\mathcal{P}$ 的边。每行包含两个空格分隔的整数 $a$ 和 $b$ ($0 \le a, b < N$ 且 $a \neq b$),表示 $(a, b) \in \mathcal{P}$。保证同一对 $(a, b)$ 不会出现两次。

数据范围

  • $1 \le N, M \le 100\,000$
  • $0 \le T < N$

输出格式

你的输出应描述集合 $S(T)$。第一行应包含一个整数 $L$,即 $S(T)$ 中节点的数量,随后是 $L$ 行,每行包含 $S(T)$ 中的一个不同元素。元素应按升序打印,每行一个。

样例

样例输入 1

3 3 2
0 1
0 2
1 2

样例输出 1

1
1

说明 1

该示例对应的图如右图所示。节点 $1$ 属于 $S(2)$,因为从 $1$ 到 $2$ 的(唯一)路径使用了边 $(1, 2)$。节点 $0$ 不属于 $S(2)$,因为路径 $0 \to 1 \to 2$ 没有使用边 $(0, 2)$。

样例输入 2

6 8 2
0 1
0 2
1 2
2 0
2 3
3 4
4 2
2 5

样例输出 2

2
1
4

说明 2

该示例对应的图如右图所示。与样例 1 的原因相同,节点 $0$ 不属于 $S(2)$,而 $1$ 属于。节点 $3$ 和 $5$ 不属于 $S(2)$,因为我们没有边 $(3, 2)$ 或 $(5, 2)$。最后,$4$ 属于 $S(2)$,因为从 $4$ 到 $2$ 的所有路径都使用了边 $(4, 2)$。

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.