Work
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1706 Accepted Submission(s): 1024
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
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
题解:用拓扑,新学的,看起来很好用的样子。把图的箭头指向反向理解。见图:1,入度为2。 2,入度为2。 3入度为2。 4,5,6,7为0
;
依次找到入度为0的点,记录人数,删去,父亲入度减1。知道找完为止。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
int main()
{
int n,k,num;
int in[110]; //记录入度(个数)
int sum[110]; //记录每个节点可以管理的人数(即其下分支的所有数的和)
bool link[110][110]; //记录 a,b 是否连接,连接是true
while(~scanf("%d %d",&n,&k))
{
num=0;
CLR(in,0);
CLR(sum,0);
CLR(link,false);
int a,b;
for(int i=1;i<n;i++)
{
scanf("%d %d",&a,&b);
in[a]++;
link[a][b]=true;
}
while(1)
{
for(int i=1;i<=n;i++)
{
if(in[i]==0) //入度为0,就在最底层
{
for(int j=1;j<=n;j++) //i是底层的一个数
{
if(link[j][i]==true) //注意这里是link[j][i]不要想当然的写!!!
{
in[j]--; //入度减 1,去掉那个孩子
sum[j]+=sum[i]+1; //可以管理的人数是他一个孩子可以管理的人数加上他的孩子本身
break;
}
}
in[i]=-1; //入度变为-1,不再找他
num++; //个数++,知道个数为n(全部找完),退出循环
}
}
if(num==n)
break;
}
int ant=0;
for(int i=1;i<=n;i++)
{
if(sum[i]==k)
ant++;
}
printf("%d\n",ant);
}
return 0;
}