题目在这里

关于SARS病毒传染的问题。在同一个组的学生是接触很近的,后面也会有新的同学的加入。其中有一位同学感染SARS,那么该组的所有同学得了SARS。要计算出有多少位学生感染SARS了。编号为0的同学是得了SARS的。
直接用并查集解决水掉
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
#define MAX 100 struct node
{
int no;//编号
int rank;
int parent;
int total;
}; class DisJoinSet
{
protected:
int n;
node * tree;
public:
DisJoinSet(int n );
~DisJoinSet();
void Init();
void Union(int x,int y);
int Find(int x);
int GetAswer(int x);
}; DisJoinSet ::DisJoinSet(int n)//初始化操作
{
this->n = n;
tree = new node[n];
for(int i = ; i < n; i++)
{
tree[i].no = i;
tree[i].parent = i;
tree[i].total = ;
tree[i].rank = ;
}
}
DisJoinSet::~DisJoinSet()
{
delete[] tree;
} void DisJoinSet :: Init()
{
}
int DisJoinSet::Find(int x)
{
int temp = tree[x].parent;//temp 为x的父亲结点
if( x != tree[x].parent)
{
tree[x].parent = Find(tree[x].parent);//路径压缩
return tree[x].parent;
}
else
{
return x;
}
} void DisJoinSet ::Union(int x,int y)
{
int rootx = Find(x);
int rooty = Find(y);
if(rootx == rooty)
{
return ;
}
else//并查集基本操作
{
if(tree[rootx].rank < tree[rooty].rank)
{
tree[rootx].parent = rooty;
tree[rooty].total += tree[rootx].total;
}
else
{
tree[rooty].parent = rootx;
tree[rootx].total += tree[rooty].total;
if(tree[rootx].rank == tree[rooty].rank)
{
tree[rootx].rank++;
}
}
}
} int DisJoinSet::GetAswer(int x)//返回xtotal
{
int t = Find(x);
return tree[t].total;
} int main()
{
int n;
int m;
while(cin>>n>>m && n!= && m>= )
{
DisJoinSet dis(n);
int per1;
int per2;
for(int i = ; i< m;i++)
{
int num = ;
cin>>num;
cin>>per1;
for(int i = ;i < num; i++)
{
cin>>per2;
dis.Union(per1,per2);
}
}
cout<<dis.GetAswer()<<endl;
}
return ;
}
 

POJ1611(The Suspects)--简单并查集的更多相关文章

  1. poj1611 The Suspects(并查集)

    题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔 ...

  2. POJ1611 The Suspects (并查集)

    本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量  学生编号1,2,3,4 ...

  3. poj 1611 The Suspects(简单并查集)

    题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...

  4. poj1611 The suspects【并查集】

    严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Y ...

  5. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  6. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  7. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  8. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  9. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

随机推荐

  1. AndoridSQLite数据库开发基础教程(8)

    AndoridSQLite数据库开发基础教程(8) 添加索引 索引是一种通过预先排序和对表的一个或多个列构建索引表来优化数据库查找的手段.下面为表添加索引,操作步骤如下: (1)在打开的数据库中,单击 ...

  2. 【转载】 深度学习之卷积神经网络(CNN)详解与代码实现(一)

    原文地址: https://www.cnblogs.com/further-further-further/p/10430073.html ------------------------------ ...

  3. grib2文件格式说明

    GRIB是一种二进制编码的名称,用于加工资料的传输和交换,GRIB编码的分析或预报产品是由一系列八位组构成的连续比特流组成.在GRIB2中编码资料主要分为9段.    0段——指示段八位组序号    ...

  4. ES6深入浅出-7 新版的类(上集)-1.介绍原型

    ES6新出的关键class BE受雇与网景开发了JS 当我们在写一个对象的时候,我们实际上内存的形式表示. obj等于一个空对象,可以直接toString.它为什么可以有toString window ...

  5. MyBatis的学习总结:调用存储过程【参考】

    一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...

  6. Zabbix 3.2.6-Mysql多实例监控-Percona Monitoring Plugins自动发现

    mysql多实例监控实录   系统环境: cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 内核版本: uname -r 3.1 ...

  7. Delphi XE6 使用定时器或者线程解决程序界面无响应问题

    ---恢复内容开始--- 介绍 在手机应用上,我们不应该使用速度慢的代码,当然我们在桌面程序上也应该避免这个,当手机应用长时间没有相应的时候,程序会提示“程序没响应,是否关闭”的提示,这个非常不好,所 ...

  8. DB2中ALTER TABLE的使用

    今天在看DB2存储过程的时候发现了如下语句能够清空表: ... SET EX_SQL='ALTER TABLE TEST_TABLE ACTIVATE NOT LOGGED INITIALLY WIT ...

  9. 19-js策略模式

    var PriceStrategy = function() { var stragtegy = { return30: function(price) { return +price + parse ...

  10. 【Luogu P3258】[JLOI2014]松鼠的新家

    Luogu P3258 题意就是对于一棵树,要求按照给出的顺序对每一个节点进行访问,记录每一个节点被经过的次数:特别地,我们认为只有从一个节点往外走才能被认为是经过一次.(最后一句话非常重要,仔细理解 ...