HDU Multi-School League 5326 work
Title: http://acm.hdu.edu.cn/showproblem.php?pid=5326
This should be the simplest topic of the two-school game of the week.
Problem-solving idea: It is back to root. Using an array of root[i] = J means that the ancestor of I is J, and this is handled for each input relationship. The result of traversing each number until Root[i] is then 0 (because the root does not have a superior, so the root is 0), and in the process of going back to the root, an array of cnt[i] is used to indicate the number of times I have passed this point. The end is to traverse Cnt[i], add cnt[i] = k results can be AC
#include <bits/stdc++.h>using namespace Std;const int MAX = 100+2;int Root[max] = {};int Cnt[max] = {};//back to root int getr oot (int n, int k) {for (int i=1; i<=n; ++i) { int t = i; while (Root[t]) { cnt[root[t]]++; Each pass the superior, then the superior accumulates, somewhat like the superior reports the same 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%d", &u, &v); ROOT[V] = u; } Back to the root printf ("%d\n", Getroot (n, k)); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Http://blog.csdn.net/core__code
HDU Multi-school league 5326 work