Farmer John and his fellow farmers have been working nonstop to control the spread of the terrible bovine disease COWVID-19 across their farms.
Together, they oversee a collection of NN farms (1≤N≤1051≤N≤105), conveniently numbered 1…N1…N. The farms are connected by a set of N−1N−1 roads such that any farm can be reached from farm 1 by some sequence of roads.
Unfortunately, a cow in farm 1 has just tested positive for COWVID-19. None of the other cows at that farm or at any other farms have the disease yet. However, knowing the contagious nature of the disease, Farmer John anticipates exactly one of the following adverse events on each successive day:
(1) In a single farm, a "superspreader" event causes the number of cows at that farm with COWVID-19 to double; or
(2) A single cow with COWVID-19 moves along a road from one farm to an adjacent farm.
Farmer John is worried about how fast the outbreak might spread. Please help him by determining the minimum possible number of days before it could be the case that at least one cow in every farm has the disease.
The first line contains the single integer NN. The next N−1N−1 lines each contain two space-separated integers aa and bb describing a road between farms aa and bb. Both aa and bb are in the range 1…N1…N.
The minimum number of days until the outbreak could reach every farm.
4 1 2 1 3 1 4
5
One possible sequence of events corresponding to this example is the following: the number of sick cows in farm 1 doubles and then doubles again, so that after two days, there are 4 sick cows in farm 1. In each of the next 3 days, a sick cow travels from farm 1 to each of farms 2, 3, and 4 respectively. After 5 days, at least 1 sick cow exists at each farm.
Problem credits: Dhruv Rohatgi
[/hide]
(Analysis by Dhruv Rohatgi )
Consider any "optimal" sequence of events: a minimum-length sequence of moves or doublings that causes every farm to have a sick cow. Observe that if an infected cow at any time moves from some farm ii to an adjacent farm jj which already has an infected cow, we can replace this event with a superspreader event at farm jj. This replacement causes an equally optimal sequence of events, because it doesn't decrease the ensuing number of infected cows at either farm. So without loss of generality, we can assume that in the optimal solution, an infected cow never moves to a farm with another infected cow.
Additionally, suppose that at some point in time, a farm ii has exactly one infected cow, and this cow moves to an adjacent farm jj. Then there'll be no infected cow in farm ii, so at some later time, some infected cow has to move *into* farm ii. Let's insert into the event sequence a superspreader event at farm ii, right before the infected cow leaves farm ii. If we keep the rest of the sequence the same, farm ii will subsequently always have at least one more cow than in the original sequence. So we can cut out the event where an infected cow moves back into farm ii. This produces an optimal sequence without increasing the number of moves. So once again, we can assume without loss of generality that the described event never happens.
This means that if we look at the set of *infected farms*, i.e. farms which have an infected cow, this set never shrinks, and every time a move event happens, the set expands by exactly one. Essentially, the set forms an ever-expanding subtree around farm 11.
So our (specifically constructed) optimal sequence of events has exactly n−1n−1 move events, one for each farm besides farm 11: the event where this farm enters the infected set. How many superspreader events do we need?
Root the tree at farm 11 and consider any subtree rooted at farm ii. Eventually there will be exactly one infected cow in farm ii (and none yet in its subtree). If the farm has d(i)d(i) children, then we can do ⌈log2(d(i)+1)⌉⌈log2(d(i)+1)⌉ consecutive superspreader events at farm ii, and then move one infected cow to each child. It's always better to do all the superspreader events at farm ii before moving cows out to the children, so this is optimal.
As a result, the total number of superspreader events needed is ∑i⌈log2(d(i)+1)⌉∑i⌈log2(d(i)+1)⌉ (summing over all farms with at least one child) and the minimum number of events needed to infect all farms is this quantity plus n−1n−1.
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define MAXN 100000 int N; int d[MAXN]; int main() { cin >> N; int a,b; for(int i=0;i<N-1;i++) { cin >> a >> b; a--,b--; d[a]++, d[b]++; } int ans = N-1; // number of move events for(int i=0;i<N;i++) if(d[i] > 1 || i == 0) // check that i is not leaf node in tree { int children = d[i]; if(i!=0) children--; // compute ceil(log(children + 1)) int log_children = 0; int pow = 1; while(pow < children + 1) log_children++, pow *= 2; ans += log_children; } cout << ans << '\n'; }
[/hide]
© 2025. All Rights Reserved. 沪ICP备2023009024号-1