hdoj 5326 Work (简单并查集)

作者: qwq 分类: ACM 发布时间: 2017-07-27 11:23

 

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.

InputThere 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
OutputFor 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

很简单的并查集,只需要把合并的算法删除掉就可以了

#include<iostream>

using namespace std;
int n,k;
int s[101];
int par[101];
void init(){
for(int i=0;i<101;i++){
	par[i]=i;
	s[i]=0;
} 
}
void setp(int a,int b){
	par[b]=a;
	s[a]+=s[b]+1;
	int t=a;
	while(par[t]!=t){
		t=par[t];
		s[t]+=s[b]+1;
	}
	
} 
int main(){
	int a,b;
	while(cin>>n>>k){
		init();
		int sum=0;
	for(int i=1;i<n;i++){
		cin>>a>>b;
		setp(a,b);
	}
	for(int i=1;i<=n;i++){
		if(s[i]==k){
			sum++;
		}
	}
	cout<<sum<<endl;	
	}
	
	
	return 0;
}

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注