luogu#P16338. 「ALFR Round 10」纸牌

    ID: 16136 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>贪心洛谷原创O2优化树的遍历洛谷月赛

「ALFR Round 10」纸牌

Problem Description

There are nn players playing a certain card game, numbered from 11 to nn. The players form a tree structure. Player ii initially has aia_i cards and bib_i energy points. You are player dd.

At the beginning of the game, everyone agrees on an integer uu. Then player uu chooses a permutation p1,,pnp_1,\ldots,p_n, such that pp is a DFS order of the original tree when rooted at uu.

Initially, let k=0k=0. Then for each i=1,2,,ni=1,2,\ldots,n in order, player pip_i must choose exactly one of the following actions:

  1. Discard some cards from their hand. The number of discarded cards must be at most apia_{p_i}, and must be at least the number discarded by the previous player who chose to discard, plus 11.
    Formally, choose an integer xx such that k<xapik < x \le a_{p_i}, and set kxk \gets x.
    In particular, if kapi\boldsymbol{k \ge a_{p_i}}, then this action cannot be chosen.

  2. Lose 22 energy points.
    Formally, set bpibpi2b_{p_i} \gets b_{p_i} - 2.

Once a player's remaining energy becomes 0\le 0, they are eliminated.

You win if and only if after the entire process ends, you are not eliminated, regardless of whether other players are eliminated.

You want to know how many integers uu with 1un1 \le u \le n satisfy the following:

There exists some strategy of the other players (excluding you), such that no matter what you do, you will inevitably lose.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, denoting the number of test cases.

For each test case:

The first line contains two positive integers n,dn,d, representing the number of nodes in the tree and your player index.

The next nn lines each contain two integers ai,bia_i,b_i.

The next n1n-1 lines each contain two integers u,vu,v, indicating that there is an edge between nodes uu and vv in the tree.

Output Format

For each test case, output one line containing one integer: the required answer.

1
5 1
1 2
2 2
1 1
3 2
4 2
1 2
1 3
3 4
3 5
4
1
5 3
2 3
3 3
1 6
6 2
8 6
1 2
1 3
3 4
4 5
0

Hint

Explanation of Sample 1

When u{2,3,4,5}u \in \{2,3,4,5\}, there exists an optimal strategy for the opponents that guarantees your failure.

For example, when u=3u=3, one possible DFS order rooted at 33 is 3,4,1,2,53,4,1,2,5. Following this order:

  • Player 33 chooses to lose 22 energy points.
  • Player 44 chooses to discard a4=2a_4=2 cards.

Then you (player 11) must discard at least 2+1=32+1=3 cards, but a1=1a_1=1, so you cannot choose the discard action. You are forced to lose 22 energy points and are eliminated immediately. Therefore, you lose.

Explanation of Sample 2

Since b3>2b_3>2, no matter what strategy the opponents use, you can always choose to lose 22 energy points without being eliminated. Therefore, you are guaranteed to win.

Constraints

This problem uses bundled subtasks.

Subtask ID nn \le Special Property Score
11 1010 A\text{A} 1010
22 100100 None 1515
33 10410^4 B\text{B} 2020
44 C\text{C} 2525
55 10510^5 None 3030

Special property A\text{A}: it is guaranteed that T5T \le 5.

Special property B\text{B}: the tree is generated as follows: after fixing nn, for each 2in2 \le i \le n, an integer xx is chosen uniformly at random from [1,i)[1,i), and an edge is added between xx and ii.

Special property C\text{C}: it is guaranteed that ai10a_i \le 10.

For all test cases, it is guaranteed that T100T \le 100, 2n1052 \le n \le 10^5, n106\sum n \le 10^6, 1ai,bi1091 \le a_i,b_i \le 10^9, 1dn1 \le d \le n.