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 ...
随机推荐
- mac安装webpack失败
最近开始接触构建工具webpack,公司电脑是 windows,而我自己的呢是mac.本来以为在自己电脑安装很简单,但是出了点问题,所以写出来分享下. 这里用npm的方式安装,首先你要安装node.j ...
- vue 文件流下载xlsx 功能实现
downLoadFile (url, name) { this.xhr = new XMLHttpRequest() this.xhr.open('GET', url, true) this.xhr. ...
- 广播监听USB插入与拔出
package com.joy.usbbroadcastreceiver; import android.content.BroadcastReceiver; import android.conte ...
- CPP-基础:c++读取ini文件
配置文件格式是[JP]K=2EC156673E 2F4240 5595F6 char str[50];GetPrivateProfileString("JP", "K&q ...
- React组件间通信
众所周知,ReactJS组件与组件之间的通信是一个难点.在React实际开发中,父子组件之间的传值是比较常见的,刚入门的小伙伴很容易被组件之间的通信绕懵. 今天花了点时间总结了一下React父子组件之 ...
- HTML5语义
语义通俗化为意义,也就是语义化的元素等于意义化的元素,看到这个元素的名称,就知道这个元素的意义,是拿来做什么用的,这就是HTML5的一个新特性,一个具有语义化的元素能够清楚的把元素的意义告诉浏览器和开 ...
- intellij idea 下载安装破解教程
官网下载:http://www.jetbrains.com/idea/download/#section=windows 选择 Ultimate 版本下载 下载完成后,打开安装 在安装路径位置,可以 ...
- Jquery之 Ajax /json
前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...
- Python模块(二)(序列化)
1. namedtuple 命名元组->类似创建了一个类 from collections import namedtuple p = namedtuple("Point", ...
- leetcode-18-remove
283. Move Zeroes 解题思路: 从nums[0]开始,如果是零就和它后面的第一个非零数交换,不是零就下一位.不贴代码了,比较简单. 27. Remove Element 解题思路: 这道 ...