F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Alexandra and Two Trees

Time 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
3 1 2 1 2 3 3 1 2 1 2 3 3 1 2 1 2 1 2 1 3 1 2 2 3 1 1 1 1 1 1 1 1 1
 

Sample Output
2 2 1 1
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-25 23:06:11, Gzip enabled