POJ 1611 The Suspects(并查集)
来源:互联网 发布:是大腿淘宝店 编辑:程序博客网 时间:2024/06/15 13:30
The Suspects
Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 22458 Accepted: 10925
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.
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.
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
Source
Asia Kaohsiung 2003
并查集,计算与0同组元素的个数
#include <stdio.h>#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <sstream>#include <vector>#include <queue>#include <set>#include <map>#include <ctime>using namespace std;typedef long long LL;int s[500001],rank[500001]; int n,m;int find(int x){ // while(s[x]!=x){ // x = s[x]; // } // return x; if(x!=s[x]) { s[x]=find(s[x]); } return s[x]; } void merge(int x,int y){ x = find(x); y = find(y); // if(y<x){ // s[x] = y; // }else if(y>x){ // s[y] = x; // } if(x == y) return; if(rank[x]>=rank[y]) { s[y]=x; rank[x]=rank[x]+rank[y]; }else{ s[x]=y; rank[y]=rank[y]+rank[x]; } //cout<<rank[x]<<" "<<rank[y]<<endl;} //int sumnum(int num){//int sum = 0;//for(int i = 0;i<n;i++){//if(s[i] == num){////cout<<s[i]<<endl;//sum++;//}//}//return sum;//}int main(){while(scanf("%d%d",&n,&m)!=EOF){for(int i = 0;i<n;i++){s[i] = i;rank[i] = 1;}if(m == 0 && n == 0)break;while(m--){//cout<<m<<endl;int k = 0;scanf("%d",&k);int i = 0;//int name;//scanf("%d",&name);vector<int> v;for(i = 0;i<k;i++){int num;scanf("%d",&num);//merge(name,num);v.push_back(num);}sort(v.begin(),v.end());for(i = 0;i<k;i++){merge((*(v.begin())),v[i]);}//cout<<s[99]<<endl;}int sum = 0;for(int i = 0;i<n;i++){if(s[i] == s[0]){//cout<<i<<endl;sum++;}}printf("%d\n",rank[s[0]]);}return 0;}
0 0
- POJ 1611 The Suspects(并查集)
- poj 1611 The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- POJ 1611 The Suspects 并查集
- POJ--1611--The Suspects【并查集】
- poj 1611-The Suspects:并查集
- POJ--1611 -- The Suspects [并查集]
- poj 1611 - The Suspects(并查集)
- poj 1611The Suspects 并查集
- poj 1611 The Suspects 并查集
- [并查集]POJ 1611 The Suspects
- poj 1611 The Suspects --- 并查集
- POJ 1611 The Suspects (并查集)
- POJ 1611 The Suspects(并查集)
- POJ 1611--The Suspects 并查集
- POJ 1611 The Suspects 并查集
- poj 1611 The Suspects(并查集)
- Prime Distance(筛选法求素数 )
- 杭电 1009(贪心算法)
- NFC标签被用在Android手机帮助解决一些令人担忧在人们的生活
- NFC芯片正在造模由谷歌万事达卡和花旗集团
- NFC技术,它的用途和安全问题
- POJ 1611 The Suspects(并查集)
- NFC标签和NFC读取器整合NFC支付系统以iPad
- NFC技术过硬的谷歌Nexus S的解释
- .aspx页面结构理解
- 浅析TCP字节流与UDP数据报的区别
- 组播MAC地址
- NFC为国防部面临的挑战
- 如何化解云应用中数据泄露之险?
- NFC冠军 - OMNI市酒店拉斯科利纳斯