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实现图片裁剪预览功能
在项目中.我们须要做些类似头像上传,图片裁剪的功能,ok看以下文章! 须要插件:jQuery Jcrop 后端代码: package org.csg.upload; import java.awt.R ...
- Json_decode:详解
Json_decode:详解 json_decode - 对 JSON 格式的字符串进行编码 mixed json_decode ( string $json [, bool $assoc = f ...
- 详解Android首选项框架的使用
首选项这个名词对于熟悉Android的朋友们一定不会感到陌生,它经常用来设置软件的运行参数. Android提供了一种健壮并且灵活的框架来处理首选项.它提供了简单的API来隐藏首选项的读取和持久化,并 ...
- 【Java】Java_09 类型转换
1.自动类型转换 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型.在图中,黑色的实线表示无数据丢失的自动类型转换,而红色的虚线表示在转换时可能会精度的损失. 特例: 可以将整型常量直接赋 ...
- hibernate 继承映射关系( JOINED)
一个主表,其他的表每个都有自己的表来装填自己特有的部分,共同的部分就放在主表中. package com.bjsxt.hibernate; import javax.persistence.Ent ...
- android 内部类的优化
developer.android.com 文档中有一篇关于性能的文章,里面提到了内部类的使用.文章建议"对于私有内部类 使用 包訪问权限取代私有权限訪问", 这里说的是在内部类訪 ...
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- MySQL:系列合集
MySQL一:初识数据库 MySQL二:库操作 MySQL三:存储引擎 MySQL四:表操作 MySQL五:数据操作 MySQL六:索引原理与慢查询优化 MySQL七:数据备份 MySQL八:视图.触 ...
- leetcode第一刷_Balanced Binary Tree
二叉平衡树好火啊.差点儿每一个公司的笔试题里都有它.考了好多次我都不会,挂笔试非常有可能就是由于它.另一个它的同伙叫二叉搜索树,貌似人气比它还要高一些. 二叉平衡树是什么样的树呢.是每一个节点的左右子 ...
- Java重构-策略模式、状态模式、卫语句
前言 当代码中出现多重if-else语句或者switch语句时.弊端之一:如果这样的代码出现在多处,那么一旦出现需求变更,就需要把所有地方的if-else或者switch代码进行更改,要是遗漏了某一处 ...