HDU5156 (offline tarjan+ marking)

Source: Internet
Author: User

Harry and Christmas TreeTime limit:5000/2500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 213 Accepted Submission (s): 53


Problem DescriptionIn 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 is 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 Gifts located on the subtree rooted at node X had.
Inputthere is several test cases,you should process to the end of file.
For each test case, there is integers n m on the first line, n indecates the number of the nodes, M indecates the num ber of Countings. 1≤n≤50000,1≤m ≤500000
The next following n-1 lines. Each line contains the integers a B, it means there is an edge between A and B. 1≤a,b≤n,a ≠ b .
The next following m lines. Each line contains integers a B, indecates the countings. 1≤a≤n,1≤b ≤100000 .
Outputfor 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 33 14 12 35 11 94 64 6

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

Sourcebestcoder Round #25
Recommendheyang | We have carefully selected several similar problems for you:5157 5154 5153 5152 5151
Test instructions: RT
Idea: Probably hit the mark, each point u on the path from the U to the root 1 on the point contribution to 1, on the U +1 is good, so if you and v the same color,
Then you will repeat the point on the path of the nearest common ancestor F to root 1 on the U and V, so just 1 on the F.
The only pit point is that the recent public ancestor to use offline Tarjan to beg, my writing also add input hanging, otherwise t = =

HDU5156 (offline tarjan+ marking)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.