Under construction

Still being built. Things may change

Cowntact Tracing

Time limit2sMemory limit1024 MB

Summary
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 NN (2≤N≤1052 \le N \le 10^5) cows labeled 1…N1 \dots N. 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 QQ (1≤Q≤201 \le Q \le 20) different values for the number of nights, each an integer in the range [0,N][0, N]. 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 NN.

The next line contains a bit string of length NN, where the iith bit is 1 if the iith cow is infected and 0 otherwise. At least one cow is infected.

The next N−1N-1 lines contain the edges of the tree.

Then comes QQ, followed by the QQ values for the number of nights.

Output

Print QQ lines, one answer for each number of nights. Print −1-1 if the case is impossible.

Examples3

  1. Example 1

    Input
    5
    11111
    1 2
    2 3
    3 4
    4 5
    6
    5
    4
    3
    2
    1
    0
    
    Expected output
    1
    1
    1
    1
    2
    5
    
  2. Example 2

    Input
    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
    
    Expected output
    10
    3
    2
    1
    1
    1
    1
    1
    1
    1
    1
    
  3. Example 3

    Input
    5
    11100
    1 2
    2 3
    3 4
    4 5
    6
    0
    1
    2
    3
    4
    5
    
    Expected output
    3
    1
    1
    -1
    -1
    -1