Poj 1611The Suspects
来源:互联网 发布:iqr淘宝网首页 编辑:程序博客网 时间:2024/06/03 18:15
#include<iostream>
#include<stdio.h>
using namespace std;
#define N 30005
int father[N],count[N];
void makeSet(int x)
{
father[x]=x;
count[x]=1;
}
int findRoot(int x)
{
if(x!=father[x])
x=findRoot(father[x]);
return x;
}
void unionSet(int a,int b)
{
a=findRoot(a);
b=findRoot(b);
if(a==b)
return ;
if(count[a]<=count[b])
{
father[a]=b;
count[b]+=count[a];
}else
{
father[b]=a;
count[a]+=count[b];
}
}
int main(int argc,char *argv[])
{
freopen("input1.txt","r",stdin);
int n,m,t,a,b;
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
for(int i=0;i<n;i++)
makeSet(i);
while(m--)
{
scanf("%d%d",&t,&a);
t--;
while(t--)
{
scanf("%d",&b);
unionSet(a,b);
}
}
printf("%d\n",count[findRoot(0)]);
}
return 0;
}
#include<stdio.h>
using namespace std;
#define N 30005
int father[N],count[N];
void makeSet(int x)
{
father[x]=x;
count[x]=1;
}
int findRoot(int x)
{
if(x!=father[x])
x=findRoot(father[x]);
return x;
}
void unionSet(int a,int b)
{
a=findRoot(a);
b=findRoot(b);
if(a==b)
return ;
if(count[a]<=count[b])
{
father[a]=b;
count[b]+=count[a];
}else
{
father[b]=a;
count[a]+=count[b];
}
}
int main(int argc,char *argv[])
{
freopen("input1.txt","r",stdin);
int n,m,t,a,b;
while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
{
for(int i=0;i<n;i++)
makeSet(i);
while(m--)
{
scanf("%d%d",&t,&a);
t--;
while(t--)
{
scanf("%d",&b);
unionSet(a,b);
}
}
printf("%d\n",count[findRoot(0)]);
}
return 0;
}
0 0
- POJ 1611 The Suspects
- poj 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ-1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 - The Suspects
- POJ 1611 The Suspects
- Poj 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 The Suspects
- POJ 1611 The Suspects
- POJ 1611 The Suspects
- poj 1611 the suspects
- POJ 1611 The Suspects
- Lua学习笔记三之从Lua中调用C
- [PHP] Easy popup in Magento
- poj3468(树状数组)
- ZOJ-1720
- CMake error: error in configuration process, project files may be invalid
- Poj 1611The Suspects
- 最详细的代理讲解--JDK动态代理和cglib代理 (类比spring中事物的代理)
- 蛇形螺旋矩阵的生成和遍历 Spiral Matrix
- 敏捷实践总结(四)——Scrum第一个Sprint实践经验积累
- win7 64位操作系统配置android开发环境
- 中移动蠢蠢欲动
- Android内存优化
- 【转】B-tree/B+tree/B*tree
- 华为面试归来