Mojtabas Trees and Arpas Queries <March HourStorm>

3.7

3 votes
Advanced Data Structures, Data Structures, Fenwick tree, Medium
Problem

Mojtaba has two trees, each of them has vertices. Arpa has queries, each in type . Let be the set of vertices in the path from to in the first tree and be the set of vertices in the path from to in the second tree. Mojtaba has to calculate the size of for each query. Help him!

Input Format

The first line of input will contain two integers, ().

The next lines will contain edges of the first tree.

The next lines will contain edges of the second tree.

The next lines contain queries.

Output Format

For each query, let be the set of vertices in the path from to in the first tree and be the set of vertices in the path from to in the second tree, print the size of for each query.

Sample Input
5 5
3 4
5 1
4 1
3 2
1 2
4 1
5 1
2 3
4 1 5 5
3 4 2 4
4 3 1 3
1 5 5 3
5 2 1 3
Sample Output
0
1
1
2
3
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

 

First Tree.second tree

The tree on the left is the first tree and the one in right is the second tree.

In the first query, , so the size of is 0.

In the third query, , so the answer is 3.

Editor Image

?