2015 Multi-University Training Contest 3 hdu 5326 Work

Work

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


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
 
解题:一顿乱搞
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 struct arc {
 5     int to,next;
 6     arc(int x = 0,int y = -1) {
 7         to = x;
 8         next = y;
 9     }
10 } e[maxn];
11 int head[maxn],tot;
12 void add(int u,int v) {
13     e[tot] = arc(v,head[u]);
14     head[u] = tot++;
15     e[tot] = arc(u,head[v]);
16     head[v] = tot++;
17 }
18 int ind[maxn],cnt[maxn],ret,n,k;
19 void dfs(int u,int fa) {
20     cnt[u] = 1;
21     for(int i = head[u]; ~i; i = e[i].next) {
22         if(e[i].to == fa) continue;
23         dfs(e[i].to,u);
24         cnt[u] += cnt[e[i].to];
25     }
26     if(k + 1 == cnt[u]) ++ret;
27 }
28 int main() {
29     int u,v;
30     while(~scanf("%d%d",&n,&k)) {
31         memset(head,-1,sizeof head);
32         memset(ind,0,sizeof ind);
33         ret = tot = 0;
34         for(int i = 1; i < n; ++i) {
35             scanf("%d%d",&u,&v);
36             add(u,v);
37             ++ind[v];
38         }
39         for(int i = 1; i <= n; ++i)
40             if(!ind[i]) {
41                 dfs(i,-1);
42                 break;
43             }
44         printf("%d\n",ret);
45     }
46     return 0;
47 }
View Code

 

posted @ 2015-07-28 21:10  狂徒归来  阅读(185)  评论(0编辑  收藏  举报