Блог пользователя wuhudsm

Автор wuhudsm, история, 9 месяцев назад, По-английски

Reminder: We welcome you to participate in the official DIV1/DIV2 round scheduled on the 31st!

A

Idea:frostcat

solution
code(C++)
Rate the Problem

B

Idea:frostcat

solution
code(C++)
Rate the Problem

C1 and C2

Idea:Snowdust

solution(easy version)
solution(hard version)
code(easy version)
code(hard version)
Rate the Problem

D

Idea:Davy_D._Kaosar

solution
code
Rate the Problem

E

Idea:ProofByContradiction_

solution
code
Rate the Problem

F

Idea:wuhudsm

solution1
solution2 by Ming_Xu
code(solution 1)
code(solution 2)
Rate the Problem
Разбор задач TheForces Round #43 (DIV2-Forces)
  • Проголосовать: нравится
  • +28
  • Проголосовать: не нравится

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Orz round

»
9 месяцев назад, скрыть # |
Rev. 4  
Проголосовать: нравится 0 Проголосовать: не нравится

These are good problems. Thanks for sharing your upsolving, bro!

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Great round, I really enjoyed E and F!

»
9 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +5 Проголосовать: не нравится

E can be solved in $$$\mathcal O(n)$$$:

(The approach in the editorial can be optimized AFAIK, here's my (different) approach)

Let's assume that we can delete the full tree and let's fix any vertex $$$u$$$ (and it's subtree). Then we observe that, in order to delete the subtree of $$$u$$$ (including $$$u$$$), we have two cases:

1) all deletion operations to delete the subtree have both endpoints in the subtree of $$$u$$$. in particular, this will be the case for the root $$$1$$$. 2) all but one deletion operation has both endpoints in the subtree of $$$u$$$. Then, there is one more operation that has one endpoint in some subtree of $$$u$$$ and the other endpoint is outside of $$$u$$$. We denote this endpoint by $$$X_u$$$. Note that as long as we can delete all other vertices, it doesn't matter where the endpoint (inside the subtree of $$$u$$$) is.

This motivates the following "tree-dp"/"dfs" — for every vertex, we store a sequence of operation to achieve case 1 or case 2:

  • for leaves, we can clearly not delete the subtree (=the leaf) with both endpoints in that subtree, since $$$u \ne v$$$. This means that case 1 is "not possible" and case 2 is "possible" with an empty sequence (and the endpoint inside the subtree of that leaf will be the leaf itself)
  • for non-leaves $$$u$$$, we compute the sequences for all (direct) children. If for any child, there is no sequence for case 1 nor for case 2, then there will also be no such sequences for $$$u$$$ itself.

Now, let $$$k$$$ denote the number of children for which case 1 is possible but case 2 isn't. Then, there are multiple cases:

  1. $$$k = 0$$$: Case 2 is trivial since we can concatenate all the Case 1 sequences of the children and remember that we need $$$u$$$ as an endpoint to delete the entire subtree. We can find a sequence for case 1 if any child $$$v$$$ has a sequence for case 2. We then concatenate these sequences and add $$$(u, X_v)$$$ which will delete the rest of the subtree.
  2. $$$k = 1$$$: Let $$$v$$$ be the child for which only case 2 is possible. This case is essentially the same as $$$k = 0$$$, but instead of $$$X_u = u$$$, we set $$$X_u = X_v$$$ (which may not be $$$v$$$).
  3. $$$k = 2$$$: Let $$$v_1$$$ and $$$v_2$$$ be the children for which case 2 is not possible. Case $$$2$$$ is not possible now, since we can't set $$$X_u = X_{v_1}$$$ or $$$X_u = X_{v_2}$$$. The only possibility to delete the full subtree of $$$u$$$ is to perform the operation $$$(X_{v_1}, X_{v_2})$$$
  4. $$$k \gt 2$$$: impossible, see case $$$k = 2$$$

The final answer is the "case 1" sequence for the root (if it exists). Note that we have already shown correctness in the beginning.

This can be easily implemented in $$$\mathcal O(n^2)$$$. If we precompute the optimal actions, we can use Linked Lists to merge the actions, resulting in a runtime of $$$\mathcal O(n)$$$.

My submission.