The Suspects

Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 31100 Accepted: 15110
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


按照自己的思路重新做了下,1A。就是时间慢了点,map+vector的缘故吧,但是比较好理解。
思路有两种
1、将每个团队连成一条线,团队的中某一个人变成了自己团队的祖先(头头),然后进行合并的时候就会不停地找祖先,那么有传染的也被连成了一条线,一旦有一个人的中间祖先或者最后的祖先跟0号有关系,那么传染链会直接被并进去,并到最后就是全部被传染的人。时间16ms
代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
const int N=30010;
int pre[N];
int rank[N];
int num[N];
inline int finder(int x)
{
if(x!=pre[x])
pre[x]=finder(pre[x]);
return pre[x];
}
inline void joint(int a,int b)
{
int fa=finder(a);
int fb=finder(b);
if(fa==fb)
return ;
else if(rank[fa]>rank[fb])
{
pre[fb]=fa;
num[fa]+=num[fb];
}
else
{
pre[fa]=fb;
if(rank[fa]==rank[fb])
rank[fb]++;
num[fb]+=num[fa];
}
}
int main(void)
{
int n,m,i,j,mm,a,b;
while (~scanf("%d%d",&n,&m)&&(n||m))
{
for (i=0; i<=n; i++)
{
pre[i]=i;
num[i]=1;
rank[i]=0;
}
for (i=0; i<m; i++)
{
scanf("%d",&mm);
if(mm!=0)
{
scanf("%d",&a);
for (j=1; j<mm; j++)
{
scanf("%d",&b);
joint(a,b);
}
}
}
int index=finder(0);
printf("%d\n",num[index]);
}
return 0;
}

  

2、用一个map记录某人所呆的团队编号,用邻接表记录一个团队的人的集合,还有一个vis数组表示这个团队是否被访问过。这样就可以通过人来找团队,也可以通过团队来找人。刚接触这题的时候想过这么做,但是好像运行出错了,然后用前面的方法做的,现在用这个方法证明确实可以A而且比较好理解 时间235ms
首先把0这个团队压入一个队列,然后合并掉队列里的所有人,合并的时候看这个队列里的人是否也在其他团队呆过(这里就要用到map了)若有的话把那个团队也压入队列,然后再次进行上述操作,直到队列为空。
代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long LL;
const int N=30010;
int pre[N],ran[N];
vector<int>team[510];
map<int,vector<int> >belong;
int vis[510];
int find(int n)
{
if(n!=pre[n])
return pre[n]=find(pre[n]);
return pre[n];
}
inline void joint(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa!=fb)
{
if(ran[fa]>=ran[fb])
{
ran[fa]+=ran[fb];
pre[fb]=fa;
}
else
{
ran[fb]+=ran[fa];
pre[fa]=fb;
}
}
}
inline void init()
{
for (int i=0; i<N; i++)
{
pre[i]=i;
ran[i]=1;
}
for (int i=0; i<510; i++)
team[i].clear();
MM(vis);
belong.clear();
} int main(void)
{
int m,i,j,one,n,person;
while (~scanf("%d%d",&n,&m)&&(n||m))
{
init();
for (i=0; i<m; i++)
{
scanf("%d",&n);
for (j=0; j<n; j++)
{
scanf("%d",&person);
team[i].push_back(person);
belong[person].push_back(i);
}
} queue<int>Q;
for (i=0; i<belong[0].size(); i++)
Q.push(belong[0][i]); while (!Q.empty())
{
int now=Q.front();
Q.pop();
if(vis[now])
continue;
vis[now]=1;
for (i=0; i<team[now].size(); i++)
{
int v=team[now][i];
joint(0,v);
for (j=0; j<belong[v].size(); j++)
{
if(!vis[belong[v][j]])
Q.push(belong[v][j]);
}
}
} printf("%d\n",ran[0]);
}
return 0;
}

POJ——1611The Suspects(启发式并查集+邻接表)的更多相关文章

  1. hdu 1856(hash+启发式并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  2. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  3. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  4. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  5. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  6. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  7. poj 1182 食物链 (并查集)

    http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...

  8. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...

  9. POJ1611 The Suspects (并查集)

    本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量  学生编号1,2,3,4 ...

随机推荐

  1. 如何处理错误消息Please install the gcc make perl packages

    如何处理这行错误消息? Please install the gcc make perl packages from your distribution. 执行命令行:yum install gcc ...

  2. Logback文档(1)

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL030VCK2svQZU2rRyDR05KMh5

  3. 2018.4.3 Linux环境变量与变量

    环境变量与变量 shell在开始执行时就已经定义了一些和系统的工作环境有关的变量,用户还可以重新定义这些变量. 环境变量可用命令env或set来查询.(DOS环境为set) 环境变量查询与显示 env ...

  4. js 去除数组中的空值以及数组判断是否有重复数据

    1.判断是否有重复数据 function isRepeat(array){ var hash = {}; for(var i in array) { if(array[i]!="" ...

  5. PopClip:你会热爱的文本穿梭机

    http://www.ifanr.com/234952 由于我是一名 Evernote 用户(不是印象笔记),最近发现它所提供的浏览器插件无论是 Web Cliper 还是 Clearly,反应速度都 ...

  6. 空类生成对象输出的结果是什么? toString()输出 覆写Object toString()方法输出的结果是什么

    空类生成对象输出的结果是什么? 输出的是对象在内存空间地址的哈希值 com.swift.P@1db9742 空类生成对象toString()输出的结果是什么? 输出的是对象在内存空间地址的哈希值的字符 ...

  7. label自适应文本大小

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectZero]; NSString *string = @"aa2fkoksdajfis ...

  8. JS数据结构与算法--双向链表

    双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...

  9. windows10蓝屏page fault in nonpaged area

    Windows系统最让人头疼的问题就是蓝屏了,总是出现得那么莫名其妙,而且造成原因也是千奇百怪的.所以,对于电脑蓝屏,系统迷也无法一次性讲清楚.前天,我的电脑就经历过这样的蓝屏page fault i ...

  10. Golang 简单web测试

    // mhoso project main.go package main import ( "log" "net/http" "./controll ...