Cowntagion

Algorithm

Starting at the first infected farm, duplicate until there are enough infected cows to infect all neighboring uninfected farms, and repeat until there are no more uninfected cows.

Pseudocode

While there are uninfected farms:
    For each infected farm:
        Double until there are enough cows to infect all neighboring uninfected farms.
        Send a cow to each of those farms to infect them.

Python

n = int(input())
cows = [0]*n # infected cows in each farm
cows[0] = 1 # Infect first cow
newInfected = [0] # Newly infected farms
adj = [[] for _ in range(n)] # Adjacency List
moves = 0
for _ in range(n-1):
    road = list(map(int,input().split()))
    adj[road[0]-1].append(road[1]-1)
    adj[road[1]-1].append(road[0]-1)
while 0 in cows:
    justInfected = []
    for farm in newInfected:
        canInfect = 0
        for neighbor in adj[farm]:
            if cows[neighbor] == 0:
                canInfect += 1
                moves += 1 # Cost to move infected cow to farm
                cows[neighbor] += 1
                justInfected.append(neighbor)
        while cows[farm] <= canInfect:
            cows[farm] *= 2
            moves += 1
        cows[farm] -= canInfect
    newInfected = list(justInfected)
print(moves)