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 ...
随机推荐
- T420s成功加装固态硬盘(SSD)
目的 为了提升系统和经常使用工具的启动速度,ThinkPad T420s光驱位加一块固态硬盘. 操作步骤 购买:没做太多对照了解,初步计划是安装在光驱位,直接JD上买了SanDisk的128G和推荐的 ...
- 安装Drupal7.12+Postgresql9.1(Ubuntu Server 12.04)
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ OS环境准备 这次是从OS开始安装的.最开始装Ubuntu12.04这里就不再赘述, 唯 ...
- html标签说明
dictype 不区分大小写 HTML 4.01 与 HTML5 之间的差异 在 HTML 4.01 中有三种 <!DOCTYPE> 声明.在 HTML5 中只有一种: <!DOCT ...
- python操作xml文件
一.什么是xml? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. abc.xml <?xml version="1.0&q ...
- Jquery全选与反选点击执行一次的解决方案
在做项目时遇到一个bug,checkbox全选与反选功能,只能点击一次,再点就不起作用了,为了解决此问题,我查找了好多资料,下面把具体解决方案整理分享给大家,需要的朋友可以参考下: 代码需求, 使用a ...
- 项目实践中--Git服务器的搭建与使用指南
一.前言 Git是一款免费.开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.在平时的项目开发中,我们会使用到Git来进行版本控制. Git的功能特性: 从一般开发者的角度来 ...
- gdb 详解
环境:gcc (OpenWrt/Linaro GCC 4.8) 以如下的简单代码为例,说明gdb的使用. void func1(int a, int b) { int c; c = a + b; } ...
- unity3d 动画卡帧 动画合成 动画层次
2013-02-26 16:22 2059人阅读 评论(0) 收藏 举报 unity3d 中动画的添加 http://unity3d.com/support/documentation/Manua ...
- iOS开发常见错误(转)
//大全 //读取数据库数据 -(void)movethesqlitefile{ NSString *sourcepath=[[NSBundle mainBundle]pathForResource: ...
- Office Web Apps Server 2013与PDF(一)
好吧--这个消息有点旧,迟了将近4个月. Office Web Apps是微软各大服务产品系列中的一个基础服务,可以为SharePoint 2013.Exchange 2013.Lync 2013提供 ...