#P8994. [北大集训 2021] 经典游戏

    ID: 9531 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树状数组2021O2优化分治树链剖分字典树 TrieSG 函数

[北大集训 2021] 经典游戏

Background

CTT2021 D4T2.

Problem Description

One day, C and K felt bored, so they decided to play a classic mini-game.

On a rooted tree with nn nodes, node ii has aia_i tokens. Players take turns to make moves. In each move, a player may take one token from any node uu and place it onto any node vU(u)v \in U(u), where U(u)=subtree{u}{u}U(u)=subtree\{u\}\setminus\{u\}, meaning the set of nodes in the subtree of uu (excluding uu itself). The player who cannot make a move loses.

However, since C and K are students of P** and T**, this kind of game where you can see the winning strategy at a glance is too boring. So they thought it would be more fun if each of them had a special ability.

Before the game starts, C may choose to change the current root RR of the tree to any node RR^{\prime} adjacent to RR. Two nodes are adjacent if and only if there is an edge directly connecting them.

Before the game starts, K must choose one node in the tree and add one token on it.

C and K will play mm games. The process of each game is as follows:

  1. Before the game starts, C and K agree to first place one token on the node with label xx, and then set the root of the tree to yy.
  2. Then C may choose whether to use the special ability. After C finishes the decision, K may choose whether to use the special ability.
  3. After the decisions about special abilities are finished, they play one game on this tree with C moving first and K moving second. After the game ends, the token configuration on the tree is restored to the state right after step 1 ends.

C thinks this game can be made into an easy problem, so he decides to test you: in step 2 of each game, how many decision choices does C have such that no matter how K uses the special ability, C has a winning strategy at the start of the game? Two decision choices are considered different if and only if the new root RR^{\prime} chosen by the two decisions is different, or exactly one of them does not use the special ability.

Input Format

The first line contains one integer, representing the score of the subtask for this test point. You may use this information to determine the special properties satisfied by this test point. In particular, in the samples provided, this line is replaced by 00.

The second line contains two positive integers n,mn, m separated by spaces, denoting the number of nodes in the tree and the number of games. The nodes are numbered from 11 to nn.

The next n1n-1 lines each contain two positive integers ui,viu_i, v_i separated by spaces, satisfying 1ui,vin1 \le u_i,v_i \le n, indicating that there is an edge directly connecting nodes uiu_i and viv_i.

The next line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n separated by spaces, satisfying 0a1,a2,,an1090 \leq a_1,a_2,\ldots,a_n \leq 10^9.

The next mm lines each contain two positive integers x,yx, y separated by spaces, describing one game, satisfying 1x,yn1 \le x,y \le n.

Output Format

You need to output mm lines. The ii-th line should contain a non-negative integer xx indicating, in the ii-th game, how many decision plans C has for using the special ability such that C has a winning strategy in this game. Note that not using the special ability is also a possibly feasible decision plan.

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

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

1
1
0
1
1
1
0
0
2
1

Hint

Subtask Score 1n,m1\le n,m\le max{a1,a2,,an}\max\{a_1,a_2,\dots,a_n\}\le Special Property
1616 55 11 None
1515 300300
1414 50005000 10910^9
1313 100000100000 Guaranteed that the given tree is a chain.
1212 Guaranteed that the tree has a node with degree n1n-1.
1111 Guaranteed that the initially given root is the same across the mm games.
1010 500000500000 None
99 10000001000000

Translated by ChatGPT 5