#P7443. 「EZEC-7」加边

    ID: 8274 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树形数据结构博弈论O2优化洛谷月赛

「EZEC-7」加边

Background

How do you do brute force? Brute force is, add edges! Add edges! Add edges! Then query with DSU!

Alice does not like DSU (Disjoint Set Union), but she likes adding edges.

Problem Description

Given a tree with nn nodes, the nodes are numbered starting from 11. Node 11 is the root. Each edge is directed from the parent to the child. Each node has a weight aia_i. Alice and Bob are playing a game: they place a token on the root node. Alice and Bob take turns moving the token along directed edges. Whoever cannot move loses.

It is known whether Alice moves first or second. Before the game starts, Alice may add one directed edge uvu\to v (1u,vn1\le u,v\le n), and then they play the game on the resulting graph. She wants to have a winning strategy. She may also choose not to add an edge. If the winner cannot be determined, it does not count as a win.

Given positive integers A,BA,B, the cost for Alice to add an edge uvu\to v is A×au+B×avA\times a_u+B\times a_v. The cost of choosing not to add an edge is 00.

Alice wants to minimize her cost. If no matter how she adds an edge she cannot meet the requirement, output 1-1.

Alice will make TT queries. You need to output the answer for each query.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of queries.

For each query, the first line contains four positive integers n,t,A,Bn,t,A,B, where nn is the number of nodes in the tree, and tt indicates the move order: t=0t=0 means Alice moves first, and t=1t=1 means Alice moves second.

The next line contains n1n-1 positive integers. The ii-th number is the parent index fi+1f_{i+1} of node i+1i+1.

The next line contains nn positive integers. The ii-th number is the value of aia_i.

Output Format

For each query, output one integer per line, the minimum cost for Alice. If no matter how she adds an edge she cannot meet the requirement, output 1-1.

4
3 1 2 7
1 1
4 3 2
3 1 2 7
1 2
4 3 2
3 0 2 7
1 2
4 3 2
9 1 523 182
1 1 2 2 2 3 3 1
3 23 18 293 162 483 574 384 109
-1
0
22
86491

Hint

[Sample Explanation]

In the 11-st query, Alice moves second. No matter how she adds an edge, she cannot have a winning strategy, so output 1-1.
In the 22-nd query, Alice moves second. She already has a winning strategy without adding an edge, so the cost is 00.
In the 33-rd query, Alice moves first. She can only add an edge 131\to 3 to guarantee a win. The cost is 2×4+7×2=222\times 4+7\times 2=22.
In the 44-th query, Alice moves second. She can add an edge 959\to 5 to guarantee a win. The cost is 523×109+182×162=86491523\times 109+182\times 162=86491. She has other ways to guarantee a win, but 8649186491 is the minimum cost.


[Constraints]

This problem uses bundled testdata.

  • Subtask 1 (10 points): n10n\le 10, T=1T=1.
  • Subtask 2 (15 points): n200\sum n\le 200.
  • Subtask 3 (15 points): n2000\sum n\le 2000.
  • Subtask 4 (10 points): fi=i1f_i=i-1.
  • Subtask 5 (10 points): fi=1f_i=1.
  • Subtask 6 (20 points): n5×105\sum n\le 5\times 10^5.
  • Subtask 7 (20 points): no special restrictions.

For 100%100\% of the data, 1T2×1031\le T\le 2\times10^3, 2n2×1052\le n\le 2\times 10^5, n5×106\sum n\le 5\times 10^6, 1ai,A,B1091\le a_i,A,B\le 10^9, fi<if_i<i, t{0,1}t\in\{0,1\}.


[Hint]

Please use a fast input method.

Translated by ChatGPT 5