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 ...
随机推荐
- sqlite3 解决并发读写冲突的问题
#include "stdafx.h" #include "sqlite3.h" #include <iostream> #include < ...
- lodash capitalize 首字母大写
_.capitalize([string='']) 转换字符串首字母为大写,剩下为小写. _.capitalize('FRED'); // => 'Fred'
- HttpClient Coder Example
Example 1: HttpClient httpClient = new HttpClient(); httpClient.getHostConfigurati ...
- 使用SAS令牌连接Azure EventHub
概述 事件中心使用在命名空间和事件中心级别提供的共享访问签名.SAS令牌是从SAS密钥生成的,它是以特定格式编码的URL的SHA哈希. 事件中心可以使用密钥(策略)的名称和令牌重新生成哈希,以便对发送 ...
- rtp协议详解/rtcp协议详解
转自:http://www.cnblogs.com/li0803/archive/2010/11/20/1882792.html 1.简介 目前,在IP网络中实现实时语音.视频通信和应用已经成为网络应 ...
- unity, 显示像素图,以及iOS下像素图变模糊解决办法
在PS里画了个16x16像素的图: 在webplayer下Filter Mode选为Point,显示效果为: 在ios下显示效果为: 是由于iOS下会将图片压缩为pvr所致,想得到清晰的效果,需将Fo ...
- Atitit hre框架v5 新特性 HREv5
Atitit hre框架v5 新特性 HREv5 1. V5新特性 apiurl2="/wrmiServlet";1 2. V1 新特性1 3. V2 新特性 添加php版1 ...
- leetCode 87.Scramble String (拼凑字符串) 解题思路和方法
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- GroupCoordinator joingroup源码解析
转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7463693.html kafka新版consumer所有的group管理工作在服务端都由Group ...
- vimrc之fileformat
我在Linux开发时经常会遇到这样的问题,在windows下编辑的文件拿到Linux下打开时发现会在每行的结尾出现一个^M的符号,影响美观 为了消除这个符号,刚开始不知道Linux下有unix2dos ...