并查集学习过之后做了几道相关联系,这里贴出1611
The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 20494   Accepted: 9940

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 题中要计算感染患者的人数,因此想到要把感染患者放到同一个集合中,即出现一个与0号患者同组的同学,则把这个同学放入到与0相同的集合中,把与0不是同组的同学放到另一个集合中,因此想到用并查集解这道题。0节点不一定是这个集合的根节点,所以最后要找到0节点的根节点,然后计算根节点所包含的子节点的数目。
代码:
#include <iostream>
using namespace std; int father[], num[]; void Make_set(int x)
{
father[x] = x;
//没有用到秩,而是设置每个集合中所包含节点的数目
num[x] = ;
} int Find_set(int x)
{
if(x != father[x])
father[x] = Find_set(father[x]);
return father[x];
} void Union(int x, int y)
{
int GrandX = Find_set(x);
int GrandY = Find_set(y); if(GrandX == GrandY)
return;
if(num[GrandX] < num[GrandY])
{
father[GrandX] = GrandY;
num[GrandY] += num[GrandX];
}else
{
father[GrandY] = GrandX;
num[GrandX] += num[GrandY];
}
} int main()
{
int n, m;
while(cin >> n, cin >> m) //cin >> n, cin >> m
{
if((m==) && (n==))
break; //为每个节点创建一个新的集合
for(int i = ; i < n; i++)
Make_set(i); for(int i = ; i < m; i++)
{
int count, first;
cin >> count;
cin >> first;
for (int j = ; j < count; j++)
{
int temp;
cin>> temp;
Union(temp, first);
}
} cout << num[Find_set()] << endl;
}
return ;
}

poj1611 解题报告的更多相关文章

  1. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  2. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  3. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  4. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  5. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

  6. 习题: codevs 2492 上帝造题的七分钟2 解题报告

    这道题是受到大犇MagHSK的启发我才得以想出来的,蒟蒻觉得自己的代码跟MagHSK大犇的代码完全比不上,所以这里蒟蒻就套用了MagHSK大犇的代码(大家可以关注下我的博客,友情链接就是大犇MagHS ...

  7. 习题:codevs 1519 过路费 解题报告

    今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...

  8. NOIP2016提高组解题报告

    NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. LINUX 笔记-tee命令

    作用:把输出的一个副本输送到标准输出,另一个副本拷贝到相应的文件中 格式:tee filename 例:who | tee who.out 使用who命令,结果输出到屏幕上,同时保存在who.out文 ...

  2. 基于webuploader.js的单图片上传封装

    HTML代码 <div class="manageImgUp">点击图片上传</div> <div class="manageImgUpLc ...

  3. Apache常用配置

    Apache配置文件:conf/httpd.conf.(注意:表示路径时使用‘/’而不使用‘\’,注释使用‘#’) 1. ServerRoot:服务器根目录,也就是Apache的安装目录,其他的目录配 ...

  4. windows平台安装并使用MongoDB

    下载并安装MongoDB,我的安装路径:D:\Program_Files\MongoDB 创建数据库目录,我的目录:D:\mongodb\data\db 命令行下运行MongoDB服务器: 在命令行窗 ...

  5. 聊聊Java中的反射(一)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 反射reflection主要为了动态操作Java代码,它的主要功能体现在Java提供的refl ...

  6. Azure 基础 : 使用 template 简化部署

    笔者在前文中介绍了如何使用 PowerShell 脚本在 Azure 上创建虚拟主机.正如你所看到的,整个创建过程还是有点繁琐的,因为我们需要使用 PowerShell 脚本创建并关联所有相关的组件. ...

  7. Shell中bash的特性小结

    Shell: 用户与操作系统之间完成交互式操作的一个接口程序,为用户提供简化了的操作:上世纪的70年代中期在贝尔实验室,Bourne位Unix开发了一个shell程序Bourne Shell,简称sh ...

  8. 27.Linux-DM9000C网卡移植(详解)

    上一节 我们学习了:   网卡驱动介绍以及制作虚拟网卡驱动: http://www.cnblogs.com/lifexy/p/7763352.html 接下来本节,学习网卡芯片DM9000C,如何编写 ...

  9. 关于C#开发中那些编码问题

    最近一直在搞各种编码问题,略有心得,与大家分享一番. System.Text提供了Encoding的抽象类,这个类提供字符串编码的方法.常用的编码方式主要有ASCII,Unicode,UTF8(Uni ...

  10. Leetcode题解(十五)

    42.Trapping Rain Water 题目 这道题目参考http://www.cnblogs.com/felixfang/p/3713197.html 观察下就可以发现被水填满后的形状是先升后 ...