
In-Degree of a vertex is the number of edges coming towards the node.

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’.
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.
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
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:
As we have discussed in the previous approach, we are trying to find a cycle 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. In this approach, we will use a breadth-first search to find a cycle in the graph.
We define a bfs(graph, start, 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 start is the starting node, and visited is a boolean array keeping track of the visited nodes.
Algorithm: