复试训练——图论—— 并查集

首先我们定义一个数组,用双亲表示法来表示各棵树

int Tree[N];

有Tree[i]来表示结点为i的双亲结点,若Tree[i]为-1则表示该节点不存在双亲结点,即结点i为其所在的树的根结点。

那么,为了查找结点x的所在的树的双亲结点我们定义以下递归函数:

int findRoot(int x){
   if(Tree[x]==-1) return x;
   else return findRoot(Tree[x]);
}

非递归形式的函数:

int findRoot(int x){
    int ret;
    while(Tree[x]==-1)
       x=Tree[x];
       ret=x;
       return x;
        }

在查找的过程中添加路径优化,修改上述函数(非递归形式):

int  findRoot(int x){
     if(Tree[x]==-1) return x;
     else {
          int tmp=findRoot(Tree[x]);
          Tree[x]=tmp;
           return tmp;
  }
}

递归形式:

int findRoot(int x){
    int ret;
    int tmp=x;
    while(Tree[x]!=-1)
        x=Tree[x];
        ret=x;
        x=tmp;
        while(Tree[x]!=-1){
             int t=Tree[x];
             Tree[x]=ret;
             x=t;
  }
}

题目1012:畅通工程

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:8628

解决:3811

题目描述:

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

输入:

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

输出:

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

样例输入:

4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0

样例输出:

1
0
2
998

来源:

2005年浙江大学计算机及软件工程研究生机试真题

代码:

#include <stdio.h>

#define N 1000
int Tree[N];
int findRoot(int x){
	if(Tree[x]==-1) return x;
	else{
		 int tmp=findRoot(Tree[x]);
		     Tree[x]=tmp;
			 return tmp;
	}
}
int main(){
	int n,m;
	while(scanf("%d",&n)!=EOF&&n!=0){
		scanf("%d",&m);
		int i;
		for(i=1;i<=n;i++){
			Tree[i]=-1;
		}
		while(m--!=0){
			int a,b;
			scanf("%d%d",&a,&b);
			a=findRoot(a);
			b=findRoot(b);
			if(a!=b) Tree[a]=b;
		}
		int ans=0;
		for(i=1;i<=n;i++){
			if(Tree[i]==-1) ans++;
		}
		printf("%d\n",ans-1);
	}
	return 0;
}

题目1444:More is better

时间限制:3 秒

内存限制:100 兆

特殊判题:

提交:3060

解决:1397

题目描述:

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.

样例输入:

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

样例输出:

4
2

代码:

#include <stdio.h>
#define N 10000001
int Tree[N];
int findRoot(int x){
	if(Tree[x]==-1) return x;
	else {
		int tmp=findRoot(Tree[x]);
		    Tree[x]=tmp;
			return tmp;
	}
}
int sum[N];
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		int i;
		for(i=1;i<=N;i++){
			Tree[i]=-1;
			sum[i]=1;
		}
		while(n--!=0){
			int a,b;
			scanf("%d%d",&a,&b);
			a=findRoot(a);
			b=findRoot(b);
			if(a!=b){ 
				Tree[a]=b;
				sum[b]+=sum[a];
			}
		}
		int ans=0;
		for(i=1;i<=N;i++){
			if(Tree[i]==-1&&sum[i]>ans) ans=sum[i];
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

转载于:https://my.oschina.net/u/1996306/blog/831182

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值