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

提供两个模板:

模板一:

 int ufs[MAXN];    //并查集

 void Init(int n)    //初始化
{
int i;
for(i=;i<n;i++){
ufs[i] = i;
}
} int GetRoot(int a) //获得a的根节点。路径压缩
{
if(ufs[a]!=a){ //没找到根节点
ufs[a] = GetRoot(ufs[a]);
}
return ufs[a];
} void Merge(int a,int b) //合并a和b的集合
{
ufs[GetRoot(b)] = GetRoot(a);
} bool Query(int a,int b) //查询a和b是否在同一集合
{
return GetRoot(a)==GetRoot(b);
}

模板二:

 int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的子节点
}

题意:
  有n个学生属于m个团体,(0<n<=30000,0<=m<=500)一个学生可以属于多个团体。一个学生疑似患病,则他属于整个团体都疑似患病,已知0号学生疑似患病,求一共多少个学生患病。
思路:
  很经典的并查集的题目,找一个pre[]数组记录存储每一个以当前下标为根节点的集合的个体数目,最后输出0号的根节点对应的sum值,就是0号学生所在团体的人数。
  代码:

 #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
using namespace std;
int pre[];
int find(int x){//查找x父节点
int r=x; //委托r去找父节点
while(pre[r]!=r) //如果r的上级不是r自己(也就是说找到的节点他不是父节点 )
r=pre[r]; // r 接着找他的上级,直到找到父节点 为止
return r;//父节点驾到~~~
}
void join(int x,int y){ //我想让x节点和节点连成一条线
int fx=find(x),fy=find(y);//寻找x,y的父节点
if(fx!=fy)//x和y的父节点显然不是同一个
pre[fx]=fy;//让x的父节点成为y的字节点
}
int main(){
std::ios::sync_with_stdio(false);
int n,m;
while(cin>>n>>m){
memset(pre,,sizeof(pre));
if(n==&&m==)break;
for(int i=;i<n;i++) pre[i] = i;
while(m--){
int t, a,b;
cin>>t>>a;
for(int i=;i<t;i++){
cin>>b;
join(a,b);
a=b;
}
}
int sum=;
for(int i=;i<n;i++){
if(find(i)==find())
sum++;
}
cout<<sum+<<endl;
}
return ;
}

最新文章

  1. 为什么可以说Java语言是准动态语言?
  2. codeforces problem 140E New Year Garland
  3. AWS-CDH5.5安装-安装
  4. hibernate----(Hql)另一种查询---利用Criteria类
  5. canvas对象arcTo函数的使用-遁地龙卷风
  6. 《利用python进行数据分析》读书笔记--第十一章 金融和经济数据应用(一)
  7. Cordova or Xamarin 用.net开发IOS和Android程序
  8. PLSQL_性能优化系列17_Oracle Merge Into和Update更新效率
  9. ToolBar存档
  10. 电容值E系列标称方法
  11. 使用phpize建立php扩展(Cannot find config.m4)(转)
  12. node.js操作mongoDB数据库
  13. Node.js模块os
  14. 关于MyEclipse6.5 总是弹出 Update Progress(xx-xx-xx时间)的问题
  15. 织梦dedecms后台发布文章提示“标题不能为空”
  16. java 随记
  17. 关于gcc的一点小人性化提示
  18. wxpython 支持python语法高亮的自定义文本框控件的代码
  19. 设置python环境变量
  20. ggplot2画histogram(坐标轴刻度值字体大小,坐标轴标题字体大小,柱形宽度,大标题字体大小、居中)

热门文章

  1. dva-loading 实践用法
  2. 创建基于ASP.NET core 3.1 的RazorPagesMovie项目(三)-已搭建基架的Razor页面解释和更新
  3. Win10安装5 —— 系统安装步骤
  4. 为什么要使用NoSQL数据库
  5. 2020牛客寒假算法基础集训营5 G.街机争霸 (bfs)
  6. liunx 中设置zookeeper 自启动(service zookeeper does not support chkconfig)
  7. git的安装方法
  8. CodeForces 1141B
  9. 切换目录命令 - cd
  10. 番外:克隆本地PDB中其他参数和子句的说明