The Suspects (并查集问题模板)

简介: The Suspects (并查集问题模板)

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)。这些学生由于兴趣爱好等原因组成了m个群体。


由于非典(SARS)流行,该大学的学生会需要排除可能的非典患者。


由于非典传染性强,学生会的成员假定:如果一个群体中有一个人是非典患者,那么这个群体中的所有人都是非典患者。


现在已知编号为0的学生为非典患者。请你找出这些学生中非典患者的人数。


输入输出格式



输入格式:


输入数据由多组数据组成。


每组数据包括m+1行:

第1行有两个由空格隔开的非负整数n和m,其意义如题目所述。

第2∼m+1行表示每个群体的人员信息,每行的第一个数字k表示该群体的人数,其后有k个用空格隔开的非负整数,表示这个群体的各个成员的编号。

当n=m=0时,表示输入结束,不需要处理之后的数据。


输出格式:


对于每组输入数据,输出一个整数,表示这组数据中非典患者的数量。每组数据的输出以换行符结尾。


题目分析;大概就是看和0的一个组的人数,是不是就可以想到树根问题,我们把树根一样的并在一起(并查集),然后遍历和0对比看是不是有相同树根。


又是一道模板题,听懂了记得给个赞鼓励一下,码字不易,用爱发电。

上ac代码。


f58230e9f063709cf3167704f4efdf14.gif


有事你就q我;QQ2917366383


学习算法

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int p[40000];
int find(int x)//树根 
{
  if(p[x]!=x)return find(p[x]); return p[x];
}
int main()
{
  int n,m;
  while(cin>>n>>m)
  {   int ans=1;//开始就有一个学生有疫病
    memset(p,0,sizeof(p));//初始化 
    if(!n&!m)break;
    for(int i=0;i<n;i++)p[i]=i;//自己是自己的父亲节点 
    for(int i=1;i<=m;i++)
    {
      int num,x,y;  
        cin>>num>>x;//先读入群体里人的个数和第一个人的编号,这样方便之后合并 
      for(int i=2;i<=num;i++)
      {
        cin>>y; 
        p[find(y)]=find(x);//额,这步解释一下就是和第一个成员 x绑成为一个组; 
      }   
    }
    for(int i=1;i<n;i++)//遍历每个人 
      if(find(0)==find(i)) ans++;
          cout<<ans<<endl;
   } 
}
相关文章
|
3天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
13天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
509 203
|
5天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
731 157
|
11天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。
|
5天前
|
数据采集 消息中间件 人工智能
跨系统数据搬运的全方位解析,包括定义、痛点、技术、方法及智能体解决方案
跨系统数据搬运打通企业数据孤岛,实现CRM、ERP等系统高效互通。伴随数字化转型,全球市场规模超150亿美元,中国年增速达30%。本文详解其定义、痛点、技术原理、主流方法及智能体新范式,结合实在Agent等案例,揭示从数据割裂到智能流通的实践路径,助力企业降本增效,释放数据价值。
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
673 46

热门文章

最新文章