Work
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1303 Accepted Submission(s): 791
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
题目大意:给你n-1个关系,表示a比b官大,就是说a管辖b。问,有多少个官管着k个员工。
第一次发现自己意淫出来点肥猪流解题算法好开森~~~~~~~~
题干明确给出全序关系,一方管另一方,是绝对不可能矛盾的东西,刷图论刷的多了,突然想起来这个题还能用floyd的递推关系思想来做~
思路很简单:如果j管i并且i管k,则能退出来j管着k。这是floyd的思想。
复杂度:N^3.一共100个点,最坏的打算100*100*100 ,1000MS,足够了,是不会超时的。
这里直接上AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
int a[105][105];
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]=0x1f1f1f1f;
}
}
for(int i=0;i<n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(a[x][y]==0x1f1f1f1f)
{
a[x][y]=1;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
{
if(a[j][i]==1&&a[i][k]==1)
a[j][k]=1;
}//递推人际关系,如果j管i,i管k,那么j就能管k
int output=0;
for(int i=1;i<=n;i++)
{
int cont=0;
for(int j=1;j<=n;j++)//遍历每一个人,如果a【i】【j】==1,说明i管j
{
if(a[i][j]==1)
cont++;
}
if(cont==k)
output++;
}
printf("%d\n",output);
}
}