CF1817B.Fish Graph

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a simple undirected graph with nn nodes and mm edges. Note that the graph is not necessarily connected. The nodes are labeled from 11 to nn.

We define a graph to be a Fish Graph if it contains a simple cycle with a special node uu belonging to the cycle. Apart from the edges in the cycle, the graph should have exactly 22 extra edges. Both edges should connect to node uu, but they should not be connected to any other node of the cycle.

Determine if the graph contains a subgraph that is a Fish Graph, and if so, find any such subgraph.

In this problem, we define a subgraph as a graph obtained by taking any subset of the edges of the original graph.

Visualization of example 1. The red edges form one possible subgraph that is a Fish Graph.

给你一个包含 nn 个节点和 mm 条边的简单无向图。注意,该图不一定是连通的。节点编号为 11 到 nn。

我们定义一个图是“鱼形图(Fish Graph)”,当且仅当它包含一个简单环,并且该环上存在一个特殊节点 uu;除环上的边外,图中还恰好有两条额外的边,且这两条边均与节点 uu 相连,但它们的另一端点不能是该环上的任何其他节点。

请判断原图是否包含一个作为子图的鱼形图;若存在,请输出任意一个这样的子图。

在本题中,子图定义为:从原图的边集中任取一个子集所构成的图。

示例 1 的示意图。红色边构成一个可能的鱼形图子图。

输入格式

The first line of input contains the integer tt (1≤t≤10001 \leq t \leq 1000), the number of test cases. The description of test cases follows.

The first line of each test case contains two integers, nn and mm (1≤n,m≤2 0001 \le n, m \le 2\,000) — the number of nodes and the number of edges.

Each of the next mm lines contains the description of an edge. Each line contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n, ui≠viu_i\neq v_i) — an edge connects node uiu_i to node viv_i.

It is guaranteed that no two edges connect the same unordered pair of nodes.

Furthermore, it is guaranteed that the sum of nn and the sum of mm over all test cases both do not exceed 2 0002\,000.

输入的第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤2 0001 \le n, m \le 2\,000),分别表示节点数和边数。

接下来的 mm 行每行描述一条边,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n,且 ui≠viu_i \neq v_i),表示一条连接节点 uiu_i 与节点 viv_i 的边。

保证不存在两条边连接同一对无序节点。

此外,保证所有测试用例的 nn 之和以及所有测试用例的 mm 之和均不超过 2 0002\,000。

输出格式

For each testcase, output "YES" if the graph contains a subgraph that is a Fish Graph, otherwise print "NO". If the answer is "YES", on the following lines output a description of the subgraph.

The first line of the description contains one integer kk — the number of edges of the subgraph.

On the next kk lines, output the edges of the chosen subgraph. Each of the kk lines should contains two integers uu and vv (1≤u,v≤n1\le u, v\le n, u≠vu\neq v) — the edge between uu and vv belongs to the subgraph. The order in which uu and vv are printed does not matter, as long as the two nodes are connected by an edge in the original graph. The order in which you print the edges does not matter, as long as the resulting subgraph is a fish graph.

If there are multiple solutions, print any.

对于每个测试用例,如果图中包含一个子图是“Fish Graph”,则输出 "YES";否则输出 "NO"。若答案为 "YES",则在接下来的行中输出该子图的描述。

描述的第一行包含一个整数 kk —— 该子图的边数。

接下来的 kk 行中,输出所选子图的各条边。每行包含两个整数 uu 和 vv(1≤u,v≤n1\le u, v\le n,u≠vu\neq v),表示边 (u,v)(u,v) 属于该子图。uu 和 vv 的输出顺序无关紧要,只要这两个节点在原图中确实由一条边相连即可。各条边的输出顺序也无关紧要,只要最终得到的子图是一个 Fish Graph 即可。

若存在多种可行解,输出任意一种即可。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    6
    5 4
    6 4
    4 3
    1 4
    2 1
    3 2
    YES
    5
    5 3
    2 3
    3 1
    4 3
    1 4
    NO

说明/提示

In the first example, a possible valid subgraph contains the cycle 1→2→3→4→11 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1. The special node of this cycle is node 44. The two extra edges 4−54 - 5 and 4−64 - 6 are both connected to 44, completing the Fish Graph.

In the second example, a possible valid subgraph contains the cycle 1→3→4→11 \rightarrow 3 \rightarrow 4 \rightarrow 1. The special node of this cycle is node 33. The two extra edges 3−23 - 2 and 3−53 - 5 are both connected to 33, completing the Fish Graph.

In the last example, it can be proven that there is no valid subgraph.

在第一个例子中,一个可能的有效子图包含环 1→2→3→4→11 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 1。该环的特殊节点是节点 44。两条额外的边 4−54 - 5 和 4−64 - 6 均与节点 44 相连,从而构成 Fish Graph。

在第二个例子中,一个可能的有效子图包含环 1→3→4→11 \rightarrow 3 \rightarrow 4 \rightarrow 1。该环的特殊节点是节点 33。两条额外的边 3−23 - 2 和 3−53 - 5 均与节点 33 相连,从而构成 Fish Graph。

在最后一个例子中,可以证明不存在有效子图。

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

首页