[并查集] POJ 1611 The Suspects
Time Limit: 1000MS | Memory Limit: 20000K | |
Total Submissions: 35206 | Accepted: 17097 |
Description
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
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<stdio.h>
- #include<string.h>
- int father[30050],n;
- void init()
- {
- int i;
- memset(father,-1,sizeof(father));
- for(i=0;i<n;++i) father[i]=i;
- }
- int find(int x)
- {
- if(x==father[x]) return (x);
- else father[x]=find(father[x]);
- return (father[x]);
- }
- void merge(int x,int y)
- {
- if(find(x)!=find(y)) father[find(x)]=find(y);
- }
- int main()
- {
- int m,i,qnum,x,pre,ans;
- while(~scanf("%d%d",&n,&m))
- {
- if(n==0&&m==0) break;
- init();
- while(m--)
- {
- scanf("%d",&qnum);
- scanf("%d",&pre);
- for(i=1;i<qnum;++i)
- {
- scanf("%d",&x);
- merge(x,pre);
- }
- }
- ans=0;
- for(i=0;i<n;++i) if(find(i)==find(0)) ++ans;
- printf("%d\n",ans);
- }
- return 0;
- }
[并查集] POJ 1611 The Suspects的更多相关文章
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 :The Suspects经典的并查集题目
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
随机推荐
- command 'x86_64-linux-gnu-gcc' failed with exit status 1错误及解决方案
Ubuntu16.04安装Scrapy(pip install Scrapy)时提示错误如下: Failed building wheel for cryptography Running setup ...
- 20161106PM-接口
使用网址:http://apistore.baidu.com apikey:1f1d014aee0adeddbe33a6e1f55f7925 Composer GET http://apis.baid ...
- FMDB
一.FMDB简介 1.FMDB简介 iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较繁琐.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB. ...
- python 最大公约数
求解两个整数(不能是负数)的最大公约数(要求两数不能同时为0)当两数都是0时,最大公约数为0方式一:穷举法 def GCU(m, n): if not m: return n elif not n: ...
- test homework2 ~ faulty program
Program1:
- 【Java】异常处理_学习笔记
异常: 1.格式1: try { //业务代码 } catch(Exception e) { //异常处理代码 } 说明: a. 异常抛出:执行try里的代码,系统会自动生成一个异常对象,该对象会 ...
- HTML基本标签
h1-h6:标题标签.(从大到小) p:段落标签. img:图片标签:属性src:图片的相对路径:alt:图片加载失败的提示语言. a:超链接标签:属性:href:地址链接:target:网页打开的默 ...
- js URL中文传参乱码
js: var searchVal = encodeURIComponent($.trim($('#js_search_val').val()));//搜索的值 encodeURIComponent( ...
- welcome-file-list设置问题之css,js文件无法加载
web.xml里的welcome-file-list里设置默认访问页面为/html/index.html 但是在访问时,页面CSS都没加载. 正常输入网址却没问题.用/html/index.jsp也没 ...
- Load Runner录制C/S客户端
1. 打开应用程序 2. 点击如下菜单 弹出窗口如下 3. 点击New,弹出窗口如下,选择Web(HTTP/HTML) 4. 点击Create,弹出窗口 5. 点击OK, ...