Poj 1611 The Suspects

简介:

Poj 1611 的传送门

              ***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 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和m, 其中n是学生的数量, m是团体的数量。0 < n <= 30000,0 <= m <= 500。
每个学生编号是一个0到n-1之间的整数,一开始只有0号学生被视为可能的患者。
紧随其后的是团体的成员列表,每组一行。
每一行有一个整数k,代表成员数量。之后,有k个整数代表这个群体的学生。一行中的所有整数由至少一个空格隔开。
n = m = 0表示输入结束,不需要处理。
然后输出可能感染的人的个数

解题思路:并查集:

具体详见代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 30010;

int fa[maxn];//父亲节点
int rank[maxn];//rank[x]就是表示x的高度上的一个上界
int sum[maxn];//sum[]存储该集合中元素个数,并在集合合并时更新sum[]即可
void Init(int x)//创建单元集
{
    fa[x]=x;
    sum[x]=1;
    rank[x]=0;
    return ;
}

int Find(int x)//递归找父亲,路径压缩
{
    if(x != fa[x])
        fa[x]=Find(fa[x]);
    return fa[x];
}

void Union(int a, int b)//让rank比较高的作为父亲节点
{
    a=Find(a);
    b=Find(b);
    if(a == b)
        return ;
    if(rank[a] > rank[b])
    {
        fa[b]=a;
        sum[a]+=sum[b];
    }
    else
    {
        fa[a]=b;
        if(rank[a] == rank[b])
            rank[b]++;
        sum[b]+=sum[a];
    }
}
int main()
{
    int m,n,k,a,b;
    while(cin>>m>>n)
    {
        if(!m && !n)
            break;
        if(n == 0)
        {
            puts("1");
            continue;
        }
        for(int i=0; i<m; i++)
            Init(i);
        while(n--)
        {
            cin>>k>>a;
            k--;
            while(k--)
            {
                cin>>b;
                Union(a, b);
            }
        }
        cout<<sum[Find(0)]<<endl;
    }
    return 0;
}
目录
相关文章
|
XML 安全 定位技术
无人船水下地形测量作业流程
无人船水下地形测量作业流程
912 0
lodash判断对象的属性是否存在
lodash判断对象的属性是否存在
757 0
|
JSON IDE 机器人
超简单:mac导出微信聊天记录(附上粉丝群全部聊天记录)
今天再给大家讲解一下如何直导出mac版本微信的聊天记录,当然如果你没有mac,那可以直接关闭这篇文章了。
10975 0
超简单:mac导出微信聊天记录(附上粉丝群全部聊天记录)
|
Web App开发 Python
【Chromedriver】下载、安装及配置
简介:【Chromedriver】下载、安装及配置
11194 60
【Chromedriver】下载、安装及配置
|
人工智能 安全 数据挖掘
销售易、红圈、励销云:CRM系统的新纪元
销售易、红圈和励销云是国内知名的CRM解决方案,各有独特优势。销售易功能全面,适合中大型企业;红圈定制化能力强,适用于多行业大中型企业;励销云灵活高效,是中小企业的理想选择。本文从功能、用户体验、价格、市场评价及适用场景等方面对比总结这三款CRM系统,帮助企业根据自身需求做出最佳选择。
|
存储 网络协议 Unix
docker的底层原理一:客户端-服务器架构
本文详细解释了Docker的客户端-服务器架构,包括常驻后台的Docker守护进程、通过命令行接口发送请求的Docker客户端、以及它们之间通过Unix socket或网络接口进行的通信。
301 0
|
机器学习/深度学习 算法 安全
第3讲笔记:详解隐私计算框架及技术要点
隐语架构是一个分层设计,支持不同技术路线,具有高内聚、低耦合特性,允许各层次的技术人员发挥所长。它包括产品层、算法层和计算层。产品层有SecretPad和SecretNote,提供轻量化安装和全栈产品,支持MPC、TEE等。算法层涉及PSI、PIR协议和SCQL,用于安全数据分析,屏蔽底层复杂性。计算层包含RayFed分布式调度框架和SPU密态计算核心,提供高性能密态计算能力和机器学习算法支持。
618 1
|
数据采集 设计模式 缓存
LabVIEW生产者消费者架构
LabVIEW生产者消费者架构
355 2
|
算法 安全 区块链
区块链如何实现交易匿名性
**区块链匿名性摘要:** - 匿名性源于公钥/私钥系统,公钥作地址,私钥验证交易,不透露身份信息。 - Coin Mixing 和 CoinJoining 混合交易,使资金流向难以追踪。 - 匿名币如 Monero、Zcash 使用零知识证明和环签名技术增强匿名。 - 隐身地址和一次性地址增加隐私,公私钥交换确保安全交易而不暴露身份。 - 多层次加密与协议结合,保障区块链交易隐私。
|
存储 资源调度 JavaScript
Vite是什么?怎样使用Vite创建Vue3项目?
Vite是什么?怎样使用Vite创建Vue3项目?
1036 0