B - The Suspects(并查集)
B - The Suspects
Time Limit:1000MS Memory Limit:20000KB 64bit IO Format:%lld & %llu
Description
Input
Output
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 //初学并查集,第二周训练第一个完全没百度做出来的,中规中矩的,虽然过了但是用了 352KB 922ms,好险。以后有机会更新优化。
#include <iostream>
#include <cstdio>
using namespace std; const int N=;
int set_tree[N];
int person[N];
bool flag[N]; void Init ()
{
for (int i=;i<N;i++)
{
set_tree[i]=i;
flag[i]=;
}
} int find_head(int x)
{
while (x!=set_tree[x]) x=set_tree[x];
return x;
} void link(int x,int y)
{
x=find_head(x);
y=find_head(y);
if (x==y) return;
set_tree[x]=set_tree[y];
} void hebing(int p)
{
int i;
for (i=;i<p-;i++)//合并p个人.0.1,1.2,......,p-2.p-1 合并
{
link(person[i],person[i+]);
}
} int main()
{
int n,m,p,i,all;
Init();
while (scanf("%d%d",&n,&m))
{
if (n==&&m==) break;
Init();
all=;
while (m--)
{
scanf("%d",&p);
for (i=;i<p;i++)
{
scanf("%d",&person[i]);
flag[person[i]]=;
}
hebing(p);
} for (i=;i<n;i++)
{
if (flag[i]==&&find_head(i)==find_head())
all++;
}
printf("%d\n",all); }
return ;
}
//原来要路径压缩 468k 16ms
#include <iostream>
#include <cstdio>
using namespace std; const int N=;
int f[N];
int person[N];
bool flag[N]; void Init ()
{
for (int i=;i<N;i++)
{
f[i]=i;//并查集初始化
flag[i]=;
}
} int find_head(int x)
{
if (x!=f[x])
f[x]=find_head(f[x]);
return f[x];
} void link(int x,int y)
{
x=find_head(x);
y=find_head(y);
if (x==y) return;
f[x]=f[y];
} int main()
{
int n,m,p,i,j,all;
Init();
while (scanf("%d%d",&n,&m))
{
if (n==&&m==) break;
Init(); all=;// 一开始 0 号就是患者 while (m--)
{
scanf("%d",&p);
for (i=;i<p;i++)
{
scanf("%d",&person[i]);
flag[person[i]]=;
}
for (j=;j<p-;j++)//合并p个人.0.1,1.2,......,p-2.p-1 合并
link(person[j],person[j+]);
} for (i=;i<n;i++)
{
if (flag[i]==&&find_head(i)==find_head())
all++;
}
printf("%d\n",all); }
return ;
}
B - The Suspects(并查集)的更多相关文章
- The Suspects(并查集维护根节点信息)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 37090 Accepted: 17980 De ...
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- The Suspects(并查集求节点数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28164 Accepted: 13718 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
随机推荐
- JAVA-IO操作,字节-字符转换流
掌握OutputStreamWriter和InputStreamReader类的作用 一般操作输入输出内容的时候,就需要使用字节或字符流,但是,有些时候,需要将字符流变成字节流形式,或者字节流变成字符 ...
- iDempiere的用户密码加密处理(AD_User.Password)(Postgresql 9.1)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 首先对Postgresql数据库进行加密处理. 参考网页:francs写的:Postgr ...
- 《DirectX 9.0 3D游戏开发编程基础》必备的数学知识 读书笔记
最近在看游戏导航源码,但是看了几天感觉看不懂.里面全是一些几何运算,以及一些关于3d方面的知识.发现自己缺少3d这方面的知识,正好也想研究一下3d游戏开发的基本原理,于是决定买本书看看了,后来在ope ...
- UIAlertViewController 2
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
- 集成ueditor工具
摘要: 摘要: 版权声明:本文为博主原创文章,未经博主允许不得转载. UEditor 是百度的一套开源的在线HTML编辑器. 第一步:去官网看官网文档,了解这个工具如何使用以及下载,本人下载的是1.4 ...
- C# 读取Ini配置文件类
配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value ...
- Git使用笔记三
场景:开源社区提交代码每个人每一个MR只有两个commits,一个是merge前的,一个是merge时候的.因为他们不过是把自己在同一个PR上面提交的commits全部压缩成了一个而已.这个在git上 ...
- tony_CENTOS启动方式设置
方法: 在etc文件夹下面有个初始加载文件是用来启动系统的,系统在启动的时候先去env中找到shell的必要配置,然后把shell启动起来,那么再然后就要启动整个系统了,到底是启动图形界面呢,还是字符 ...
- Angularjs学习笔记6_table1
<!DOCTYPE html><html lang="en" ng-app="plunker"><head> < ...
- DatabaseFactory.CreateDatabase 方法操作数据库
using Microsoft.Practices.EnterpriseLibrary.Data;using Microsoft.Practices.EnterpriseLibrary.Data.Sq ...