首页 > 代码库 > HDU5156(离线tarjan+打标记)

HDU5156(离线tarjan+打标记)

Harry and Christmas tree

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 213    Accepted Submission(s): 53


Problem Description
In Christmas Day, Harry got a Christmas tree. The tree has n nodes. And you can assume the tree rooted at node 1. Harry found that there are some gifts with different colors on some nodes. Then Harry began to count the gifts on the tree. Every time Harrty counted a gift, he written down a pair (a, b) on the paper. Where a indecates which node this gift located, and b indecates the color of this gift. After Harry finished all his counting, for each node x, he wanted to know the number of different kinds of colors that all gifts located on the subtree rooted at node x had.
 

Input
There are several test cases,you should process to the end of file.
For each test case, there are two integers n m on the first line, n indecates the number of the nodes, m indecates the number of countings. 1n50000,1m500000
The next following n-1 lines. Each line contains two integers a b, it means there is an edge between a and b. 1a,bn,ab.
The next following m lines. Each line contains two integers a b, indecates the countings. 1an,1b100000.
 

Output
For each test case output one line contains n numbers, where a[i] indecates the number of different kinds of colors that all gifts located on the subtree rooted at node i had.
 

Sample Input
5 3 3 1 4 1 2 3 5 1 1 9 4 6 4 6
 

Sample Output
2 0 0 1 0
Hint
Make your program running as fast as possible.
 

Source
BestCoder Round #25
 

Recommend
heyang   |   We have carefully selected several similar problems for you:  5157 5154 5153 5152 5151 

题意:RT

思路:大概就是打标记,每个点u对从u到树根1上的路径上点贡献为1,在u上+1就好了,这样如果u和v的颜色一样,

            那么就会对u和v的最近公共祖先f到树根1上的路径上的点算重复一次,所以只需在f上-1就好了

            唯一的坑点是,求最近公共祖先要用离线Tarjan来求,我的这种写法还要加输入挂,否则照样T = =

<script src="https://code.csdn.net/snippets/572828.js" type="text/javascript"></script>

HDU5156(离线tarjan+打标记)