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