Nobita In Trouble

4

35 votes
Medium-Hard
Problem

Since nobita is stuck into problem which is given by his friends giyan and sunio. To prove his intelligence, he wants to solve this question and he wants your help to solve this task since doreamon is not available . Task is as follow:

You are given a tree with n nodes (numbered from 1 to n) and of course n-1 edges. In this graph some of the nodes are lucky and rest are unlucky. You will be given certain queries in which you will be given number of two nodes u and v, you have to answer how many lucky nodes lies in the shortest path between this two nodes.

Input format:

First line of the input will contain number of nodes in the graph. Next n-1 lines contains two integers a and b which denotes an edge between node a and b. Next line will contain array A of n integers(0<=A[i]<=1,1<=i<=n). Here A[i]=0 indicate that node i is unlucky node and A[i]=1 indicate that node i is lucky node.next line will contain an integer q, number of queries and next q lines will contain two integers u and v.

Output Format:

For every query output one integer which denotes number of lucky nodes in the shortest path between u and v.

Constraints:

2 <= n <= 100000

0<=A[i]<=1

1<= Q <= 200000

Author: Mohib Manva

Tester: Akshay Miterani

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For first Query, shortest path from 1 to 3 is 1 -> 2 -> 3, since 1 and 3 both are lucky so ans is 2.

For second Query, shortest path from 1 to 5 is 1 -> 2 -> 4 -> 5.

For third Query, Shortest path from 4 to 5 is 4->5.

Editor Image

?