hdu 5326 Work(杭电多校赛第三场)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326

Work

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 583    Accepted Submission(s): 392


Problem Description


It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company.
As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all the relationship forms a tree. If A’s title is higher than B(A is the direct or indirect leader of B), we call it A manages B.
Now, give you the relation of a company, can you calculate how many people manage k people.
 
Input
There are multiple test cases.
Each test case begins with two integers n and k, n indicates the number of stuff of the company.
Each of the following n-1 lines has two integers A and B, means A is the direct leader of B.

1 <= n <= 100 , 0 <= k < n
1 <= A, B <= n
 
Output
For each test case, output the answer as described above.
 
Sample Input
7 2
1 2
1 3
2 4
2 5
3 6
3 7
 
Sample Output
2
 
Source
 
题目大意:输入的第一行:第一个数字n表示的是有几个人,第二个数字m表示查找管理两个人的。
       接下去就有n-1行表示前面的管理后面的,最后求的就是有几个人是管理m个人的。
 
详见代码。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 int n,m,Map[110][110];
 8 
 9 int dfs(int x)
10 {
11     int sum=0;
12     for (int i=1;i<=n;i++)
13     {
14         if (Map[x][i]==1)
15         {
16             sum++;
17             sum+=dfs(i);
18         }
19     }
20     return sum;
21 }
22 
23 int main()
24 {
25     int a,b,ans;
26     while (~scanf("%d%d",&n,&m))
27     {
28         ans=0;
29         memset(Map,0,sizeof(Map));
30         //memset(ok,0,sizeof(ok));
31         for (int i=1; i<=n-1; i++)
32         {
33             scanf("%d%d",&a,&b);
34             Map[a][b]=1;
35         }
36         for (int i=1;i<=n;i++)
37         {
38             if (dfs(i)==m)
39                 ans++;
40         }
41         printf ("%d
",ans);
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/qq-star/p/4686536.html