ZOJ 1789
2010-12-04 14:39
363 查看
//2363500 2010-12-02 21:50:28 Accepted 1789 C 10 392 VRS
//1789 SARS隔离 并查集
#include<stdio.h>
#include<string.h>
int setFather[30005];
int setCount[30005];
int Find(int x)
{
int root=x;
while(setFather[root]!=root)
root=setFather[root];
while(setFather[x]!=x)
{
setFather[x]=root;
x=setFather[x];
}
return root;
}
void Union(int x,int y)
{
int rootX=Find(x);
int rootY=Find(y);
if(rootX==rootY)
return;
else if(rootX<rootY) //可以让下标小的作为根结点
{
setFather[rootY]=rootX;
setCount[rootX]+=setCount[rootY];
}
else
{
setFather[rootX]=rootY;
setCount[rootY]+=setCount[rootX];
}
}
int main()
{
int n,m;
int i,j;
int tempNum;
int groupFir,groupSec;
while(scanf("%d %d",&n,&m) && !(n==0 && m==0))
{
for(i=0;i<n;i++)
{
setFather[i]=i;
setCount[i]=1;
}
for(i=0;i<m;i++)
{
scanf("%d %d",&tempNum,&groupFir);
//每次都对当前集合合并,如果有重复的就会合并到前面的集合中,否则生成新的集合
for(j=0;j<tempNum-1;j++)
{
scanf("%d",&groupSec);
Union(groupFir,groupSec);
}
}
printf("%d\n",setCount[0]);
}
return 0;
}
//1789 SARS隔离 并查集
#include<stdio.h>
#include<string.h>
int setFather[30005];
int setCount[30005];
int Find(int x)
{
int root=x;
while(setFather[root]!=root)
root=setFather[root];
while(setFather[x]!=x)
{
setFather[x]=root;
x=setFather[x];
}
return root;
}
void Union(int x,int y)
{
int rootX=Find(x);
int rootY=Find(y);
if(rootX==rootY)
return;
else if(rootX<rootY) //可以让下标小的作为根结点
{
setFather[rootY]=rootX;
setCount[rootX]+=setCount[rootY];
}
else
{
setFather[rootX]=rootY;
setCount[rootY]+=setCount[rootX];
}
}
int main()
{
int n,m;
int i,j;
int tempNum;
int groupFir,groupSec;
while(scanf("%d %d",&n,&m) && !(n==0 && m==0))
{
for(i=0;i<n;i++)
{
setFather[i]=i;
setCount[i]=1;
}
for(i=0;i<m;i++)
{
scanf("%d %d",&tempNum,&groupFir);
//每次都对当前集合合并,如果有重复的就会合并到前面的集合中,否则生成新的集合
for(j=0;j<tempNum-1;j++)
{
scanf("%d",&groupSec);
Union(groupFir,groupSec);
}
}
printf("%d\n",setCount[0]);
}
return 0;
}
相关文章推荐
- zoj 1789 The Suspects (并查集)
- zoj 1789
- zoj 1789 判汉密顿图
- zoj - 1789 - The Suspects
- ZOJ 2158/POJ 1789 - Truck History
- zoj 2158 poj 1789 Truck History (Prim)
- zoj - 1789 - The Suspects
- zoj 1789 The Suspects
- ZOJ1789(The Suspects)j简单并查集
- ZOJ 2158 && POJ 1789 Truck History (经典MST)
- zoj 1789 The Suspects
- zoj 1789 The Suspects
- zoj1789(并查集)
- zoj 1789
- zoj 1789 The Suspects
- Truck History POJ 1789 ZOJ 2158 Prim算法
- ZOJ 1789 水水的并查集,刚开始学会并查集,做了此题。经过一次失败后终于AC了哈哈~ AC感觉就是好啊!与大家共分享啊
- zoj 1789 The Suspects
- ACM zoj 1789(并查集实现)
- zoj 1789||poj 1611 The Suspects(并查集,简单)