Cowntact Tracing
Time limit2sMemory limit1024 MB
Given a tree of cows and which cows are infected after some nights, find the fewest cows that could have been infected at the start, for each night count.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Farmer John has () cows labeled . The connections between the cows form a tree. Unfortunately, a sickness is spreading through the herd.
Initially, some cows are infected. Every night, an infected cow passes the sickness to its neighbors. Once a cow is infected, it stays infected. After some number of nights, Farmer John notices a problem and tests his cows to see who has the sickness.
You are given () different values for the number of nights, each an integer in the range . For each number of nights, find the minimum number of cows that could have been infected at the start, or report that the number of nights is inconsistent with the given information.
Input
The first line contains .
The next line contains a bit string of length , where the th bit is 1 if the th cow is infected and 0 otherwise. At least one cow is infected.
The next lines contain the edges of the tree.
Then comes , followed by the values for the number of nights.
Output
Print lines, one answer for each number of nights. Print if the case is impossible.