2643sembei? ancestors!

2643   sembei? ancestors!

题目描述

There have a tree ,which has n nodes, each node has a value w , the number of nodes whose value is w won't exceed 200.Then there are q queries, each has two integer u,v, we define a pair (x,y) is a good pair if w[x] = u, w[y] = v and x is on the path from root to y.

note: 1 is the root.

输入格式:

The first line contains one integer n .(1<=n<=10000)

Next line contains n integers w1,w2...wn, which means the values from 1 to n. (1<=wi<=10000)

Next n-1 line following, each line contain two integer a,b,(1<=a,b<=n), there is an edge between a and b.

Next is an integer q, (0<=q<=10000)

  Then q lines following, each line contain two integer u,v.

输出格式:

for each query, print the number of good pairs.

输入样例 复制
4
1 2 3 4
1 2
1 3
2 4
3
2 2
1 3
1 4
输出样例 复制
0
1
1

说明

1
1
通过提交
时空限制1000ms/128mb
题目来源数据结构实验与竞赛训练
评测方式在线评测
题目类型
难        度