You are given an undirected graph with ‘N’ nodes and ‘M’ edges. Your task is to find out if it is possible to turn the graph into a directed graph such that all the vertices have positive in-degree.
Note:In-Degree of a vertex is the number of edges coming towards the node.
For example:

In the above graph, we can give direction to the first edge (0, 1), from vertex 0 to vertex 1 and the second edge (1, 2) from vertex 1 to vertex 2, and finally, the last edge (0, 2) from vertex 2 to vertex 0. So all nodes in-degree is 1. Hence the answer is “Yes”.

The first line of input contains a single integer ‘T’, denoting the number of test cases.
The first line of each input contains two space-separated integers, ‘N’ and ‘M’, denoting the number of vertices and the number of edges, respectively.
The following ‘M’ lines of input contain two space-separated integers, ‘A’ and ‘B’, denoting a bidirectional edge between vertices ‘A’ and ‘B’.
Output Format:
For each test case, If it is possible to make all vertices in-degree positive, then print “Yes”. Otherwise, print “No”.
Print the output of each test case in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
2 <= N <= 10^5
N - 1 <= M <= 10^5
0 <= A, B < N
Time Limit: 1 sec
2
3 3
0 1
1 2
2 0
3 2
0 1
1 2
Yes
No

For the first test case, we can give direction to the first edge (0, 1), from vertex 0 to vertex 1, and the second edge (1, 2) from vertex 1 to vertex 2, and the last edge (0, 2) from vertex 2 to vertex 0. So all nodes in-degree is 1. Hence the answer is “Yes”.

For the second test case, we can’t make all the vertices in degree positive. Hence the answer is “No”.
2
4 3
0 1
2 1
2 3
4 4
0 1
1 2
2 0
2 1
No
Yes
At which condition all nodes have positive in-degree.
In this approach, we have to make the graph directed such that all nodes are reachable from every node. This can be done if a cycle is present in the graph so that all vertices can have at least one incoming edge. If there is even one cycle is present in the graph, every node is reachable, as other nodes will only have incoming edges.
We define a dfs(graph, node, parent, visited) function that checks for a cycle in the graph and returns true or false. The graph is the adjacency list of the graph, the node is the current visiting node, the parent is the parent of the current node, and visited is a boolean array keeping track of the visited nodes.
Algorithm:
O(N + M), where N is the number of vertices of the graph and M is the number of edges.
The time complexity for doing a depth-first search on a graph is O(N + M). So the total time complexity becomes O(N + M).
O(N + M), where N is the number of vertices of the graph and M is the number of edges.
In the worst case, the adjacency list requires O(N + M) space. The space complexity O(N) is required for maintaining a visited array of vertices and the recursion stack in the worst case. So the total space complexity becomes O(N + M + N) = O(N + M).