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

/*
题意:总共有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[];
int num[];
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 ;
}
return ;
}
void init(){
for(int i=;i<=n;i++){
bin[i]=i;
num[i]=;
}
}
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=;i<k;i++){
scanf("%d",&a);
if(i){
bindx(a,last);
}
last=a;
}
}
printf("%d\n",num[findx()]);
}
return ;
}

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

  1. HDU-6109 数据分割 并查集(维护根节点)

    题目链接:https://cn.vjudge.net/problem/HDU-6109 题意 给出多组等式不等式 对于每一个式子,首先判断是否不可能 如果不可能,记录本组正确式子的个数,然后进入下一组 ...

  2. hihoCoder #1291 : Building in Sandbox 逆向处理+并查集维护

    /** 题目:#1291 : Building in Sandbox 链接:https://hihocoder.com/problemset/problem/1291 题意:就是一个三维的空间里,按照 ...

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

    题面 传送门 思路 真是一道神奇的题目呢 题目本身可以转化为二分图匹配问题,要求右半部分选择的点的最大编号最小的一组完美匹配 注意到这里左边半部分有一个性质:每个点恰好连出两条边到右半部分 那么我们可 ...

  4. 《程序员代码面试指南》第三章 二叉树问题 Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题

    题目待续.... Tarjan算法与并查集解决二叉树节点间最近公共祖先的批量查询问题 java代码

  5. Codeforces325 D【并查集维护连通性】

    参考:大牛blog 思路: 因为是环,所以可以复制一下图,先判断一下和他是不是和与他相邻的8个之一的一个障碍使得构成了一个环,环就是一个连通,用并查集维护即可: 如果没有就ans++,然后并把这个点加 ...

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

    题目大意:给你一个N*N的棋盘,棋盘上每个点都有一个权值 第一问求一个权值形成的最大联通块中点的数量 第一问求两个权值共同形成的最大联通块中点的数量 提供一种并查集的做法:(感谢大佬们的题解)第一问把 ...

  7. 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)

    题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树.  开始和队友 ...

  8. 【uva12232/hdu3461】带权并查集维护异或值

    题意: 对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题: I P V :告诉你a[P] = V I P Q V:告诉你a[P] XOR a ...

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

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

随机推荐

  1. FS BPM 业余研发(用户详细操作手册--单人串行/并行)之 深圳分公司技术部请假审批流程

    1.FS BPM 简介 BPM软件中BPM是英文字母缩写,大致有二个意思.第一.Business Process Management,即业务流程管理,是一套达成企业各种业 务环节整合的全面管理模式. ...

  2. 常用Linux命令、包括vi 、svn

    /etc/init.d/network restart//===========================================更新脚本cd /www/scripts更新站点./sta ...

  3. JavaScript遍历对象-总结一

    原生JavaScript 遍历 1.for 循环遍历 let array1 = ['a','b','c']; for (let i = 0;i < array1.length;i++){ con ...

  4. D - DZY Loves Hash CodeForces - 447A

    DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...

  5. vue练手小项目--眼镜在线试戴

    最近看到了一个眼镜在线试戴小项目使用纯js手写的,本人刚学习vue.js没多久,便试试用vue做做看了,还没完善. 其中包括初始图片加载,使用keywords查找,父子组件之间传递信息,子组件之间传递 ...

  6. C语言编程练习(一)

    问题一: 问题描述:输入n个数,n<=100,找到其中最小的数和最大的数 输入样例: 4                      1 2 3 4 输出样例:14 #include " ...

  7. CentOS 7搭建LAMP环境(一)

    CentOS是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成.由于出自同样的源代码,因此有些要求高度稳定性的服务器以CentO ...

  8. uva11538

    解题思路: 1. 计数问题, 有三种相对摆放方式: 水平, 竖直, 对角线. 根据加法原理即可, 并且没有交集. 水平和竖直是一样的, 只要n*m矩形旋转90度. 所以结果是: n*m*(m-1)+n ...

  9. .Neter玩转Linux系列之五:crontab使用详解和Linux的进程管理以及网络状态监控

    一.crontab使用详解 概述:任务调度:是指系统在某个时间执行的特定的命令或程序. 任务调度分类: (1)系统工作:有些重要的工作必须周而 复始地执行. (2)个别用户工作:个别用户可能希望执 行 ...

  10. DAO与DTO

    DAO叫数据访问对象(data access object) DTO是数据传输对象(data transfer object) DAO通常是将非对象数据(如关系数据库中的数据)以对象的方式操纵.(即一 ...