【北大夏令营笔记-并查集】poj1611-The Suspects

The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 21517 Accepted: 10422
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
Source

Asia Kaohsiung 2003

//题意,有一些学生,加入了一些社团,包括0。一旦0患病,和他有关的人(同一团体)都是嫌疑对象。

问有多少嫌疑对象

//思路:并查集,将所有与0有关的人都放在同一集合,并把与0有关
的人所在的社团的所有人数统计

AC代码:

#include<stdio.h>
#define MAX 30000
int parent[MAX+10];
int total[MAX+10];
//total[Get_Root(a)]是a所在社团的人数
int Get_Root(int x)
{
    //获取a的根,并把a父节点改为根
    if(parent[x]!=x)
      parent[x]=Get_Root(parent[x]);
    return parent[x];
}
void Merge(int x,int y)
{
     x=Get_Root(x);
     y=Get_Root(y);
     if(x==y)
     return;
     parent[y]=x;
     total[x]+=total[y];
}
int main()
{
    int i,j,n,m,k,x,y;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)
        break;
        for(i=0;i<n;i++)
        {
           parent[i]=i;
           total[i]=1;
        }
        for(i=0;i<m;i++)
        {
           scanf("%d",&x);
           scanf("%d",&k);
           x--;
           while(x--)
           {
              scanf("%d",&y);
              Merge(k,y);
           }
        }
        printf("%d\n",total[Get_Root(0)]);
    }
    return 0;
}
时间: 2024-09-17 04:51:31

【北大夏令营笔记-并查集】poj1611-The Suspects的相关文章

【北大夏令营笔记-并查集】poj1988-Cube Stacking

Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18401 Accepted: 6378 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with

【北大夏令营笔记-数学题】百练1700-Crossing River

1700:Crossing River 查看 提交 统计 提示 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the

【北大夏令营笔记-线段树】POJ3468-A Simple Problem with Integers

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57993 Accepted: 17658 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of ope

【北大夏令营笔记-线段树】POJ3264-Balanced Lineup

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32777 Accepted: 15424 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John de

【北大夏令营笔记-动态规划】poj1458-Common Subsequence

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37543 Accepted: 15016 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., x

poj 1611 The Suspects:并查集

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

图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 B(G).其中 T(G)是遍历图时所经过的边的集合,B(G) 是遍历图时未经过的边的集合.显然,G1(V, T) 是图 G 的极小连通子图,即子图G1 是连通图 G 的生成树. 深度优先生成森林   右边的是深度优先生成森林: 连通图的生成树不一定是唯一的,不同的遍历图的方法得到不同的生成树;从不

并查集(Disjoint Set)

在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂,但数据量极大,若用正常的数据结构来描述的话,往往在空间上过大,计算机无法承受:即使在空间上勉强通过,运行的时间复杂度也极高,根本就不可能在规定的运行时间(1-3秒)内计算出试题需要的结果,只能用并查集来描述. 定义 并查集(Disjoint Set),即"不相交集合",是一种树型的数据结构

HDU 3038 How Many Answers Are Wrong? :带权并查集

链接: http://acm.hdu.edu.cn/showproblem.php?pid=3038 题目: Problem Description TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. T