2015 HDU multi-school league 5326 Work, hdu5326

Source: Internet
Author: User

2015 HDU multi-school league 5326 Work, hdu5326

2015 HDU multi-school league 5326 Work


Question: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5326


This question should be the simplest one in this second multi-school competition.

Solution: Return root. Use an array root [I] = j to indicate that the upper-level of I is j. This process is used for the relationship between each input. Then traverse each number until the result of root [I] is 0 (because the root has no parent, so the root is 0). In the process of returning to the root, use an array cnt [I] to indicate the number of times the vertex passes through I. Finally, traverse cnt [I] and accumulate the result of cnt [I] = k.


# Include <bits/stdc ++. h> using namespace std; const int MAX = 100 + 2; int root [MAX] ={}; int cnt [MAX] = {}; // return the root int getRoot (int n, int k) {for (int I = 1; I <= n; ++ I) {int t = I; while (root [t]) {cnt [root [t] ++; // each time a superior passes through, the superior accumulates, A bit like the superior report t = root [t];} int ans = 0; for (int I = 1; I <= n; ++ I) {if (cnt [I] = k) ans ++;} return ans;} int main (void) {// freopen ("in.txt", "r ", stdin); int n, k; while (cin> n> k) {memset (root, 0, sizeof (root); memset (cnt, 0, sizeof (cnt); int u, v; for (int I = 1; I <n; ++ I) {scanf ("% d", & u, & v); root [v] = u;} // return root printf ("% d \ n", getRoot (n, k);} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/core__code

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.