poj 2492 并查集 A Bug's Life
2011-05-27 16:51
337 查看
这道题可以用disset数组来装每个虫的对象,并把对象合并到一起,
然后看看如果偶有一组的数根相等,那么就是有问题了
#include<iostream>
using namespace std;
#define max 2010
int pa[max];
int rank[max];
int disset[max];
void make_set(int x)
{
pa[x]=x;
rank[x]=0;
disset[x]=-1;
return ;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y) return ;
if(rank[x]>rank[y])
pa[y]=x;
else
{
pa[x]=y;
if(rank[x]==rank[y])
rank[y]++;
}
return ;
}
int main()
{
int p=1;
int cases;
bool flag;
int x,y;
cin>>cases;
while(cases--)
{
int bug;
long pair;
scanf("%d%ld",&bug,&pair);
for(int i=0;i<=bug;i++)//初始化赋值让我给整错了
make_set(i);
flag=false;
while(pair--)
{
scanf("%d%d",&x,&y);
if(flag)
continue;
if(find_set(x)==find_set(y))
flag=true;
else
{
if(disset[x]!=-1)
{
union_set(disset[x],y);
}
else
disset[x]=y;//这个过程中只要状对象就行,矛盾在于对象
if(disset[y]!=-1)
{
union_set(x,disset[y]);
}
else
disset[y]=x;
}
// union_set(x,y);
}
printf("Scenario #%d:/n",p++);
if(flag)
printf("Suspicious bugs found!/n/n");
else
printf("No suspicious bugs found!/n/n");
}
return 0;
}
然后看看如果偶有一组的数根相等,那么就是有问题了
#include<iostream>
using namespace std;
#define max 2010
int pa[max];
int rank[max];
int disset[max];
void make_set(int x)
{
pa[x]=x;
rank[x]=0;
disset[x]=-1;
return ;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)
{
x=find_set(x);
y=find_set(y);
if(x==y) return ;
if(rank[x]>rank[y])
pa[y]=x;
else
{
pa[x]=y;
if(rank[x]==rank[y])
rank[y]++;
}
return ;
}
int main()
{
int p=1;
int cases;
bool flag;
int x,y;
cin>>cases;
while(cases--)
{
int bug;
long pair;
scanf("%d%ld",&bug,&pair);
for(int i=0;i<=bug;i++)//初始化赋值让我给整错了
make_set(i);
flag=false;
while(pair--)
{
scanf("%d%d",&x,&y);
if(flag)
continue;
if(find_set(x)==find_set(y))
flag=true;
else
{
if(disset[x]!=-1)
{
union_set(disset[x],y);
}
else
disset[x]=y;//这个过程中只要状对象就行,矛盾在于对象
if(disset[y]!=-1)
{
union_set(x,disset[y]);
}
else
disset[y]=x;
}
// union_set(x,y);
}
printf("Scenario #%d:/n",p++);
if(flag)
printf("Suspicious bugs found!/n/n");
else
printf("No suspicious bugs found!/n/n");
}
return 0;
}
相关文章推荐
- poj 并查集 - 2492 A Bug's Life
- 【带偏移量的并查集】:poj2492,A Bug's Life
- poj 2492 并查集 a bug's life
- poj 2492 A BUG'S LIFE 不是纯裸的并查集
- Hdu 1892&&Poj 2492 A Bug's Life[判断二分图 || 种类并查集]
- POJ 2492--A Bug's Life 【并查集,向量偏移】
- POJ - 2492 A Bug's Life(种类并查集)
- POJ 2492 A Bug's Life(扩展并查集)
- [NWPU][2014][TRN][12]并查集D - A Bug's Life POJ 2492
- poj2492 A Bug's Life(并查集)
- POJ 2492 A Bug's Life (并查集)
- POJ 2492 || HDU 1829:A Bug's Life(并查集)
- poj 2492 A Bug's Life 【并查集拓展】
- poj 2492 A Bug's Life(并查集)分组并查集
- POJ 2492 A Bug's Life(并查集)
- HDU 1829 && POJ 2492 A Bug's Life(种类并查集)
- POJ 2492 A Bug's Life 并查集
- poj2492_A Bug's Life
- POJ 2492 A Bug's Life
- A Bug's Life 【POJ--2492】【带权的并查集】