Fish Graph
Contest: Codeforces Round 869 (Div. 1)
Difficulty rating: 1900
Tags: brute force, constructive algorithms, dfs and similar, graphs

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 format:
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 format:
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.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

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.

Example input:
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

Example 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

Editorial:
Try to prove this, and see if your proof can be turned into an algorithm.
Firstly, let's try to find some necessary conditions for a Fish Subgraph to exist. Because the Fish Graph has a cycle with two extra edges attached, the original must contain a cycle with a node in the cycle having degree at least 4. It turns out this condition is actually sufficient, let's prove this:
So, assume a graph contains a cycle, and one of the nodes in the cycle has $$$deg(u) \geq 4$$$. We will only look at this cycle, and two extra edges connected to the special node that are not cycle edges, and remove all the other edges from consideration.
These two extra edges could have both endpoints inside the cycle, creating diagonal(s) and not the fins of the Fish Graph we want. If both edges don't form diagonals of the cycle, we've found a Fish Graph. Otherwise, let's label the nodes in the cycle $$$v_1 v_2 \dots v_k$$$, and label the two extra edges $$$e_1$$$ and $$$e_2$$$. Look at the diagonal that connects nodes $$$v_1$$$ and $$$v_d$$$, with $$$d>2$$$ as small as possible. Using it, we find a smaller cycle $$$v_1 v_2 ... v_d$$$. To finish the proof, we notice that one of the edges from $$$\{e_1,e_2\}$$$ and the edge $$$v_1 v_k$$$ now have become free to use as fins of the Fish Graph, as they are no longer diagonals of the smaller cycle. $$$\square$$$
This proof can be turned into an algorithm solving the problem.
First off, we need to find any cycle with a node with degree $$$\geq 4$$$. Let's brute force the node $$$u$$$, which must have degree $$$\geq 4$$$. Then brute force the first edge of the cycle $$$uv$$$ (by looping over the adjacency list of node $$$u$$$). Now to find any cycle, it suffices to find a path from $$$v$$$ to $$$u$$$, temporarily deleting the edge $$$uv$$$. This can be done with DFS or BFS. When a cycle is found, all edges except $$$2$$$ extra edges can be removed from consideration, and the proof for sufficiency can be implemented to fix possible diagonals. For BFS it is even easier, because it already finds the shortest path from $$$u$$$ to $$$v$$$, making diagonals impossible. The time complexity will be $$$\mathcal{O}(m \cdot (n+m))$$$, because the algorithm loops over all edges, and does a graph traversal for each of them.
Bonus: How do you make this algorithm $$$\mathcal{O}(n+m)$$$?