#P11189. 「KDOI-10」水杯降温

    ID: 10663 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP贪心二分2024洛谷原创O2优化洛谷月赛

「KDOI-10」水杯降温

Description

Little S has a tree with nn vertices, rooted at vertex 11. On vertex ii (1in)(1\le i\le n), there is a water cup with an initial temperature of aia_i.

Little S, who picked up a water cup and drank water without knowing the temperature and was scalded many times, decided to change all the temperatures of the water in the cups to 00.

Now, Little S can do the following two types of operations an arbitrary number of times (possibly zero):

  • Use a heater at vertex ii. This will make the temperature of the water cups on all vertices in subtree ii increase by 11;
  • Or, blow a gust of wind from a leaf ii. This will make the temperature of the water cups on the route between 11 and ii decrease by 11.

Help Little S determine whether he can make the temperature of all the water cups become 00.

Input Format

Each test contains multiple test cases.

The first line of the input contains a single integer cc — the id of the test. c=0c=0 represents that this is a sample test.

The second line contains a single integer tt — the number of test cases.

For each test case:

  • The first line contains a single integer nn — the number of vertices.
  • The second line contains n1n-1 integers f2,f3,,fnf_2,f_3,\dots,f_n, in which fif_i is the parent vertex of vertex ii. It is guaranteed that fi<if_i<i.
  • The third line contains nn integers aia_i — the initial temperature of the water cup on vertex ii.

Output Format

For each test case:

  • If it is possible to make the temperature of all the water cups 00, print one line, a string Huoyu;
  • If it is impossible to do so, print one line, a string Shuiniao.
0
5
5
1 1 2 3
6 5 2 2 1
5
1 1 2 2
6 5 1 2 1
5
1 1 2 2
4 -1 5 -2 -2
5
1 1 2 2
6 -4 8 -3 -3
5
1 1 2 2
-1 -1 -1 -4 -1
Shuiniao
Huoyu
Shuiniao
Shuiniao
Huoyu

Hint

Sample 1 Explanation

Let AuA_u be a heating operation on vertex uu, BuB_u be a wind operation from vertex uu and (S)k(S)^k be repeating the operations in SS for kk times.

  • In the first, third and fourth test case, it can be proven that Little S can't make the temperature of all the water cups 00;
  • In the second test case, a possible operation sequence is B3(A4)3(B4)5B5B_3(A_4)^3(B_4)^5B_5;
  • For the fifth test case, a possible operation sequence is (A4)3A1(A_4)^3A_1.

Sample 2

See water/water2.in and water/water2.ans in the attachments.

This sample satisfies the constraints of test 33.

Sample 3

See water/water3.in and water/water3.ans in the attachments.

This sample satisfies the constraints of test 7,87,8.

Sample 4

See water/water4.in and water/water4.ans in the attachments.

This sample satisfies the constraints of test 1212.

Sample 5

See water/water5.in and water/water5.ans in the attachments.

This sample satisfies the constraints of test 13,1413,14.

Sample 6

See water/water6.in and water/water6.ans in the attachments.

This sample satisfies the constraints of test 151715\sim 17.

Sample 7

See water/water7.in and water/water7.ans in the attachments.

This sample satisfies the constraints of test 182118\sim 21.


Constraints

Let n\sum n be the sum of nn over all test cases in a single test.

For all the tests, it is guaranteed that:

  • 1t10001\leq t\leq 1\,000;
  • 2n1052\leq n\leq 10^5, n106\sum n\le 10^6;
  • For each 2in2\le i\le n, 1fi<i1\le f_i<i;
  • For each 1in1\le i\le n, 1012ai1012-10^{12}\leq a_i\leq10^{12}.
Test Id nn\leq n\sum n\le ai\lvert a_i\rvert\leq Special Properties
11 55 5050 55 -
22 200200
33 50005\,000
4,54,5 5050 500500 5050
66 10810^{8}
7,87,8 200200 20002\,000 200200
99 10810^{8}
10,1110,11 10001\,000 10410^4 10001\,000
1212 10810^{8}
13,1413,14 10510^5 3×1053\times 10^5 101210^{12} A
151715\sim 17 B
182118\sim 21 C
22,2322,23 3×1043\times 10^4 10510^5 10810^{8} -
24,2524,25 10510^5 10610^6 101210^{12}
  • Property A: For each 2in2\le i\le n, fi=i1f_i=i-1;
  • Property B: For each 1in1\le i\le n, ai(fj=iaj)+5a_i\le \left(\sum_{f_j=i}a_j\right)+5, where f1=0f_1=0;
  • Property C: The depth of the tree does not exceed 22, where the depth of the tree denotes the maximum number of edges in the simple path from a node to the root.

Hints

The input and output files are huge in this problem, please use fast I/O methods.