Divisibility tree
Submissions: 610   Accuracy:


  Difficulty: Medium   Marks: 4

One day Ram was drawing and suddenly he saw a drawing book which consists of several simple connected graph with no cycle or in other words tree. He thought of doing something interesting from the drawing. Consider the tree consist of N nodes from 1 to N. He found out that all the trees are rooted at node 1. Then he thinks of removing the edges from the following diagrams to convert it into disjoint union tree and he want to find out the number of edges he can remove so that the vertices or nodes of each connected component left is divisible by 2 . He wants to find out the edges as much as possible meaning that there should be maximum number of edges. As he is very busy for his upcoming drawing competition, so he urgently wants to reach for his drawing classes. So help him finding the edges.

The first line of input will contain an integer T denoting the no of test cases. Then T test cases follow. Each line of the test case will contain two integers p and q, denoting the number of nodes and number of edges respectively. The next line will contain q space separated pairs x and y which will define the edge of the tree.


For each test case in a new line print maximum number of edges which should be removed.

2<=p and q <= 100
1<=x and y<=1000


10 9
2 1 3 1 4 3 5 2 6 1 7 2 8 6 9 8 10 8



Take two integers at a time i.e. 2 is connected with 1, 3 is connected with 1,4 is connected with 3, 5 is connected with 2 and so on. Fig will understand you better.

Original tree:
After removing edge 1-3 and 1-6. So ans is 2 because
all nodes are even                                           

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: vaibhav garg
Author: justrelax

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.