codeup 9.6小节——数据结构专题(2)->并查集

问题 A: 通信系统

时间限制: 1 Sec  内存限制: 32 MB
提交: 1078  解决: 297
[提交][状态][讨论版][命题人:外部导入]

题目描述

某市计划建设一个通信系统。按照规划,这个系统包含若干端点,这些端点由通信线缆链接。消息可以在任何一个端点产生,并且只能通过线缆传送。每个端点接收消息后会将消息传送到与其相连的端点,除了那个消息发送过来的端点。如果某个端点是产生消息的端点,那么消息将被传送到与其相连的每一个端点。
为了提高传送效率和节约资源,要求当消息在某个端点生成后,其余各个端点均能接收到消息,并且每个端点均不会重复收到消息。
现给你通信系统的描述,你能判断此系统是否符合以上要求吗?

输入

输入包含多组测试数据。每两组输入数据之间由空行分隔。
每组输入首先包含2个整数N和M,N(1<=N<=1000)表示端点个数,M(0<=M<=N*(N-1)/2)表示通信线路个数。
接下来M行每行输入2个整数A和B(1<=A,B<=N),表示端点A和B由一条通信线缆相连。两个端点之间至多由一条线缆直接相连,并且没有将某个端点与其自己相连的线缆。
当N和M都为0时,输入结束。

输出

对于每组输入,如果所给的系统描述符合题目要求,则输出Yes,否则输出No。

样例输入

4 3
1 2
2 3
3 4

3 1
2 3

0 0

样例输出

Yes
No

思路:1.只有一组 多了就不行No
        2.已经在同一个集合却又有连线一定会导致环的出现 不行No. 

#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn=1010;
int n,m;
int father[maxn];

void initFather(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
}

int findFather(int x){
	int a=x;
	while(x!=father[x]){
		x=father[x];
	}
	while(a!=father[a]){
		int z=a;
		a=father[a];
		father[z]=x;
	}
	return x;
}

void Union(int a,int b){
	int faA=findFather(a);
	int faB=findFather(b);
	if(faA!=faB){
		father[faA]=faB;
	}
}

int main(){
	// freopen("inputa.txt","r",stdin);
	int a,b;
	bool hasRing=false;
	while((cin>>n>>m)&&n){
		initFather(n);
		hasRing=false;
		for(int i=0;i>a>>b;
			if(findFather(a)==findFather(b)){
				hasRing=true;//不能break 要接收完break
			}
			if(!hasRing){//无环才合并 否则没意义
				Union(a,b);
			}
		}
		if(!hasRing){
			//判断是否共用一个根
			int root=findFather(1);
			int i;
			for(i=2;i<=n;i++){
				if(root!=findFather(i)) break;
			}
			if(i>n) cout<<"Yes\n";
			else cout<<"No\n";//有多组
		}else{
			cout<<"No\n";
		}
	}
	return 0;
}

 

问题 B: 畅通工程

时间限制: 1 Sec  内存限制: 32 MB
提交: 291  解决: 164
[提交][状态][讨论版][命题人:外部导入]

题目描述

某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?

输入

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。 
    注意:两个城市之间可以有多条道路相通,也就是说
    3 3
    1 2
    1 2
    2 1
    这种输入也是合法的
    当N为0时,输入结束,该用例不被处理。

输出

对每个测试用例,在1行里输出最少还需要建设的道路数目。

样例输入

5 3
1 2
3 2
4 5
0

样例输出

1

判断有几组即可,答案等于组数-1

#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn=1010;
int father[maxn];
int n,m;
int Hash[maxn];

void initfather(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
}

int findfather(int x){
	int a=x;
	while(x!=father[x]){
		x=father[x];
	}
	while(a!=father[a]){
		int z=a;
		a=father[a];
		father[z]=x;
	}
	return x;
}

void Union(int a,int b){
	int faA=findfather(a);
	int faB=findfather(b);
	if(faA!=faB){
		father[faA]=faB;
	}
}

int main(){
	// freopen("inputb.txt","r",stdin);
	int a,b;
	while(cin>>n>>m&&n){
		initfather(n);
		memset(Hash,0,sizeof(Hash));
		for(int i=0;i>a>>b;
			Union(a,b);//重复了会直接避免的
		}
		//查找有几组即可
		int num=0;
		for(int i=1;i<=n;i++){
			int F=findfather(i);
			if(Hash[F]==0){
				Hash[F]=1;
				num++;
			}
		}
		cout<

 

问题 C: How Many Tables

时间限制: 1 Sec  内存限制: 32 MB
提交: 136  解决: 106
[提交][状态][讨论版][命题人:外部导入]

题目描述

Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.

输入

The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.

输出

For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.

样例输入

2
6 4
1 2
2 3
3 4
1 4

8 10
1 2
2 3
5 6
7 5
4 6
3 6
6 7
2 5
2 4
4 3

样例输出

3
2
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn=1010;
int father[maxn];
int n,m;
int Hash[maxn];

void initfather(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
}

int findfather(int x){
	int a=x;
	while(x!=father[x]){
		x=father[x];
	}
	while(a!=father[a]){
		int z=a;
		a=father[a];
		father[z]=x;
	}
	return x;
}

void Union(int a,int b){
	int faA=findfather(a);
	int faB=findfather(b);
	if(faA!=faB){
		father[faA]=faB;
	}
}

int main(){
	// freopen("inputc.txt","r",stdin);
	int a,b,T;
	cin>>T;
	while(T--){
		cin>>n>>m;
		initfather(n);
		memset(Hash,0,sizeof(Hash));
		for(int i=0;i>a>>b;
			Union(a,b);//重复了会直接避免的
		}
		//查找有几组即可
		int num=0;
		for(int i=1;i<=n;i++){
			int F=findfather(i);
			if(Hash[F]==0){
				Hash[F]=1;
				num++;
			}
		}
		cout<

 

问题 D: More is better

时间限制: 1 Sec  内存限制: 128 MB
提交: 368  解决: 133
[提交][状态][讨论版][命题人:外部导入]

题目描述

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.

输入

The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)

输出

The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.

样例输入

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

样例输出

4
5

找到最长的那棵树的长度即可。注意n==0时特判断1     (测试数据貌似一共两组。。。)

#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn=10000010;
int father[maxn];
int Hash[maxn];

void initfather(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
}

int findfather(int x){
	int a=x;
	while(x!=father[x]){
		x=father[x];
	}
	while(a!=father[a]){
		int z=a;
		a=father[a];
		father[z]=x;
	}
	return x;
}

void Union(int a,int b){
	int faA=findfather(a);
	int faB=findfather(b);
	if(faA!=faB){
		father[faA]=faB;
	}
}

int main(){
	// freopen("inputd.txt","r",stdin);
	int a,b,n,Max;
	while(cin>>n){
		if(n==0){
			cout<<"1\n";
			continue;
		}
		initfather(maxn);
		memset(Hash,0,sizeof(Hash));
		Max=0;
		for(int i=0;i>a>>b;
			Max=max(max(Max,a),b);
			Union(a,b);
		}
		for(int i=1;i<=Max;i++){
			Hash[findfather(i)]++;
		}
		cout<<*max_element(Hash+1,Hash+Max+1)<

 

 

你可能感兴趣的:(算法竞赛,codeup墓地,数据结构,#,树)