Problem Summary
Given a tree with N nodes, find the maximum number of edges that can be removed from it to get a forest such that each tree of the forest contains an even number of nodes.
Solution
Apparently, the answer is 0 if n is odd. So we only need to consider when n is even.
First, we can use DFS to calculate the number of children of each node. (Assume node 1 is root.)
Then we do another DFS to try to greedily cut edges. That is, for an edge (x,y), we cut it and increase the answer by 1, whenever the number of children of node y is even.
Code
|
|