CF1368E.Ski Accidents

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Arthur owns a ski resort on a mountain. There are nn landing spots on the mountain numbered from 11 to nn from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot.

A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks.

Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable.

Formally, after closing some of the spots, there should not be a path that consists of two or more tracks.

Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 47n\frac{4}{7}n spots so that the remaining part is safe. Help him find any suitable way to do so.

亚瑟在一座山上拥有一家滑雪场。山上共有 nn 个着陆点,从山顶到山脚依次编号为 11 到 nn。这些着陆点之间由单向滑雪道连接。所有滑雪道均朝向山脚方向延伸,因此滑雪道所构成的有向图中不存在有向环。每个着陆点出发的滑雪道至多有两条,但可能有多条滑雪道通向同一个着陆点。

一名滑雪者若能从某个着陆点出发,经由一系列滑雪道最终到达另一个着陆点,则称其可从起点滑行至终点。然而,最近发生了多起事故,原因在于滑雪场的结构允许滑雪者沿某些危险路径滑行——这些路径会使滑雪者达到过高的速度,从而危及自身及其他顾客。此处,一条路径被称为危险路径,当且仅当它包含至少两条滑雪道。

亚瑟希望保障顾客安全,为此他计划关闭部分着陆点;一旦某着陆点被关闭,则所有进入或离开该点的滑雪道均不可使用。

形式化地说:在关闭若干着陆点后,剩余结构中不应存在任何包含两条或更多滑雪道的路径。

亚瑟并不想关闭过多的着陆点。他仅需找到一种方案,使得关闭的着陆点数量不超过 47n\frac{4}{7}n,且剩余部分是安全的。请帮助他找出任意一种满足条件的关闭方案。

输入格式

The first line contains a single positive integer TT — the number of test cases. TT test case description follows.

The first line of each description contains two integers nn and mm (1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5) — the number of landing spots and tracks respectively.

The following mm lines describe the tracks. Each of these lines contains two integers xx and yy (1≤x<y≤n1 \leq x \lt y \leq n) — indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个正整数 TT —— 测试用例的数量。随后是 TT 个测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)—— 分别表示着陆点的数量和滑道的数量。

接下来的 mm 行描述了这些滑道。每行包含两个整数 xx 和 yy(1≤x<y≤n1 \leq x \lt y \leq n)—— 分别表示该滑道的起点和终点着陆点的编号。保证每个着陆点最多作为两个滑道的起点。可能存在起点与终点相同的滑道。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print a single integer kk (0≤k≤47n0 \leq k \leq \frac{4}{7}n) — the number of spots to be closed. In the next line, print kk distinct integers — indices of all spots to be closed, in any order.

If there are several answers, you may output any of them. Note that you don't have to minimize kk. It can be shown that a suitable answer always exists.

对于每个测试用例,输出一个整数 kk(0≤k≤47n0 \leq k \leq \frac{4}{7}n)—— 即需要关闭的斑点数量。
在下一行,输出 kk 个互不相同的整数 —— 即所有需要关闭的斑点的索引(顺序任意)。

若存在多个合法答案,输出其中任意一个即可。注意:你无需最小化 kk。可以证明,总存在满足条件的答案。

输入输出样例

  • 输入#1

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

    输出#1

    2
    3 4 
    4
    4 5 6 7

说明/提示

In the first sample case, closing any two spots is suitable.

In the second sample case, closing only the spot 11 is also suitable.

在第一个样例中,关闭任意两个位置都是可行的。

在第二个样例中,仅关闭位置 11 也是可行的。

输入解题思路,AI测评打分。不知道怎么写?

首页