#P6038. 「ACOI2020」惊吓路径

    ID: 4886 远端评测题 1000ms 128~256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2020线段树倍增二分O2优化st表,稀疏表

「ACOI2020」惊吓路径

Description

Koro-sensei told them that the cave can be approximately seen as an out-tree with nn nodes. Due to the terrain, the edge from one node to another has a direction, and the directions of all edges are consistent. The root of this tree is the node with in-degree 00. Each node has a scare value; the scare value of node ii is aia_i.

Koro-sensei told them that there are many scare paths in this cave. If the path formed by two nodes u,vu, v is a scare path, then it satisfies:

  • vv must be in the subtree of uu.
  • The bitwise OR of the scare values of all nodes on the path from uu to vv is k\geq k.

Walking through a scare path will earn a “surprise gift” from Koro-sensei. Koro-sensei has prepared the gifts in advance, but Karma already knows Koro-sensei has some improper intentions, not to mention the number of gifts might not be enough. Koro-sensei promised that there would be as many surprise gifts as there are scare paths. Karma has learned, through some mysterious means, how many surprise gifts Koro-sensei prepared. Now he wants to know how many scare paths there are, i.e., the minimum number of gifts Koro-sensei must prepare. If it is not enough, he will expose Koro-sensei’s intentions. Of course, Karma wants to take advantage of this and mess with Koro-sensei. So he cheated got Koro-sensei’s map in advance, and wants to ask: how many scare paths are there in this graph?

Input Format

The first line contains two integers n,kn, k.

The second line contains nn integers, representing the scare value of each node.

The next n1n - 1 lines each contain two integers u,vu, v, indicating that there is a directed edge between nodes uu and vv, and node uu can reach node vv. Node vv cannot reach node uu.

Output Format

Output one integer in one line, representing the number of scare paths in this cave.

5 10
5 9 6 4 2
3 1
3 4
1 2
1 5

2
7 5
6 7 4 5 7 8 9
1 2
2 3
2 4
1 5
5 6
5 7

16
8 5
4 3 2 5 6 7 6 2
1 2
1 5
2 3
2 4
5 6
5 7
6 8

16

Hint

Sample Explanation #1

Only two paths satisfy the conditions:

  1. 3123 \to 1 \to 2. The bitwise OR of the scare values of all nodes on this path is 6or5or9=156\operatorname{or}5\operatorname{or}9=15.
  2. 121 \to 2. The bitwise OR of the scare values of all nodes on this path is 5or9=135\operatorname{or}9=13.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (10 points): n5×103n \leq 5 \times 10^3, k105k \leq 10^5.
  • Subtask 2 (30 points): For any edge, v=u+1v = u + 1, n106n \leq 10^6, k,ai109k, a_i \leq 10^9.
  • Subtask 3 (20 points): n105n \leq 10^5, k,ai109k, a_i \leq 10^9.
  • Subtask 4 (40 points): n5×105n \leq 5 \times 10^5, k,ai109k, a_i \leq 10^9.

For 100%100\% of the testdata: 1n1061 \leq n \leq 10^6, 1k,ai1091 \leq k, a_i \leq 10^9.


Note

In the fourth subtask, the memory limit is 256MB; in the other subtasks, the memory limit is 128MB.

Translated by ChatGPT 5