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. 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

/*
题意:总共有n个学生编号0~n-1,总共m个社团,现在0号学生是可能感染病菌的学生,那么和他在一个社团的学生需要
    被隔离,这些学生如果还在其他社团的话,那么那个社团的所有学生也要被隔离,现在问你需要隔离多少学生

初步思路:并查集,用一个数组来维护一棵树的节点数量,所有只需要输出0所在的树的节点数量就行了
*/
#include <iostream>
#include <stdio.h>
using namespace std;
int n,m;
int a,last;
int k;
int bin[30005];
int num[30005];
int findx(int x){
    int tmp=x;
    while(x!=bin[x]){
        x=bin[x];
    }
    bin[tmp]=x;//路径压缩
    return x;
}
int bindx(int x,int y){
    int fx=findx(x);
    int fy=findx(y);
    if(fx!=fy){
        num[fy]+=num[fx];//这两个点相联合了,那么根节点的信息要加到一块
        bin[fx]=fy;
        return 1;
    }
    return 0;
}
void init(){
    for(int i=0;i<=n;i++){
        bin[i]=i;
        num[i]=1;
    }
}
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
        init();
        while(m--){
            scanf("%d",&k);
            for(int i=0;i<k;i++){
                scanf("%d",&a);
                if(i){
                    bindx(a,last);
                }
                last=a;
            }
        }
        printf("%d\n",num[findx(0)]);
    }
    return 0;
}
时间: 2024-12-29 11:31:06

The Suspects(并查集维护根节点信息)的相关文章

(hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 562 Accepted Submission(s): 239   Problem Description Five hundred years later, the number of dragon balls will increase unexpected

畅通工程(并查集找根节点)

畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 203 Accepted Submission(s): 168   Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间

[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

并查集间单个节点的转移(UVa 11987 Almost Union-Find)

从来没有这么艰难地完成一道算法题过!经过8次失败之后总算提交成功了!所以决定写一篇博文,对并查集的相关内容做一些总结. 普通并查集的操作无非是两种,find_set(x)即找到节点x所在的集合的代表节点,或者是union_set(x,y),即将x和y所在的两个集合合并起来.如下图所示,有左右两个并集 通常,我们会选用并查集中父节点为自己的元素作为这个并查集的代表,例如图中的节点a和节点e.那么,我们如何通过集合中的一个节点找到该节点所在集合的代表节点呢?其实很简单,例如上图中的d节点,它首先通过

[Codeforces 1027 F] Session in BSU [并查集维护二分图匹配问题]

题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可以利用这个性质 考虑一个左边的点和它右边联通的两个点,发现这两个点只能选择一个和这个左边的点匹配 那么我们考虑把这个点点匹配的模型转化成点边匹配 我们在同一个左边点连的两个右边点之间连边,那么问题就变成了一个点和一条相邻的边匹配,求完美匹配的问题了 而这个问题,我们显然可以用并查集来很好的解决 考虑

[USACO18JAN] MooTube (并查集 -&gt; 维护连通性)

题目大意:给你一棵边权树,定义两点间距离为它们唯一路径上的最小路权,求与某点距离不大于K(k为已知)的点的数量 带权并查集维护集合内元素总数 路和问题 都按权值大到小排序,枚举问题, 建权值不小于K的边,并查集维护连通性,求集合元素内总数即可 1 #include <bits/stdc++.h> 2 #define N 200100 3 #define inf 0x3f3f3f3f 4 using namespace std; 5 6 int n,q,cnt; 7 int fa[N],f[N]

[USACO18OPEN] Multiplayer Moo (并查集+维护并查集技巧)

题目大意:给你一个N*N的棋盘,棋盘上每个点都有一个权值 第一问求一个权值形成的最大联通块中点的数量 第一问求两个权值共同形成的最大联通块中点的数量 提供一种并查集的做法:(感谢大佬们的题解)第一问把所有相同权值的相邻的点用带权并查集合并一下就OK了 第二问,就需要一些骚操作了 我们的目的是把两个不同权值的所有联通块合并,再去看它们共同形成的最大联通块的大小 可以用一个结构体记录两个联通块之间的关系 分别是两个联通块的标号(即这个联通块构成的并查集的祖先节点) 以及这两个联通块的颜色 而为了简化

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

猴子大王Monkey King 左偏树+并查集维护

Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can't avoid quarrelling, and it only happens between two monkeys who does not know e