CF2194F1.Again Trees... (Easy Version)

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, k≤4k \leq 4. You can hack only if you solved all versions of this problem.

Given a tree consisting of nn vertices. Each vertex has a non-negative integer ava_v written on it. You are also given kk distinct non-negative integers b1,…,bkb_1, \ldots, b_k.

We will call a set of edges beautiful if, after removing these edges from the tree, the tree splits into connected components, where in each component the bitwise XOR of all the numbers ava_v in that component belongs to the set bb.

You need to count the number of beautiful sets of edges in the tree modulo 109+710^9 + 7.

这是该问题的简单版本。两个版本的区别在于,在此版本中,k≤4k \leq 4。仅当您解决了该问题的所有版本时,才可进行 Hack。

给定一棵包含 nn 个顶点的树。每个顶点上写有一个非负整数 ava_v。同时给定 kk 个互不相同的非负整数 b1,…,bkb_1, \ldots, b_k。

我们称一组边为“优美的”,如果从树中移除这些边后,树被分割为若干连通分量,且每个连通分量内所有顶点上的数 ava_v 的按位异或(XOR)结果均属于集合 bb。

您需要计算树中优美边集的数量,结果对 109+710^9 + 7 取模。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each dataset contains two integers nn and kk (2≤n≤1052 \leq n \leq 10^5, 1≤k≤41 \leq k \leq 4) — the number of vertices in the tree and the size of the set bb.

The next n−1n - 1 lines describe the edges of the tree. The ii-th line contains two integers viv_i and uiu_i (1≤vi,ui≤n1 \leq v_i, u_i \leq n) — the numbers of the vertices connected by the ii-th edge.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<2300 \leq a_i \lt 2^{30}) — the values written on the vertices.

The following line contains kk distinct integers b1,b2,…,bkb_1, b_2, \ldots, b_k (0≤bi<2300 \leq b_i \lt 2^{30}) — the elements of the set bb.

It is guaranteed that the sum of nn across all datasets does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是各测试用例的描述。

每个数据集的第一行包含两个整数 nn 和 kk(2≤n≤1052 \leq n \leq 10^5,1≤k≤41 \leq k \leq 4)—— 分别表示树中顶点的数量以及集合 bb 的大小。

接下来的 n−1n - 1 行描述树的边。第 ii 行包含两个整数 viv_i 和 uiu_i(1≤vi,ui≤n1 \leq v_i, u_i \leq n)—— 表示由第 ii 条边连接的两个顶点的编号。

下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<2300 \leq a_i \lt 2^{30})—— 表示写在各个顶点上的数值。

再下一行包含 kk 个互不相同的整数 b1,b2,…,bkb_1, b_2, \ldots, b_k(0≤bi<2300 \leq b_i \lt 2^{30})—— 表示集合 bb 的元素。

保证所有数据集中 nn 的总和不超过 10510^5。

输出格式

For each test dataset, output a single integer — the answer to the problem.

对于每个测试数据集,输出一个整数——即该问题的答案。

输入输出样例

  • 输入#1

    4
    5 1
    1 2
    1 3
    3 4
    2 5
    0 2 2 3 3
    1
    5 1
    4 5
    1 2
    3 1
    3 5
    0 0 2 0 2
    0
    5 2
    1 2
    1 3
    3 4
    2 5
    0 2 2 3 3
    1 3
    4 1
    1 2
    2 3
    3 4
    1 1 1 1
    1

    输出#1

    2
    8
    4
    3

说明/提示

Illustration for the first test dataset. In it, you can remove the edge between vertices 11 and 22. Then the bitwise XOR in the component consisting of vertices 22 and 55 is a2⊕a5=2⊕3=1a_2 \oplus a_5 = 2 \oplus 3 = 1. The bitwise XOR in the component with vertices 11, 33, and 44 is a1⊕a3⊕a4=0⊕2⊕3=1a_1 \oplus a_3 \oplus a_4 = 0 \oplus 2 \oplus 3 = 1. The second beautiful set is the one consisting of the edge connecting vertices 11 and 33.

In the third test dataset, the beautiful sets will be (an edge is denoted by the pair of vertices it connects) [(1,2)],[(1,3)],[(2,5)],[(3,4)][(1, 2)], [(1, 3)], [(2, 5)], [(3, 4)]. Note that the first and third samples differ only in the set bb.

In the fourth test dataset, the beautiful sets will be [(1,2)],[(3,4)],[(1,2),(2,3),(3,4)][(1, 2)], [(3, 4)], [(1, 2), (2, 3), (3, 4)].

第一个测试数据集的示意图。在此图中,你可以删除顶点 11 和 22 之间的边。此时,由顶点 22 和 55 构成的连通分量的按位异或值为 a2⊕a5=2⊕3=1a_2 \oplus a_5 = 2 \oplus 3 = 1;由顶点 11、33 和 44 构成的连通分量的按位异或值为 a1⊕a3⊕a4=0⊕2⊕3=1a_1 \oplus a_3 \oplus a_4 = 0 \oplus 2 \oplus 3 = 1。第二个优美的集合是由连接顶点 11 和 33 的边构成的集合。

在第三个测试数据集中,优美的集合为(边用其所连接的两个顶点对表示):[(1,2)],[(1,3)],[(2,5)],[(3,4)][(1, 2)], [(1, 3)], [(2, 5)], [(3, 4)]。注意,第一个和第三个样例仅在集合 bb 上不同。

在第四个测试数据集中,优美的集合为:[(1,2)],[(3,4)],[(1,2),(2,3),(3,4)][(1, 2)], [(3, 4)], [(1, 2), (2, 3), (3, 4)]。

输入解题思路,AI测评打分。不知道怎么写?

首页