POJ——1611The Suspects(启发式并查集+邻接表)
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(启发式并查集+邻接表)的更多相关文章
- hdu 1856(hash+启发式并查集)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- 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(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- poj 1182 食物链 (并查集)
http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...
- 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 ...
- POJ1611 The Suspects (并查集)
本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量 学生编号1,2,3,4 ...
随机推荐
- Berkeley DB (VC6.0 编译环境配置)
操作系统:winxp VC环境:VC6.0 必需文件:Berkeley DB安装文件(db-.msi) 下载地址:http://www.oracle.com/technology/software/p ...
- mysql如何让自增id从1开始设置方法
有两种方式 第一种: 如果表中数据没有用.如果直接删除数据,自动增长ID还是不会从1开始的,可以利用“清空数据表”.这样自动增长ID也将会从1开始. 清空表的sql如下 truncate table ...
- 第四次作业:Windows各种基本应用的命令处理方法
删除文件夹命令? rd (remove directory) 如何给文件夹重新命名? ren (rename) 如何在文件夹中建立文件夹? md swift\a 如何用命令查看文本文件的内容? typ ...
- mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错
升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...
- jq封装插件,简单dome
(function($) { $.fn.extend({ bold: function() { this.css({ fontWeight: "bold", color: 'red ...
- webpack开始一个项目的步骤
这几天在学习Vue 用到了webpack打包工具 开始一个项目的时候 需要配置很多项 刚开始写的时候 配置文件总是缺什么再去配置什么 创建项目就用了半个小时 后来觉得应该有个步骤 这样 ...
- 13Shell脚本—编写简单脚本
1. 概述 Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interrctive): 用户每输入一条命令就立即执行. 批处理(Batch): 由用户事先编写好一个完整的 Shell 脚 ...
- OpenCV编译 Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed
OpenCV编译 Make出错 recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed 添 ...
- 【Arduino开发板刷Bootloader01】
其接线方式就是: Programmer(工具开发板) Being programmed(目标开发板) Vcc ...
- apicloud入门学习笔记1:简单介绍
官网地址:https://www.apicloud.com/ 新手开发指南:https://docs.apicloud.com/APICloud/junior-develop-guide 开发语言:H ...