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 ...
随机推荐
- 如何将SAP Multi Target应用部署到SAP云平台的Cloud Foundry环境去
SHINA是SAP HANA Interactive Education的缩写,是一个demo应用,用于演示如何开发SAP HANA原生应用. 这个应用包含了sample数据以及HANA数据库表,vi ...
- JAVA图形界面常用知识点总会《代码分析》
1. package CLASS16.bin.com.GridLayout; import javax.swing.ImageIcon;import javax.swing.JFrame;import ...
- GWTDesigner_v5.1.0破解码
GWTDesigner_v5.1.0_win32_x86.exe破解码,双击运行keygeno.jar,然后输入用户名.网卡MAC,然后单击Generate,将生成的文件放在C:\Documents ...
- JavaScript -- 操作符和逻辑运算
算数操作符 + : 加 - : 减 * : 乘 / : 除 %:取余 递增和递减 1.递增 ++a与a++都是对a进行递增的操作 区别 ++a先返回递增之后的a的值 a++先返回a的原值,再返回递增之 ...
- mysql grant 用户权限说明
mysql grant 用户权限说明 Mysql 有多个个权限?经常记不住,今天总结一下,看后都能牢牢的记在心里啦!! 很明显总共28个权限:下面是具体的权限介绍:转载的,记录一下: 一.权限表 my ...
- 01_3_查询指定id的单个对象
01_3_查询指定id的单个对象 1. 映射文件配置如下信息 <select id="selectStudentById" resultClass="Student ...
- comboBox 下拉宽度自适应
///适用combobox绑定datatable private void comboBox_DataSourceChanged(object sender, EventArgs e) { Combo ...
- C# 使用Epplus导出Excel [5]:样式
C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...
- bp神经网络原理
bp(back propagation)修改每层神经网络向下一层传播的权值,来减少输出层的实际值和理论值的误差 其实就是训练权值嘛 训练方法为梯度下降法 其实就是高等数学中的梯度,将所有的权值看成自变 ...
- C#_接口基础学习
参考:https://www.cnblogs.com/hamburger/p/4681681.html