POJ1611-----并查集简单题

来源:互联网 发布:苏飞万能框架登录淘宝 编辑:程序博客网 时间:2024/06/03 13:49
The Suspects
Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 33374 Accepted: 16189

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 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0

Sample Output

411


带权并查集

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int s[30002], ss[30002], pre[30002];int find(int a){return a == s[a] ? a : find(s[a]);}void merge(int a, int b){int fx = find(a), fy = find(b);if(fx != fy){s[fy] = fx;ss[fx] += ss[fy];//以一个数组来储存可疑人}}int main(){int a, b, num;while(scanf("%d%d", &a, &b) && a + b){for(int i = 0; i < a; i++){s[i] = i;ss[i] = 1;}for(int i = 0; i < b; i++){scanf("%d", &num);int *sss = new int [num];//学到的新东西,开辟动态数组for(int k = 0; k < num; k++){scanf("%d", &sss[k]);if(k != 0){//每列第一个数为根merge(sss[k-1], sss[k]);}}delete(sss);//释放数组内存}printf("%d\n", ss[find(0)]);}return 0;}


知识量少的时候总是想得很复杂
#include<cstdio>#include<cstring>int pre[30010], flag[30010];int find(int a){return a == pre[a] ? a : find(pre[a]);}void merge(int x, int y){int fx = find(x), fy = find(y);if(fx != fy){pre[fx] = fy;flag[fy] += flag[fx];}}int main(){int n, m, x, y, t;while(~scanf("%d%d", &n, &m), n || m){for(int i = 0; i <= n; i++){pre[i] = i;flag[i] = 1;}for(int i = 0; i < m; i++){scanf("%d", &t);scanf("%d", &x);for(int j = 1; j < t; j++){scanf("%d", &y);merge(x, y);}}printf("%d\n", flag[find(0)]);}return 0;}


                                             
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 上呼吸道感染怎么办比较好 小猫咪太调皮怎么办 水晶彩泥弄到衣服上怎么办 进境动植物检疫许可怎么办 跳舞不会听拍子怎么办 税盘丢了注销公司怎么办 认缴资金不到位怎么办 同一单元有凶宅怎么办 有地皮没房产证怎么办 社保资金被侵吞怎么办? 集体计件手脚慢怎么办 发票当月没用完怎么办 非工业用地怎么办环评 商标注册途中英文错误怎么办 孩子的英文不好怎么办 高盛英文不好怎么办 去美国英文不好怎么办 去越南不会英语怎么办 法斗得了毛囊炎怎么办 头发里有毛囊炎怎么办 笔记本画cad慢怎么办 面试打不出问题怎么办 ai撤销多了怎么办 卖钢材没客户怎么办 手抓饼煎的很硬怎么办 微信朋友圈侵权怎么办 tekla工具栏显示不全怎么办 收银员收到假钱怎么办 世联贷款还不上怎么办 皮肤发黄暗沉怎么办 手机默认竖屏怎么办 手机网络没有了怎么办 新生婴儿不睡觉怎么办 婴儿感冒不睡觉怎么办 小孩不肯吃母乳怎么办 新生儿不会吸奶怎么办 婴儿生病不吃奶怎么办 宝宝积食有痰怎么办 新生儿不肯吸奶怎么办 宝宝不吃妈妈奶怎么办 小孩子晚上咳嗽厉害怎么办