Hello codeforces!
What is the easiest way to check if there is a perfect pairing in the tree?
Maybe Kuhn's algorithm? :)
Most likely you are thinking about dynamics on subtrees. This is indeed a good way, because it works for the size of the input. However, there is an easier way to check if a tree has a complete pairing.
Let szv be the subtree size of the vth vertex. Then I claim that there is a perfect pairing iff exactly half of all szv are even.
Proof:
Let szodd be the number of odd subtrees, and szeven be the number of even subtrees.
We first prove that szeven≤szodd.
Note that szv=∑uszu+1 (u — child of v). If szv is even, then at least one of the children of u has an odd subtree size, because their sum is odd. We pair each even szv with an odd szu (u — child v). Thus szeven≤szodd. Moreover, we proved that if szeven=szodd, then there is a perfect pairing in the tree by explicitly choosing every even szv to be a pair.
Now we prove that if szeven≠szodd, then there is no perfect pairing in the tree. Suppose there is. Then it has an edge connecting v,u such that szv≡szu≡1 (mod 2). Let v — be the parent of u. Then note that each edge is either entirely contained in a subtree of v or not. In both cases, the edge takes an even number of vertices from subtree v, and hence they will also take an even number of vertices in total, but szv≡1 (mod 2) — contradiction.
Thus szodd=szeven is equivalent to having a complete pairing.
Thanks for reading!