310344: CF1817B. Fish Graph

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Fish Graphtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a simple undirected graph with $n$ nodes and $m$ edges. Note that the graph is not necessarily connected. The nodes are labeled from $1$ to $n$.

We define a graph to be a Fish Graph if it contains a simple cycle with a special node $u$ belonging to the cycle. Apart from the edges in the cycle, the graph should have exactly $2$ extra edges. Both edges should connect to node $u$, 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.
Input

The first line of input contains the integer $t$ ($1 \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, $n$ and $m$ ($1 \le n, m \le 2\,000$) — the number of nodes and the number of edges.

Each of the next $m$ lines contains the description of an edge. Each line contains two integers $u_i$ and $v_i$ ($1 \leq u_i, v_i \leq n$, $u_i\neq v_i$) — an edge connects node $u_i$ to node $v_i$.

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

Furthermore, it is guaranteed that the sum of $n$ and the sum of $m$ over all test cases both do not exceed $2\,000$.

Output

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 $k$ — the number of edges of the subgraph.

On the next $k$ lines, output the edges of the chosen subgraph. Each of the $k$ lines should contains two integers $u$ and $v$ ($1\le u, v\le n$, $u\neq v$) — the edge between $u$ and $v$ belongs to the subgraph. The order in which $u$ and $v$ 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.

ExampleInput
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
Output
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
Note

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

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

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

Input

题意翻译

定义“鱼图”为满足以下条件的无向图: - 该图包含正好 $1$ 个环,环上有 $1$ 个特殊的结点 $u$,$u$ 除了连在环上的 $2$ 条边外还有正好 $2$ 条边,每一条边连向一个环外的结点(即,若环的大小为 $s$,整个图一共有 $s+2$ 个结点)。 现在给定一个简单无向图,问该图中是否有一个子图为“鱼图”。一个无向图的子图即为原图中删去若干个点和若干条边所形成的图。如果存在,还需要构造出其中任意一个满足要求的子图。

Output

题目大意:

给定一个简单无向图,包含n个节点和m条边,图中可能不连通。节点的编号从1到n。

定义一个图是“鱼形图”(Fish Graph),如果它包含一个简单环,且环中有一个特殊节点u。除了环中的边之外,图中还应该有恰好2条额外的边,这两条边都应该连接到节点u,但不应连接到环中的任何其他节点。

确定图中是否包含一个子图是“鱼形图”,如果存在,找出这样的一个子图。

在这个问题中,我们定义一个子图是通过取原图中任意边的子集所得到的图。

输入输出数据格式:

输入:

第一行包含一个整数t(1≤t≤1000),表示测试用例的数量。接下来是每个测试用例的描述。

每个测试用例的第一行包含两个整数n和m(1≤n, m≤2000)——节点的数量和边的数量。

接下来的m行,每行包含一条边的描述,格式为两个整数ui和vi(1≤ui, vi≤n,ui≠vi)——表示节点ui与节点vi之间的边。

保证没有两条边连接同一对节点。

此外,保证所有测试用例中n的总和与m的总和均不超过2000。

输出:

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

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

在接下来的k行中,输出子图所选的边。每行包含两个整数u和v(1≤u, v≤n,u≠v)——表示u与v之间的边属于子图。只要这两个节点在原图中由一条边连接,u和v的打印顺序不重要。只要结果子图是“鱼形图”,打印边的顺序也不重要。

如果有多个解决方案,输出其中任何一个。题目大意: 给定一个简单无向图,包含n个节点和m条边,图中可能不连通。节点的编号从1到n。 定义一个图是“鱼形图”(Fish Graph),如果它包含一个简单环,且环中有一个特殊节点u。除了环中的边之外,图中还应该有恰好2条额外的边,这两条边都应该连接到节点u,但不应连接到环中的任何其他节点。 确定图中是否包含一个子图是“鱼形图”,如果存在,找出这样的一个子图。 在这个问题中,我们定义一个子图是通过取原图中任意边的子集所得到的图。 输入输出数据格式: 输入: 第一行包含一个整数t(1≤t≤1000),表示测试用例的数量。接下来是每个测试用例的描述。 每个测试用例的第一行包含两个整数n和m(1≤n, m≤2000)——节点的数量和边的数量。 接下来的m行,每行包含一条边的描述,格式为两个整数ui和vi(1≤ui, vi≤n,ui≠vi)——表示节点ui与节点vi之间的边。 保证没有两条边连接同一对节点。 此外,保证所有测试用例中n的总和与m的总和均不超过2000。 输出: 对于每个测试用例,如果图中包含一个子图是“鱼形图”,则输出“YES”,否则输出“NO”。如果答案为“YES”,在接下来的行中输出子图的描述。 描述的第一行包含一个整数k——子图的边数。 在接下来的k行中,输出子图所选的边。每行包含两个整数u和v(1≤u, v≤n,u≠v)——表示u与v之间的边属于子图。只要这两个节点在原图中由一条边连接,u和v的打印顺序不重要。只要结果子图是“鱼形图”,打印边的顺序也不重要。 如果有多个解决方案,输出其中任何一个。

加入题单

上一题 下一题 算法标签: