Time limit:20 Sec Memory limit:256 MB
submit:1978 solved:550 Description
God Ben an apple tree at the door of his house. The apple tree, as a tree, is of course a tree-like structure, with each branch connecting two apples, each of which can be connected to a tree root by a path of branches, and there is only one path. Since the apple tree was Ben by God, the apples were mutated and turned into a variety of colors. We use a positive integer from one to N to represent a color. There are a total of n apples on the tree. Each apple is numbered with a positive integer number between 1 and N. We use 0 to represent the roots. There will only be an Apple direct root.
There are many people Wills Ben Home worship God Ben. But God Ben not be able to worship casually. People who come to worship God Ben need to answer a question correctly in order to enter the house and worship God Ben. The question is, from the tree of Apples numbered U, from the branches to the Apples numbered V, the path of the Apple has a total number of different colors (including Apple u and Apple v color)? But God Ben noticed that some people who come to worship have color blindness. Specifically, a person may think that color A is a color B, then when they count the apple color, if both the color A of the apple, and the appearance of the color B apple, the person will only be counted into the color B, but not the color a in.
God Ben is a good person, he will not be a great imposition, he would accept due to color blindness caused by the wrong answer (of course, the answer in the color-blind environment also must be correct). But then God Ben will change his original color of the program. Although this is a piece of cake for God Ben, he wants to test you. Can you finish the task for God's Ben?
Input inputs the first behavior two integers n and M, respectively, representing the number of apples on the tree and the number of worshippers. The next line contains the n number, and the number I represents is the color of the Apple coli. Next there are n rows, each containing two numbers x and Y, representing a branch that is connected to Apple X and Y (or root and an apple). Next there are m lines, each line containing four integers u, V, A and B, which means that the person wants to count the number of Apple U to Apple V, and this person thinks color A is color B. If a=b=0, it means that the person does not suffer from color blindness.
Output
The output is a total of M lines, each line contains only one integer, representing the number of colors the person should count.
Sample INPUT5 3
1 1 3) 3 2
0 1
1 2
1 3
2 4
3 5
1 4 0 0
1 4 1 3
1 4 1 2
Sample Output2
1
2
HINT
0<=x,y,a,b<=n
n<=50000
1<=u,v,coli<=n
m<=100000
This question exists copyright, therefore no longer supports the submission, retains here only for everybody to refer to the question surface! Hope forgive me! Source
Tree tree in the trees on the MO team
It's amazing, isn't it?
Http://hzwer.com/5259.html Daily visits to Huang
Look at it and pretend you will.
How boring it is not to submit.
Bzoj3757 Apple Tree