Poj1611 The Suspects 并查集基础题

电力市场小白必看:手把手教你用Matlab实现市场出清电价计算(附完整代码) 本文为电力市场初学者提供了使用Matlab计算市场出清电价的完整指南。通过Yalmip和Matpower工具包,详细讲解了从环境配置到模型构建、求解及结果分析的整个过程,帮助读者快速掌握节点电价计算的核心技术。 阅读详情

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


并查集的最基本也是最重要,最核心的三个操作:找该元素所在集合的根节点、合并两个集合以及判断两个元素是否属于一个集合。


#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 30000
int n,m;
int parent[maxn+10];
int total[maxn+10];
int getparent(int a)
{
    if(parent[a]!=a){
        parent[a]=getparent(parent[a]);
    }
    return parent[a];
}
void Merge(int a,int b)
{
    int p1=getparent(a);
    int p2=getparent(b);
    if(p1==p2){
        return;
    }
    total[p1]+=total[p2];
    parent[p2]=p1;
}
int main()
{
    while(~scanf("%d%d",&n,&m),n+m){
        for(int i=0;i<n;i++){
            parent[i]=i;
            total[i]=1;
        }
        for(int j=0;j<m;j++){
            int k,s,o;
            scanf("%d",&k);
            scanf("%d",&s);
            for(int i=1;i<k;i++){
                scanf("%d",&o);
                Merge(s,o);
            }
        }
        printf("%d\n",total[getparent(0)]);
    }
    return 0;
}


软件崩溃时Visual Studio中看不到有效的调用堆栈,使用Windbg动态调试去分析定位 当在Visual Studio中遇到软件崩溃且无法查看详细调用堆栈时,可以使用Windbg进行动态调试去分析定位问题。 阅读详情

相关推荐

用74LS160和74LS194搭个复古流水灯:从Multisim仿真到面包板实战(附完整电路图)

本文详细介绍了如何使用74LS160和74LS194芯片搭建复古流水灯电路,从Multisim仿真到面包板实战的全过程。通过解析核心器件特性、提供完整材料清单、仿真验证电路逻辑,以及实际搭建中的布局规划和调试技巧,帮助电子爱好者掌握这一经典数字电路设计项目。

weixin_28736355的博客 391

并查集poj1611(隔离非典)

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

qq_40859951的博客 417

ARM和FPGA之间的通信(硬件原理图篇)

设计该开发板的初衷是为了学习,但也希望今后能扩展其功能,看到大家都在diy示波器,自己也非常冲动的想DIY一把,于是呼选了arm+fpga的方案,fpga主要用来实现数据的高速采样,而ARM则提供数据的显示,以及一些人机交互的功能。那么该方案中最重要的地方就是ARM和FPGA之间的通信如何实现了。在网上查了很多的资料,有说将FPGA作为ARM的一个外设,可以通过DMA的方式来直接访问;也有说把FP

屌yy的博客 5万+

用并查集找出病源

例题是: poj-1611 这是一道关于疫情的题,非常适合出现在这个时候.... 原题是: Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimi...

SAP自学之路 480

DeepSeek-R1模型+Dify实战:5步打造能读PDF的智能客服(附BGE-M3嵌入模型配置)

本文详细介绍了如何利用DeepSeek-R1大模型与Dify平台,结合BGE-M3嵌入模型,构建企业级智能文档问答系统。通过5个步骤,从环境准备到知识库构建,再到智能问答应用开发,帮助企业快速从PDF等文档中提取关键信息,提升工作效率。特别适合处理产品手册、内部规程等专业内容。

convnet3designer的博客 721

同花顺客户端数据采集:Hook注入与内存解密实战

在金融量化领域,客户端数据采集是获取低延迟、高精度行情的关键技术路径。其底层涉及进程通信、内存加密、反调试等典型Windows逆向工程问题。理解API Hook原理与AES-CBC动态密钥派生机制,有助于突破传统HTTP爬虫在国产金融软件中的失效困境。该技术不依赖网络劫持,而聚焦于本地进程内函数拦截与运行时内存解析,具备离线、可控、合规等工程优势,广泛应用于Level2行情、逐笔委托、分笔成交等高频数据场景,是量化基础设施中‘端侧数据主权’的重要实现方式。

weixin_30496999的博客 880

The Suspects(POJ-1611)

点我跳转 POJ - 1611 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 f..

Silence的博客 244

Linux内存压力测试工具【memtester】详且精

对Linux下的压力测试工具memtester有一个比较人性化的概述,以及避坑心得。

weixin_44118923的博客 1万+

poj1611(感染病患者)

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24587 Accepted: 12046 Description Severe acute respiratory syndrome (SARS), an atypical pneumoni...

