2017年上海金马五校程序设计竞赛(网上资格赛)Problem B : Coach

Description

In the ACM training team, the coach wants to split all students into many groups and each group consists of three students. Let's assume that all students are numbered from 11 to nn. The teacher wants the groups to achieve good results, so he wants to hold the following condition: In a group of three students, they are friends. Also it is obvious that each student must be in exactly one team. If AA and BB are friends, BB and CC are friends, then AA and CC are friends.

 

Input

There are multiple test cases.

For each test case, the first line contains integers nn and mm (1n5000,0m5000)(1≤n≤5000,0≤m≤5000). Then follows mm lines, each contains a pair of integers ai,biai,bi (1ai,bin)(1≤ai,bi≤n). The pair ai,biai,bi means that the students aiai and bibi are friend. It is guaranteed that each pair of aiai and bibi occurs in the input at most once.

 

Output

If the required group division plan doesn't exist, print “No”. Otherwise, print “Yes”.

 

Sample Input

3 0
6 4
1 2
2 3
4 5
5 6

 

Sample Output

No
Yes


这个题吧,就是并查集,我原先是去查每一个人所在的那可树上是不是都等于3,就yes,但是我后来发现了问题,如果六个人都是朋友的话那也可以3 3分配,所以我把判断条件改成了3的整数倍


#include 
#include 
using namespace std;
map p;
int par[100005],r[100005],num[100005];
void init(){
	for(int i=0;i<100005;i++){
		par[i]=i;
		num[i]=1;
	}
}
int findroot(int x){
	if(par[x]==x) return x;
	else return par[x]=findroot(par[x]);
}
void unite(int x,int y){
	x=findroot(x);
	y=findroot(y);
	if(x==y){
		return;
	}
	par[y]=x;
	num[x]+=num[y];
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		if(m == 0) {
			printf("No\n");
			continue;
		}
		p.clear();
		init();
		int k=1;
		for(int i=0;i




你可能感兴趣的:(ACM划水)