PvPro's blog

By PvPro, history, 3 months ago, In English

How do you usually build HLD?

Usually we calculate $$$sz_v$$$ — size of subtree of $$$v$$$. Then from $$$v$$$ we go to $$$u$$$ with largest $$$sz_u$$$ ($$$u$$$ is child of $$$v$$$). And this is still good way, but today I want to show you something different.

What if from $$$v$$$ we go to the random vertex $$$u$$$ in the whole subtree of $$$v$$$. It is the same as choosing to go to the $$$u$$$ ($$$u$$$ is child of $$$v$$$) with probability $$$\frac{sz_u}{sz_v - 1}$$$, so the probability of edge $$$v, u$$$ to be heavy is $$$\frac{sz_u}{sz_v - 1}$$$. Intuitively we should go the largest subtree.

Proof

Let's proof it works well. For vertex $$$v$$$ the expected value number of light edges on the way to root is $$$\sum_{i = 1}^{i \lt |u|}{1-\frac{sz_{u_i}}{sz_{u_{i + 1}} - 1}}$$$, where $$$u$$$ is vertexes on way from $$$v$$$ to root. Let $$$a_i$$$ be $$$sz_{u_i}$$$. Then $$$a$$$ is an increasing array and we know that $$$a_{h_v}$$$ equals to the size of the whole tree. $$$\sum_{i = 1}^{i \lt |a|}{1-\frac{a_i}{a_{i + 1} - 1}} \leq \sum_{i = 1}^{i \lt |a|}{\frac{a_{i + 1} - a_i}{a_{i + 1}}}$$$.

$$$\frac{a_{i+1} - a_i}{a_{i+1}} \le \int_{a_i}^{a_{i+1}} \frac{1}{x} \, dx = \ln(a_{i+1}) - \ln(a_i)$$$

$$$\sum_{i = 1}^{i \lt |a|}{1-\frac{a_i}{a_{i + 1} - 1}} \leq ln(a_{|a|})$$$.

So we have expected value number of light edges on the way is less than $$$ln(n)$$$.

Usages

remain to the reader

Full text and comments »

  • Vote: I like it
  • +135
  • Vote: I do not like it

By PvPro, history, 12 months ago, In English

I hope everyone enjoyed the tasks, and thank you for participating.

2110A - Fashionable Array

Editorial
Solution

2110B - Down with Brackets

Editorial
Solution

2110C - Racing

Editorial
Solution

2110D - Fewer Batteries

Editorial
Solution

2110E - Melody

Editorial
Solution

2110F - Faculty

Editorial
Solution

Full text and comments »

  • Vote: I like it
  • +160
  • Vote: I do not like it

By PvPro, history, 12 months ago, translation, In English

Hello, codeforces!

We are glad to invite you to Codeforces Round 1026 (Div. 2), which will start at May/24/2025 17:35 (Moscow time). This round will be rated for all participants with a rating below 2100. You will have 2 hours to solve 6 problems. The problems were prepared by XaRDKoDblCH and PvPro.

We would like to thank everyone who made this round possible:

Score distribution: 500 — 750 — 1500 — 2000 — 2250 — 3000

Our round will be dedicated to a cyberpunk theme, so get ready to save the world from robots! ;)

Good luck!

UPD: The contest is over! Congrats the winners:

via all participants:

  1. maspy

  2. Geothermal

  3. 9ovem

  4. peti1234

  5. turmax

via div.2 participants:

  1. 9ovem

  2. still_still_stellar

  3. Hellia

  4. Badint

  5. cuongaaaa

UPD: Editorial

Full text and comments »

  • Vote: I like it
  • +419
  • Vote: I do not like it

By PvPro, history, 17 months ago, translation, In English

Hello codeforces!

What is the easiest way to check if there is a perfect matching 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 perfect matching.

Let $$$sz_v$$$ be the subtree size of the $$$v$$$th vertex. Then I claim that there is a perfect matching iff exactly half of all $$$sz_v$$$ are even.

Proof:

Let $$$sz_{odd}$$$ be the number of odd subtrees, and $$$sz_{even}$$$ be the number of even subtrees.

We first prove that $$$sz_{even} \leq sz_{odd}$$$.

Note that $$$sz_v = \sum_{u}^{} sz_u + 1$$$ ($$$u$$$ — child of $$$v$$$). If $$$sz_v$$$ 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 $$$sz_v$$$ with an odd $$$sz_u$$$ ($$$u$$$ — child $$$v$$$). Thus $$$sz_{even} \leq sz_{odd}$$$. Moreover, we proved that if $$$sz_{even} = sz_{odd}$$$, then there is a perfect matchingg in the tree by explicitly choosing every even $$$sz_v$$$ to be a pair.

Now we prove that if $$$sz_{even} \neq sz_{odd}$$$, then there is no perfect matching in the tree. Suppose there is. Then it has an edge connecting $$$v, u$$$ such that $$$sz_v \equiv sz_u \equiv 1\space (mod\space 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 $$$sz_v \equiv 1\space(mod\space 2)$$$ — contradiction.

Furthermore, because of the inequality $$$sz_{even} \leq sz_{odd}$$$ it is true that $$$sz_{odd} = sz_{even}$$$ is equivalent to the presence of a perfect matching in the forest.

Thanks for reading!

Full text and comments »

  • Vote: I like it
  • +185
  • Vote: I do not like it