weixin_30802273的博客 240

香橙派R1 Plus LTS变身无线AP/客户端:手把手教你用RTL8811CU和MT7601U网卡刷OpenWRT

本文详细介绍了如何将香橙派R1 Plus LTS开发板通过RTL8811CU和MT7601U两款USB无线网卡刷入OpenWRT系统,实现无线AP热点发射与客户端接入的双重功能。从硬件选型、驱动安装到双网卡协同配置,提供完整的实战指南,帮助用户快速搭建稳定高效的无线网络解决方案。

weixin_30919429的博客 413

网安工具系列:几个好用的邮件伪造工具

工欲善其事必先利其器,我们来介绍两款用于邮件伪造的测试工具,实现伪造任意用户邮箱。

weixin_54626591的博客 4594

A - The Suspects C语言

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

qq_42754341的博客 466

【人工智能的数学基础】最优传输(Optimal Transport)问题与Wasserstein距离

代表两堆石子,则问题等价于如何移动一堆石子,通过最小的累积移动距离把它堆成另外一堆石子。的不同位置分配的值。为离散型概率分布,则该优化问题可以表示为矩阵形式。表示寻找总运输成本最小的方案。放大后的上确界不会小于原来的上确界,因此不妨取。具有相同的概率分布。是概率分布,因此积分可以写作采样形式。描述了一种可行的运输方案,表示应该从位置。则最优传输问题的目标函数可以用列向量。对于一个线性规划问题,总可以写出其。的所有可能联合分布的集合,下确界。其中矩阵的每一行代表概率分布。表示寻找使得目标函数最大的。

AI天才研究院 6330

poj 1611 The Suspects 【并查集基础题】

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 26901   Accepted: 13142 Description Severe acute respiratory syndrome (SARS), an atypical pneumo

世界很大 1399

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

POJ 1611 The Suspects 并查集裸题

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 18766 Accepted: 9075 Description Severe acute respiratory syndrome (SARS), an atypical pneum

churehill123的专栏 691

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

POJ 1611:The Suspects【并查集】【水题】

  POJ 1611:The Suspects   Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁。为了减少传播给别人的机会, 最好的策略是隔离可能的患者。 在Not-Spreading-Your-Sickness大学( NSYSU), 有许多学生团体。同一组的学生经常彼此相通,...

不甘 293

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

POJ 1611--The Suspects【并查集水题】

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 26908   Accepted: 13149 Description Severe acute respiratory syndrome (SARS), an atypical pneu

_阿阿阿阿欢 2281

poj 1611 The Suspects 并查集水题

The Suspects Time Limit: 1000MS   Memory Limit: 20000K       Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognize

yu_ming_cong的专栏 268

poj-1611 The Suspects(并查集简单题)

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 67614 Accepted: 31979 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimi

weixin_47750287的博客 224

POJ 1611 The Suspects 并查集简单题

POJ 1611 The Suspects 并查集简单题

淘萄桃的博客 545

The Suspects POJ - 1611 并查集水题

很简单的一道并查集。/** 并查集的第二个题 哈哈 一遍过 其实很水 怕怕的过了 又有0ms的 气!! 这个题呢,就是说一个代号为0的成员,他携带病毒,如果说有人和0是一个队,那么整个团队就是带病毒的。 如果说和0一个团队的人同时也是另外一个团队的成员,那么另外一个团队的所有人都是感染病毒的。 最后要你求解的就是携带病毒的人的个数; 很水,很水,小小的用一下并查集就过去了。 就是有一个小细节

布呗之路 284

POJ1611The Suspects (并查集大水题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 41076   Accepted: 19830 Description Severe acute respiratory syndrome (SARS), an atypical pneumo

T__TSZ的博客 269

POJ - 1611The Suspects-并查集

POJ - 1611 The Suspects Time Limit: 1000MS   Memory Limit: 20000KB   64bit IO Format: %I64d & %I64u Submit Status Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3

qq_18661257的专栏 1014

并查集入门题-以POJ1611 The Suspects为例

题目: 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 be...

pkueecscss的博客 191

poj1611(The Suspects)-并查集基础题

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21696   Accepted: 10513 Description Severe acute respiratory syndrome (SARS), an atypical pneumo

冲鸭,胖哒哒 的专栏 504

poj 1611:The Suspects(并查集,经典题)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 Description Severe acute respiratory syndrome (SARS), an atypical pneumoni...

weixin_33758863的博客 82
上一篇: Poj3468 A Simple Problem with Integers 线段树、区间更新
下一篇: Poj1308 Is It A Tree? 并查集
Megumin
博客等级 码龄10年 3粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值