BOJ 292 MABODX

MABODX

 

Accept:17    Submit:88
Time Limit:4000MS    Memory Limit:65536KB

Description

There is a country which has N city. Because The king spend littlemoney on the road ,so there is one and only one path from any cityto another city,every city has a apple tree.The apple in one citywhich has only one apple tree is the same.

Now The king wants to ask Bobo some questions .The king wants toknow if he walk from A city to B city and pick apples from the appletree when he pass one city (including A city and B city) what is thesum value of apples.But Bobo can't the question so he turns to yourhelp.


Input

There are many cases.

The first line of every case is N andM.N(1<=N<=50000) means N city, M(1<=M<=50000) means Mquestion .

Then N-1 line represent the roadbetween the country. Then the N line has N number the ith number onthe N line means the value of the apple in the ith city(the value isless than 10000) .

Then M line .Each line has 2 numberA(1<=A<=N) and B(1<=B<=N) ,means king ask if he walk fromcity A to city B.


Output

The sum value of appple of everyquestion. Output one line one answer.


Sample Input

3 2

1 2

1 3

5 7 10

1 3

3 2


Sample Output

15

22


Source

mabodx

此题用LCA做,首先预处理dfs求出每一点到根节点的权值,再LCA即可


打比赛时我一直用的带权值的并查集做,一直WA,现在也没查出错,望路过的大牛能够指正


 

 

 

 

posted on 2012-02-23 21:56  c语言源码  阅读(150)  评论(0编辑  收藏  举报

导航