#P9983. [USACO23DEC] Cowntact Tracing P

    ID: 11325 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP贪心USACO点分治2023O2优化树形 DP

[USACO23DEC] Cowntact Tracing P

Problem Description

Farmer John has NN cows numbered 1N1 \dots N (2N1052 \le N \le 10^5), 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 QQ (1Q201 \le Q \le 20) distinct numbers of nights, each an integer in the range [0,N][0, N]. 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 NN.

The next line contains a bit string of length NN consisting of 11 and 00. A 11 means the cow is infected, and a 00 means the cow is still uninfected after some number of nights.

The next N1N - 1 lines describe the edges of the tree.

Then QQ and the QQ numbers of nights are given.

Output Format

Output QQ lines, each being the answer for the corresponding number of nights. If there is no solution, output 1-1.

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 33 was infected initially. For the fifth query (11 night), one possibility is that cows 22 and 44 were infected initially. For the sixth query (00 nights), one possibility is that all five cows were infected initially.

Sample Explanation 2

For the first query (00 nights), one possibility is that all ten cows were infected initially. For the second query (11 night), one possibility is that cows 22, 77, and 99 were infected initially. For the third query (22 nights), one possibility is that cows 22 and 99 were infected initially. For the fourth through the eleventh queries, one possibility is that only cow 77 was infected initially.

Sample Explanation 3

For the first query (00 nights), one possibility is that cows 11, 22, and 33 were infected initially. For the second query (11 night), one possibility is that only cow 22 was infected initially. For the third query (22 nights), one possibility is that only cow 11 was infected initially. For the fourth through the sixth queries, it is impossible to satisfy the conditions in the problem.

Testdata Properties

  • Testdata 44-55 satisfies N10N \le 10.
  • Testdata 66-88 satisfies that all cows are infected.
  • Testdata 99-1111 satisfies N400N \le 400.
  • Testdata 1212-2323 has no additional constraints.

Translated by ChatGPT 5