시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
6 초 | 256 MB | 49 | 15 | 11 | 27.500% |
Professor Zhang has a rooted tree with vertices conveniently labeled by $1, 2, \ldots, n$. The $i$-th vertex has an integer weight $w_i$.
For each $s \in \{1, 2, \ldots, n\}$, Professor Zhang wants to find a sequence of vertices $v_1, v_2, \ldots, v_m$ such that:
There are multiple test cases. The first line of input contains an integer $T$ indicating the number of test cases. For each test case:
The first line contains an integer $n$ and a string $\mathrm{op}$ ($2 \le n \le 2^{16}$, $\mathrm{op} \in \{\mathtt{AND}, \mathtt{OR}, \mathtt{XOR}\}$): the number of vertices and the operation. The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i < 2^{16}$). The third line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$) where $p_i$ is the parent of vertex $i$.
There are about $300$ test cases, and the sum of $n$ in all the test cases is no more than $10^6$.
For each test case, output the integer $S = (\sum\limits_{i = 1}^{n}{i \cdot f(i)})$ modulo $10^9 + 7$.
3 5 AND 5 4 3 2 1 1 2 2 4 5 XOR 5 4 3 2 1 1 2 2 4 5 OR 5 4 3 2 1 1 2 2 4
91 139 195