#P9983. [USACO23DEC] Cowntact Tracing P
[USACO23DEC] Cowntact Tracing P
Problem Description
Farmer John has cows numbered (), and the relationships between the cows can be described by a tree. Unfortunately, a disease is spreading.
Initially, some cows are infected. Each night, infected cows spread the disease to their neighbors. Once a cow is infected, she remains infected forever. After several nights, Farmer John realizes what is happening, so he tests the cows to determine which cows are infected.
You are given () distinct numbers of nights, each an integer in the range . For each number of nights, find the minimum possible number of cows that could have been infected initially, or report that this number of nights is inconsistent with the given information.
Input Format
The first line contains an integer .
The next line contains a bit string of length consisting of and . A means the cow is infected, and a means the cow is still uninfected after some number of nights.
The next lines describe the edges of the tree.
Then and the numbers of nights are given.
Output Format
Output lines, each being the answer for the corresponding number of nights. If there is no solution, output .
5
11111
1 2
2 3
3 4
4 5
6
5
4
3
2
1
0
1
1
1
1
2
5
10
1111111111
1 2
2 3
2 4
2 5
2 6
6 7
7 8
8 9
9 10
11
0
1
2
3
4
5
6
7
8
9
10
10
3
2
1
1
1
1
1
1
1
1
5
11100
1 2
2 3
3 4
4 5
6
0
1
2
3
4
5
3
1
1
-1
-1
-1
Hint
Sample Explanation 1
For the first four queries, one possibility is that only cow was infected initially. For the fifth query ( night), one possibility is that cows and were infected initially. For the sixth query ( nights), one possibility is that all five cows were infected initially.
Sample Explanation 2
For the first query ( nights), one possibility is that all ten cows were infected initially. For the second query ( night), one possibility is that cows , , and were infected initially. For the third query ( nights), one possibility is that cows and were infected initially. For the fourth through the eleventh queries, one possibility is that only cow was infected initially.
Sample Explanation 3
For the first query ( nights), one possibility is that cows , , and were infected initially. For the second query ( night), one possibility is that only cow was infected initially. For the third query ( nights), one possibility is that only cow was infected initially. For the fourth through the sixth queries, it is impossible to satisfy the conditions in the problem.
Testdata Properties
- Testdata - satisfies .
- Testdata - satisfies that all cows are infected.
- Testdata - satisfies .
- Testdata - has no additional constraints.
Translated by ChatGPT 5