POJ1611 The Suspects: 并查集入门

#include<iostream>
using namespace std;
#define Size 30000
int Pre[Size+1], Sum[Size+1];// Sum[i] 表示 第i组 的人数

int Get_Pre( int a )
{
        if( Pre[a]!=a )
            a = Get_Pre( Pre[a] );// 路径压缩
        return a;
}

void Union( int a, int b )
{
        int P1 = Get_Pre(a);
        int P2 = Get_Pre(b);
        if( P1 == P2 )
            return ;
        Pre[P2] = P1;
        Sum[P1] += Sum[P2];
}

int main()
{
        int n, m;
        while( cin>>n>>m && n!=0 )
        {
                for( int i=0; i<n; i++ )
                {
                        Pre[i] = i;     Sum[i]=1;
                }
                int k, N1, N2;
                for( int i=0; i<m; i++ )
                {
                        cin>>k;
                        cin>>N1;
                        for( int j= 1; j<k; j++ ){
                                cin>>N2;
                                Union( N1, N2 );
                        }
                }
                cout<<Sum[Get_Pre(0)]<<endl;
        }
        return 0;
}
时间: 2024-12-26 19:15:20

POJ1611 The Suspects: 并查集入门的相关文章

hdu1272并查集入门

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 41540    Accepted Submission(s): 12811 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是

[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

hdu 1213 并查集入门

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12538    Accepted Submission(s): 6145 Problem Description Today is Ignatius' b

并查集入门(转)

并查集入门 并查集学习: l 并查集:(union-find sets) 一种简单的用途广泛的集合. 并查集是若干个不相交集合,能够实现较快的合并和判断元素所在集合的操作,应用很多,如其求无向图的连通分量个数等.最完美的应用当属:实现Kruskar算法求最小生成树. l 并查集的精髓(即它的三种操作,结合实现代码模板进行理解): 1.Make_Set(x) 把每一个元素初始化为一个集合 初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变). 2.Find_S

The Suspects (并查集)

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 from others. In the Not-Sprea

POJ 1611 The Suspects (并查集+数组记录子孙个数 )

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

并查集入门

我是看着<啊哈!算法>这本书完成并查集的入门,本想举出另外的栗子,奈何...书上的栗子已经很贴切了. 首先引入一个问题:已知有10个土匪,警方需要需要一点点顺藤摸瓜最后挖出他们各自背后的团伙一锅端,经过一段时间的侦查警方的得到了9条确切线索,分别能说明那两个土匪的归顺关系.那么请问此次行动总共要打掉几个团伙? 输入数据如下: 按照第一行输入人数n,线索数m,接下来的m行输入线索,每行线索如1 2代表1号和2号土匪是一伙的.现提供一组数据以供后续讲解和程序测试: <span style=&

The Suspects(并查集维护根节点信息)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 37090   Accepted: 17980 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 (并查集求数量)

Description 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 from others. In t