Three problems which are remarkably similar

Revision en2, by Sul_A., 2024-12-13 13:47:10

I found an unusual similarity between three problems: a POI 2016 task, an Educational F, and a Div. 3 G. While these problems are formulated in different ways, they can be solved in incredibly similar ways. I'm not claiming foul play; this is likely a case of parallel thinking. Spoilers ahead.

POI 2016 Parade

The task can be summed up as follows: Given a tree, choose a path that maximizes the number of "adjacent" edges. "Adjacent" as in one of its endpoints is in the path, while the other is not. The path has to consist of 2 or more nodes.

For a given path $$$v_1, v_2, ..., v_k$$$, the number of adjacent edges is $$$(\sum_{i=1}^{k} deg(v_i) - 2) + 2$$$. This is because for each node, its contribution is its incident edges, except the $$$2$$$ which are on the path. The $$$2$$$ at the end is to compensate for the ends of the path which only have one neighbor in the path. So let $$$dp(u)$$$ be the best path from the node $$$u$$$ to a node in its subtree. The transitions are: $$$dp(u)$$$ = $$$deg(u) - 2 + \max dp(v, 0)$$$, or $$$0$$$ if $$$u$$$ is a leaf. The answer will simply be the maximum $$$dp(u) + 2$$$ or maximum $$$deg(u) + dp(v_1) + dp(v_2)$$$ for $$$v_1$$$ and $$$v_2$$$ being $$$2$$$ distinct children of $$$u$$$. There is an edge case where the tree is a star. (We have to subtract 1 from the answer).

Code

Education Round 74 1238F - The Maximum Subtree (rated 2200)

The problem is as follows: Define a \bf{good} tree as a tree such that for each node, we can assign it a segment, and two nodes will share and edge iff their segments intersect. (As in there exists a point that belongs to both of them). You have to find the largest possible good subtree (connected subgraph) of the given tree.

The most crucial observation is as follows: in a good tree, each node has at most $$$2$$$ non-leaf neighbors. Its leaf neighbors will be contained in its segment, and its $$$1$$$ or $$$2$$$ non-leaf neighbors will share one of its endpoints. If there are $$$3$$$ non-leaf neighbors, They would have to form a cycle. So every good subtree corresponds to a path in the original tree, and the value of a path is equal to $$$(\sum_{i=1}^{k} deg(v_i) - 1) + 2$$$. Sound familiar? We can write the same DP, just with $$$-1$$$ instead of $$$-2$$$. Code can be found here

Tags poi, tasks, plagiarism, trees, dp, dfs

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Sul_A. 2024-12-13 14:08:16 54 (published)
en3 English Sul_A. 2024-12-13 14:06:32 607 Tiny change: ' Define a \bf{good} tree as a' -> ' Define a $\bf{good}$ tree as a'
en2 English Sul_A. 2024-12-13 13:47:10 491
en1 English Sul_A. 2024-12-13 13:37:00 3109 Initial revision (saved to drafts)