POJ 1611

菜鸟第一次做这种。想了好一会儿.==

首先还是初始化记忆数组,使得每一个元素的初始根节点是自己。

然后是对输入的数据进行并集。我们拿出每组元素的第一个作为根节点。

每次检测是否已经存在根节点。如果存在根节点压缩该元素通往新的根节点的路径,同时将其原来的根节点接在新的根节点上。

第一次WA是因为忽略了剩余元素的根节点没有更新。倒数第二个循环用于把所有点都连接到根节点上。

#include<stdio.h>
#include<iostream>
using namespace std;
int stu[30000];
int main()
{
    int n,m,k,tmp1,tmp2,tmp3,er;
    scanf("%d%d",&n,&m);
    while(n)
    {
        for(int i=0;i<n;i++)
        {
            stu[i]=i;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d",&k);
            scanf("%d",&tmp1);
            for(int j=0;j<k-1;j++)
            {
                scanf("%d",&tmp2);
                while(stu[tmp2]!=tmp2)
                {
                    tmp3=tmp2;
                    tmp2=stu[tmp2];
                    stu[tmp3]=stu[tmp1];
                }
                stu[tmp2]=stu[tmp1];
            }
        }
        er=0;
        for(int i=0;i<n;i++)
        {
            tmp2=i;
            while(stu[tmp2]!=tmp2)
            {
                tmp2=stu[tmp2];
            }
            stu[i]=tmp2;
        }
        for(int i=0;i<n;i++)
        {
            if(stu[i]==stu[0])
            {
                er++;
            }
        }
        printf("%d\n",er);
        scanf("%d%d",&n,&m);
    }
    return 0;
}
时间: 2024-07-29 18:29:28

POJ 1611的相关文章

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

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 namespa

[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 Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 22217   Accepted: 10805 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

kuangbin专题五:B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects f

B - The Suspects POJ - 1611

B - The Suspects POJ - 1611 Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 61692   Accepted: 29146 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mi

【原创】poj ----- 1611 The Suspects 解题报告

题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24253   Accepted: 11868 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recogni

【裸的并查集】POJ 1611 The Suspects

http://poj.org/problem?id=1611 [Accepted] 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include <string> 6 using namespace std; 7 const int maxn=3e4+3; 8 int fa[maxn]; 9 int n,m; 10 int

POJ 1611 The Suspects(并差集)

链接:http://poj.org/problem?id=1611 The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 27854 Accepted: 13583 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a glob