|
||||||||||
Alexandra and Two TreesTime Limit: 15000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 520 Accepted Submission(s): 133 Problem Description Alexandra and her little brother have two rooted trees. For each tree, node 1 is the root, and every other node has its father's index less than itself. Also, there are weights on the nodes. Two nodes in different trees can have the same weight, buf two nodes in the same tree can't. Now given the two trees and Q queries. Each query follows the format "u1 v1 u2 v2". Let S1 be the set of all node weights on the paths between u1 and v1 on the first tree, S2 be the set of all node weights on the paths between u2 and v2 on the second tree. You should output the number of elements in both S1 and S2. (e.g. the size of the intersection of S1 and S2) Input There are multiple test cases (no more than 50). For each case, the first three lines describe the first tree. The first line only contains an integer N, the number of the nodes. The second line contains N-1 integers, the i-th number is the father of node i+1. The third line contains N integers, the i-th number is the weight of node i. Next three lines have the same format, describing the second tree. Next line only contains an integer Q. Next Q lines, each line contains four integers u1,v1,u2,v2. $1 \leq N1, N2 \leq 100,000$. For each tree, $1 \leq P_i < i, \text{where }i > 1$ and $P_i$ is the father of i. For each node, $0 \leq \text{weight} \leq 1,000,000,000$. $1 \leq Q \leq 50,000$. $1 \leq u_1, v_1 \leq N_1, 1 \leq u_2, v_2 \leq N_2$. Number of cases with $max(N1,N2,Q) \geq 1,000$ is no more than 5. Huge data. Fast I/O may be needed. Output For each query, output the requested answer. Sample Input
Sample Output
Source | ||||||||||
|