POJ1611

The Suspects

Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 23002   Accepted: 11171

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

提交一直出现Wrong Answer,主要代码出错在union_set方法。

之前

void union_set(int x,int y){
	x = find_set(x);
	y = find_set(y);
	if(rank[x] >  rank[y]){
		pa[y] = x;
		count[x] += count[y];
	}
	else{
		pa[x] = y;
		if(rank[x] == rank[y]){
			rank[y]++;
		}
		count[y] += count[x];
	}

}

修改为

void union_set(int x,int y){
	x = find_set(x);
	y = find_set(y);
	if(x==y) return;
	if(rank[x] > rank[y])
	{
		pa[y] = x;
		count[x] += count[y];
		rank[x]++;
	}
	else
	{
		pa[x] = y;
		rank[y]++;
		count[y] += count[x];
	}
}

通过了

#include <cstdio>

const int MAXN = 30001;
int pa[MAXN];
int rank[MAXN];
int count[MAXN];

void make_set(int x){
	pa[x] = x;
	rank[x] = 0;
	count[x] = 1;
}

int find_set(int x){
	if(x != pa[x]){
		pa[x] = find_set(pa[x]);
	}
	return pa[x];
}

void union_set(int x,int y){
	x = find_set(x);
	y = find_set(y);
	if(x==y) return;
	if(rank[x] > rank[y])
	{
		pa[y] = x;
		count[x] += count[y];
		rank[x]++;
	}
	else
	{
		pa[x] = y;
		rank[y]++;
		count[y] += count[x];
	}

}

int main(void){
	int n,m;
	while(1){
		int i;
		scanf("%d%d",&n,&m);
		if(0==n && 0==m){
			break;
		}
		for(i=0;i<n;i++){
			make_set(i);
		}
		for(i=0;i<m;i++){
			int k,first,j;
			scanf("%d%d",&k,&first);
			for(j=1;j<k;j++){
				int next;
				scanf("%d",&next);
				union_set(first,next);
			}
		}
		int p = find_set(0);
		printf("%d\n",count[p]);
	}
	return 0;
}
时间: 2024-08-05 04:47:40

POJ1611的相关文章

并查集的基本运用 POJ1611

用并查集的情况是在比如 A和B联通  B和C联通那么 ABC联通 这类情况下算某个元素所在集合的元素个数.. 并查集数据结构用数组就行,数组的下标表示相应的一个主体,对应的值表示它的父节点的索引,指向父节点.根节点的值特殊一下就行: 原理:先把所有结点(元素)看成独立的集合,然后按给的条件来合并,合并的过程就是最重要的,结合已知条件,因为并查集是用于A和B联通  B和C联通那么 ABC联通 这类情况下算某个元素所在集合的元素个数   所以合并过程是把2个元素所在集合合并,实际情况就是比如已知AC

poj2236和poj1611并查集问题

POJ 2236 问在计算机坏了,修复若干,问检测两台是否能连通 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; const int N = 1005; struct Point { int x,y; }; Point p[N]; int repaired[N]; int pre[N],rank[N]; int dist(Point A,Point B) {

并查集 poj1611&amp;poj2492

poj1611 简单题 代码中id记录父节点,sz记录子树规模.一个集合为一棵树. #include <iostream> #include <cstdio> using namespace std; int id[300005]; int sz[300005]; void add(int a, int b) { int i, j; for (i = a; i != id[i]; i = id[i]); for (j = b; j != id[b]; j = id[j]); if

并查集初学(3)无间道之并查集 &amp;&amp; POJ2542 &amp;&amp; POJ1611

1.hihocoder上面讲的一道题 无间道之并查集 水题,精髓在于使用map容器进行打标签 #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; const int maxn=10005; int p[maxn]; map&l

poj1611 The Suspects(并查集)

题目链接 http://poj.org/problem?id=1611 题意 有n个学生,编号0~n-1,m个社团,每个社团有k个学生,如果社团里有1个学生是SARS的疑似患者,则该社团所有人都要被隔离.起初学生0是疑似患者,求要隔离多少人. 思路 使用并查集求解. 代码 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int

POJ--1611 The Suspects

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-Spre

The Suspects POJ1611

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 36417   Accepted: 17681 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

poj1611 The Suspects

题意: n个学生分属m个团体,(0 < n <= 30000 ,0 <= m <= 500) 一个学生可以属于多个团体.一个学生疑似患病,则它所属的整个团体都疑似患病.已知0号学生疑似患病,以及每个团体都由哪些学生构成,求一共多少个学生疑似患病. 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 思路: 并查集. 实现: 1

poj1611 并查集 (路径不压缩)

http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社团,他所在的社团照样全部感染,求感染的人数. 解题思路: 并查集的变种,实质就是求0所在的强连通图的结点数目. 这道题纠结在数据的输入上,他只是告诉你哪些学生是同一个社团的.这就需要处理一下,我的想法是:如果这个社团有num个孩子,new出一个大小为num的数组,第一个孩子不处理,从第二个孩子起,和

暑假集训(2)第二弹 ----- The Suspects(POJ1611)

B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Your-Sic