Beautiful pair of nodes

5

1 votes
Advanced Data Structures, Data Structures, Depth First Search, Fenwick Tree, Fenwick tree, Hard, Open, treap
Problem

You are given a rooted tree having n nodes, rooted at node 1. Every node i has two values associated with itself , and . In this tree you have to count total pairs of nodes which are beautiful. A pair of nodes i , j is beautiful if i is ancestor of node j and and .
Node i is called an ancestor of some node j, if node i is the parent of node j, or it is the ancestor of parent of node j. The root of a tree has no ancestors.
Input
First line contains a number n as input. Next lines contain two integers u , v denoting that there is an edge between the nodes u and v. Next line contains n space separated integers that denote the value for each node. Similarly next line contains n space separated integers that denote the value for each node.

Output
In the output you have to print total good pairs in the tree.

Constraints


Sample Input
7
1 2
1 3
2 4
2 5
3 6
3 7
4 9 1 2 2 6 9
3 4 4 5 9 3 8
Sample Output
3
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the given tree you can see that the following pair of nodes are good : , and

Editor Image

?