您的位置:首页 > 其它

[dfs]多校联合第三场 K Work

2015-07-29 09:47 330 查看


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

签到题,直接用简单的dfs即可

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int n,k;
vector<int> g[111]; //== g[111][k]
int used[111];
int ans;

int dfs(int x)
{
if (used[x] >=0) return used[x];
int sum = 0;
for(int i = 0;i<g[x].size();i++)
{
sum += dfs(g[x][i]);
}
if (sum == k)
ans++;
used[x] = sum + 1;
return used[x];
}

int main()
{
while(scanf("%d%d",&n,&k) != EOF)
{
for(int i=0; i<=n; i++) g[i].clear();

for(int i=1; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
}

ans=0;
memset(used,-1,sizeof used);
for(int i=1; i<=n; i++)
{
if(used[i]==-1)
dfs(i);
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: