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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <math.h>
#define eps 1e-8
#include <algorithm>
using namespace std;
int son[200];
int fa[200];
int main()
{
int n, k;
int i, j;
int u, v;
while(scanf("%d %d", &n, &k)!=EOF){
memset(son, 0, sizeof(son));//出事哈每个人都有0个儿子
for(i=1; i<=n; i++){
fa[i]=i; //每个人的父亲是它自己
}
for(i=1; i<n; i++){
scanf("%d %d", &u, &v);
fa[v]=u; //将v的父亲确定为u
son[u]++; //u的儿子数量+1
while( fa[u]!=u ){//如果u的父亲不是他自己 说明u不是根节点
u=fa[u]; //往上面回溯 直到根节点跳出
son[u]++; //经过的这些节点的儿子数全部+1
}
}
int cnt=0;
for(i=1; i<=n; i++)
if(son[i]==k)
cnt++;
printf("%d\n", cnt ); //遍历一下 输出
}
return 0;
}
原文:http://www.cnblogs.com/yspworld/p/4684950.html