POJ 1611 The Suspects(特别无语的并查集)

很简单的一道题目,开始用的是并查集的分离集合森林做,不知道怎么的特别不稳定,太奇怪了,WA无数次,无奈之下改成一维数组了。。sad

AC

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define PI acos(-1,0)
using namespace std;
const int maxn = 30010;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
using namespace std;

int father[50000];
int num[50000],n,m;

int Find(int r)
{
    int i = r,j;
    while(father[r]!=r)
    {
         r=father[r];
    }

    while(father[i]!=r)
    {
        j = father[i];
        father[i] = r;
        i = j;
    }
    return r;
}

void init()
{
	for(int i = 0;i < n;i++)
	{
		father[i] = i;
		num[i] = 1;
	}
}
void Merge(int l,int b)
{
    int y = Find(b);
        if(l != y)
        {
            father[y] = l;
        num[l] +=  num[y];
        }
}
int main()
{
	int j,k,l;
	while(~scanf("%d%d",&n,&m))
	{
		if(n==0 && m==0)
			break;
		init();
		int b;
		while(m--)
		{
			scanf("%d%d",&k,&l);

			int x = Find(l);
			for(j = 1;j < k;j++)
			{
				scanf("%d",&b);
				Merge(x,b);
			}

		}
	    printf("%d\n",num[Find(0)]);
	}
	return 0;
}

WA

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define PI acos(-1,0)
using namespace std;
const int maxn = 30010;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b

using namespace std;

struct node
{
    int zhi , parent;
} T[maxn];

int Find(int x)
{
	return (x == T[x].parent) ? x : Find(T[x].parent);
}

void Merge(int x,int y)
{
    int fx,fy;

    fx = Find(x);
    fy = Find(y);
    if(fx!=fy)
    {
        T[fy].parent = fx;
        T[fx].zhi += T[fy].zhi;
    }
}

int main()
{
    int m,n,k;
    while (~scanf("%d%d",&n,&m))
    {
        if (n==0 && m==0) break;

        for(int j = 0; j<n; j++)
        {
            T[j].zhi = 1;
            T[j].parent = j;
        }
       for(int i = 1; i <= m; ++i)
		{
			scanf("%d", &k);
			int *aa = new int[k];
			for(int j = 0; j < k; ++j)
			{
				scanf("%d", &aa[j]);
				if(j != 0)
					Merge(aa[j - 1], aa[j]);
			}
			delete(aa);
		}
        printf("%d\n",T[T[0].parent].zhi);

    }
    return 0;
}

5 3

1 2

2 0 1

3 1 4 5

answer  3

有时是正确结果,有时不是,特别无语啊。

3

POJ 1611 The Suspects(特别无语的并查集)

时间: 2024-10-11 04:51:57

POJ 1611 The Suspects(特别无语的并查集)的相关文章

POJ 1611 The Suspects(并查集)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22296   Accepted: 10842 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

poj 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

【转】POJ 2492 A Bug&#39;s Life:基础并查集进阶

思路参考这里(较详细) 一开始总是WA调了一晚上原来···Init初始化写在scanf上面了···哎╮(╯▽╰)╭anyway!对并查集的理解更深了一步! #include<cstdio> #include<cstring> using namespace std; #define Size 2000 struct node { int Pre; int Relation;// 与父节点的关系 0同性 1异性 }Bug[Size+1]; int N, M; bool found;

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

Poj 1182种类(带权)并查集

题目链接 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44316 Accepted: 12934 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是

POJ 2492 A Bug&#39;s Life (并查集)

Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their in

poj 2492 A Bug&#39;s Life 【并查集拓展】

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 29030   Accepted: 9455 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

poj 1182 食物链 (带关系的并查集)

  食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同类.