CF1665C Solution
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
The reading experience is better on the blog.
1. Problem Statement:
You are given a tree with nodes. Initially, every node is healthy. Each second, you can perform the following two operations:
- Spread: For a node, if at least one of its child nodes is infected, then another of its child nodes can be infected. (If multiple nodes satisfy the condition, multiple nodes can spread the infection during that second.)
- Injection: You can choose any node in the tree and infect it. (Only one additional node can be infected in one second.)
Find the minimum number of seconds required to infect the entire tree.
2. Approach:
After reading the problem, we need to notice that it says a node can spread the virus to its siblings, rather than to its child nodes. Therefore, every level of the tree is completely independent, and it is impossible to spread the virus from one level to another.
Therefore, at the beginning, we certainly need to inject the virus into at least one child node of every node (which specific one does not matter), so that more nodes can be infected each second (according to operation 1).
Then whose child node should be injected first? Consider that child nodes injected earlier have more time to spread the virus to more child nodes. Therefore, we should first inject the nodes that have more child nodes.
(If we first inject a node with fewer child nodes, all of that node’s child nodes may already be infected before we finish injecting all nodes, meaning that a lot of time is wasted.)
After ensuring that every node has at least one injected child node, we can also inject the nodes with especially many child nodes, preventing some especially large nodes from being infected too slowly through spreading alone.
Of course, we cannot simply sort by the number of child nodes as before and continually inject the node with the most child nodes. Doing so might cause a node and its child nodes to become fully infected quickly while other nodes still require a long time.
For example, suppose two nodes have 100 and 98 healthy child nodes respectively after the injections. If we sort them directly and inject the larger node first, infecting the entire tree will still require seconds. (The division by 2 is because spreading and injection infect nodes at the same time, while 98 minus 50 and then divided by 2 is because we only start injecting the node with 98 child nodes after finishing the node with 100.) However, if we inject both nodes concurrently, only seconds are needed. (The two nodes can be regarded as one node; each second, two child nodes are infected by spreading and one is infected by injection.)
Therefore, we can push the numbers of healthy child nodes into a heap and inject the one with the most child nodes each time.
3. Code:
I will not explain too much here; the code contains detailed comments.
1 |
|
Finally, I hope this solution is helpful to you. If you have any questions, you can contact me through the comments or a private message.






![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)