您的位置:首页 > 理论基础

2005年浙江大学计算机及软件工程研究生机试真题并查集UnionFindjava实现

2017-03-13 16:58 330 查看
//并查集中将两个阵营的团队结合
public static void mix(int pre[],int x,int y)
{
int a = findRoot(pre,x);
int b = findRoot(pre,y);
if(a!=b)
pre[a] = b;//赋有将共同的boss
}


并查集在检查连通性方面非常好,一般用在求两点之间有没有路径等方面

其主要包括三个方面:

1.查找根结点(检查你所要找的点所属的根结点,对于一些联通的点来说任一节点都可以作为根结点,但对于这些联通的点来说根结点只有一个)

2.状态压缩(就是把联通的点的所有的根结点都定位同一个根结点)

3.将两个连通区域的点构建连通性。

查找根结点,状态压缩

public static int findRoot(int pre[],int x)
{
int temp = x;
while(pre[temp]!=temp)//找根
{
temp = pre[temp];
}
//状态压缩
int j = x;
while(pre[j]!=temp)
{//追本溯源凡是x上级的都把他们的根赋予他们
int pr = pre[j];
pre[j] = temp;
j = pre[j];
}
return temp;
}
构建连通性

//并查集中将两个阵营的团队结合
public static void mix(int pre[],int x,int y)
{
int a = findRoot(pre,x);
int b = findRoot(pre,y);
if(a!=b)
pre[a] = b;//赋有将共同的boss
}
2005年浙江大学计算机及软件工程研究生机试真题

下面是完整的实现代码

import java.util.Scanner;

public class unionFind1012Jo {

//并查集,首先是找到其中的根结点,再进一步进行状态压缩
public static int findRoot(int pre[],int x)
{
int temp = x;
while(pre[temp]!=temp)//找根
{
temp = pre[temp];
}
//状态压缩
int j = x;
while(pre[j]!=temp)
{//追本溯源凡是x上级的都把他们的根赋予他们
int pr = pre[j];
pre[j] = temp;
j = pre[j];
}
return temp;
}
//并查集中将两个阵营的团队结合 public static void mix(int pre[],int x,int y) { int a = findRoot(pre,x); int b = findRoot(pre,y); if(a!=b) pre[a] = b;//赋有将共同的boss }
public static void main(String[] args)
{
// TODO Auto-generated method stub
int[] pre = new int[1000];

Scanner in = new Scanner(System.in);
int count = 0;
while((count = in.nextInt())!=0)
{
for(int i = 1;i< 1000;i++)
{
pre[i] = i;
}
int num = count-1;
int i = in.nextInt();

while(i-->0)
{
int a = in.nextInt();
int b = in.nextInt();
if(findRoot(pre,a)!=findRoot(pre,b))
{
mix(pre,a,b);
num--;
}
}
System.out.println(num);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算机 acm java 并查集
相关文章推荐