poj1611 简单并查集

The Suspects

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 32781   Accepted: 15902

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 the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP).
Once a member in a group is a suspect, all members in the group are suspects.
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4
1
1解析见代码:
/*
题目大意:有n个人,编号0~n-1,其中0号可能携带有病毒
和他在一个团体里的人也可能有病毒,同时也是病毒传染者,
问你一共有多少个人可能携带病毒
思路分析:简单并查集,将元素合并为若干个集合,注意在merge
过程中同时维护该集合中的人数,最后输出0号所在的集合的人数即可
*/
#include <iostream>
#include <cstdio>
#include<cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=30000+100;
int father[maxn];
int total[maxn];//合并集合的时候需要将两个集合中的元素数目同时进行合并
int ans;
int n,m;
int findroot(int x)
{
    return (x==father[x])?x:father[x]=findroot(father[x]);
}
void merge(int a,int b)
{
    int x=findroot(a);
    int y=findroot(b);
    if(x==y) return;//两者在同一个集合里
    total[x]+=total[y];
    father[y]=x;
}
int main()
{
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        for(int i=0;i<n;i++)//并查集初始化
        {
            father[i]=i;
            total[i]=1;
        }
        int k;
        int a,b;;
        while(m--)
        {
            scanf("%d",&k);
            scanf("%d",&a);
            for(int i=1;i<k;i++)
            {
                scanf("%d",&b);
                merge(a,b);
            }
        }
        printf("%d\n",total[findroot(father[0])]);//此处findroot(father[0])不能写作
    }
}
时间: 2024-12-19 15:22:45

poj1611 简单并查集的相关文章

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

题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋.然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋 这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些 sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1 每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2; 还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有 paren

【简单并查集】Farm Irrigation

Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Benny has a spacious fa

POJ 2524 Ubiquitous Religions (简单并查集,三种方式)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 30791   Accepted: 14928 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in findi

HDU 1272 简单并查集

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

poj2524(简单并查集)

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;int n,m;int bin[50001];int findx(int x){    int r=x;    while(r!=bin[r])        r=bin[r];    int j=x,k;    while(j!=r)    {        k=bin

POJ - 2236 Wireless Network(简单并查集)

Wireless Network Time Limit: 10000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap co

POJ - 2524 Ubiquitous Religions(简单并查集)

Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰. 你知道在你的大学里有n个学生(0 < n <= 50000) .你无法询问每个学生的宗教信仰.此外,许多学生不想说出他们的信仰.避免这些问题的一个方法是问

ACM_“打老虎”的背后(简单并查集)

"打老虎"的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: "习大大"自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步加强.中国的这种令外国人不解的"风情"到底是何缘由呢?其实中国人都知道,所有的腐败问题基本上都源自官场上的"潜规则",那"潜规则"又是源自什么呢?在中国盛行一句古语,"无关系不成方圆",说的

poj1611(并查集简单应用)

题目链接:http://poj.org/problem?id=1611 思路: 显然是一个并查集的题,很简单,只要将一个group中的学生并在一起,最后遍历1到n-1,看有多少学生的祖先与0的祖先相等即可. 代码如下: 1 #include<cstdio> 2 using namespace std; 3 4 int n,m,res,root[30005]; 5 6 int getr(int k){ 7 if(root[k]==k) return k; 8 else return root[k