Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Perfect matching in tree

Revision en3, by PvPro, 2024-12-02 00:19:07

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 szevenszodd.

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 szevenszodd. 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 szevenszodd, then there is no perfect pairing in the tree. Suppose there is. Then it has an edge connecting v,u such that szvszu1 (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 szv1 (mod 2) — contradiction.

Thus szodd=szeven is equivalent to having a complete pairing.

Thanks for reading!

Tags matching, tree

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English PvPro 2024-12-02 00:52:42 1 Tiny change: 'sz_{even} {\leq sz_{o' -> 'sz_{even} \leq sz_{o'
en4 English PvPro 2024-12-02 00:52:29 175
ru3 Russian PvPro 2024-12-02 00:48:37 75
en3 English PvPro 2024-12-02 00:19:07 0 (published)
en2 English PvPro 2024-12-02 00:18:48 138 (saved to drafts)
en1 English PvPro 2024-12-02 00:15:26 1802 Initial revision for English translation
ru2 Russian PvPro 2024-12-02 00:07:38 0 (опубликовано)
ru1 Russian PvPro 2024-12-02 00:05:46 1907 Первая редакция (сохранено в черновиках)