#P11032. 『DABOI Round 1』Develop a Tree

『DABOI Round 1』Develop a Tree

Background

Little Z cannot stand trees, so he always wants to randomly add some edges to a tree. He thinks bipartite graphs are very harmonious, so he wants to know how many ways there are to turn a tree into a bipartite graph. Can you answer his query?

Problem Description

For an undirected rooted tree, define f(i,k)f(i,k) as the number of ways, within the subtree rooted at ii, to add kk edges inside it so that this subtree becomes a bipartite graph. Note that when adding edges, it is allowed to add an edge parallel to an original tree edge, but any two newly added edges must not be identical.

Given an undirected rooted tree with nn nodes, where the root is node 11. For each i[1,n]i \in [1,n], compute f(i,k)modpif(i,k) \bmod p_i.

Input Format

The input has n+1n+1 lines.

Line 11 contains two integers nn and kk, representing the number of nodes in the tree and the parameter kk.

Lines 22 to nn each contain two positive integers, representing an edge between nodes uiu_i and viv_i.

Line n+1n+1 contains nn integers, representing the modulus pip_i for each query.

Output Format

Output one line containing nn integers, each being the answer for the corresponding query.

6 1
1 3
1 5
1 6
2 5
3 4
998244353 998244353 998244353 998244353 998244353 998244353
9 0 1 0 1 0

Hint

[Sample 1 Explanation]

In this tree, adding edges $(u,v)\in\{(1,3),(1,5),(1,6),(2,3),(2,5),(2,6),(3,4),(4,5),(4,6)\}$ can make the tree bipartite.


[Constraints]

This problem uses bundled testdata.

For 100%100\% of the testdata: 1n5×1051 \le n \le 5 \times 10^5, 1k201 \le k \le 20, 1ui,vin1 \le u_i, v_i \le n, 2pi2×1092 \le p_i \le 2 \times 10^9, and pip_i is a prime. There are at most 9999 distinct values of pip_i. It is guaranteed that pi>kp_i > k.

Subtask\text{Subtask} nn\le Special\text{Special} Score\text{Score}
11 5×1055\times10^5 A\text{A} 1010
22 10310^3 No\text{No} 1515
33 5×1055\times10^5 B\text{B}
44 No\text{No} 6060
  • Special A\text{Special A}: it is guaranteed that k=1k = 1.
  • Special B\text{Special B}: it is guaranteed that vi=ui+1v_i = u_i + 1.

[Hint]

The amount of I/O in this problem is large, so please use a faster I/O method.

Translated by ChatGPT 5