A15956 | Fish Graph
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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$ .
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$ .
输出格式
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.
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.
输入输出样例
输入 #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 \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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted