kuangbin_UnionFind B (POJ 1611)
过程是模板 merge完后扫一下几个跟0同祖先节点就是答案了
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
const int MAX=;
int father[MAX];
int findfather(int x)
{
while(x!=father[x])
x=father[x];
return x;
}
void merge(int x,int y)
{
x=findfather(x);
y=findfather(y);
if(x!=y) father[x]=y;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n||m)
{
int ans=;
for(int i=;i<n;i++)
father[i]=i;
while(m--)
{
int k,fir,tmp;
scanf("%d%d",&k,&fir);
k--;
while(k--)
{
scanf("%d",&tmp);
merge(fir,tmp);
}
}
int ex=findfather();
for(int i=;i<n;i++)
if(findfather(i)==ex) ans++;
printf("%d\n",ans);
}
return ;
}
(似乎这个时候代码还很丑)
kuangbin_UnionFind B (POJ 1611)的更多相关文章
- poj 1611(并查集)
http://poj.org/problem?id=1611 题意:有个学生感染病毒了,只要是和这个学生接触过的人都会感染,而和这些被感染者接触的人,也会被感染,现在给定你一些协会的人数,以及所在学生 ...
- poj 1611 The Suspects 解题报告
题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...
- poj 1611 The Suspects(简单并查集)
题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...
- 【原创】poj ----- 1611 The Suspects 解题报告
题目地址: http://poj.org/problem?id=1611 题目内容: The Suspects Time Limit: 1000MS Memory Limit: 20000K To ...
- (并查集)The Suspects --POJ --1611
链接: http://poj.org/problem?id=1611 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- POJ - 1611 The Suspects 【并查集】
题目链接 http://poj.org/problem?id=1611 题意 给出 n, m 有n个人 编号为 0 - n - 1 有m组人 他们之间是有关系的 编号为 0 的人是 有嫌疑的 然后和 ...
- 【裸的并查集】POJ 1611 The Suspects
http://poj.org/problem?id=1611 [Accepted] #include<iostream> #include<cstdio> #include&l ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
随机推荐
- ansible quick start
1. ansible默认开启ControlPersist,也就是持续化ssh的socket连接,以跳过每次task都需要进行主机认证. 2. 但是centos的openssh版本太老了,不支持Cont ...
- Oracle的DDL、DML、DCL
DDL (Data Definition Language 数据定义语言) create table 创建表 alter table 修改表 drop table 删除表 truncate table ...
- Scheme vs Schema
在计算机数据描述领域,Scheme由于原意为“主题”,“方案”,“构想”等,因此一般指比较明确的(具体的)“方案”.“体系”,例如一个术语词表.分类表等,而Schema通常翻译成“模式”,比较强调形式 ...
- BI--SDN上收集到的SAP BI的极好文章的链接
1)Overviewhttps://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/60981d00-ca87-2910-fdb ...
- (spring-第9回【IoC基础篇】)BeanFactoryPostProcessor,实例化Bean之前的第二大利器
继承结构图如上.在加载XML,注册bean definition之后,在实例化bean definition之前,必要的时候要用到BeanFactoryPostProcessor.它负责把XML中有些 ...
- iOS开发:cocoapods的使用
Cocoapods是OS X和iOS下的一个第三方类库管理工具,通过CocoaPods工具我们可以为项目添加各种依赖库,减少了我们手动引入库需要的各种配置,同时使用cocoapods可以方便的查找新的 ...
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
- socket() failed (13: Permission denied) while connecting to upstream
/*************************************************************************** * socket() failed (13: ...
- JS操作DOM常用API总结
<JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...
- mysql explain用法和结果的含义(转)
重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...