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 ...
随机推荐
- MongoDB 语法(转)
Mongod.exe 是用来连接到mongo数据库服务器的,即服务器端. Mongo.exe 是用来启动MongoDB shell的,即客户端. 其他文件: mongodump 逻辑备份工具. mon ...
- hdu2795 线段树
//Accepted 6396 KB 3046 ms //线段树 //由于n只有200000,我们可以知道,当h>200000时,大于200000的部分是没有用的 //所以我们可以用n来创建线段 ...
- STM32下载显示target dll has been cancelled
使用MDK 4.74向STM32下载时出现各种错误,而且时隐时现, Internal command error.Error:Flash download failed. Target DLL has ...
- 怎么在官网下载jstl【配图详解】
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个非常优秀的开源JSP标签库,如果要在系统使用JSTL标签,则必须将jstl.jar和 standard.jar文件放 ...
- 加载JS
- php大力力 [015节]兄弟连高洛峰php教程(土豆网栏目地址)
兄弟连高洛峰php教程 兄弟连高洛峰php教程(土豆网栏目地址) [2014]兄弟连高洛峰 PHP教程1.1.1 新版视频形式介绍 [2014]兄弟连高洛峰 PHP教程1.1.2 BS结构软件类型介绍 ...
- Python _ 开始介绍对象
Python的私有变量,函数是在前面加上一个双下划线'__'来声明的,气访问和C++大同小异 例如 class Person: __name='小甲鱼' def print1(self): # 和 c ...
- 初次使用百度地图API
因为项目需要,不得不使用百度地图的API,以前从未了解过API,这不是唬人,真的,所以对百度地图API充满了恐惧,但是到后面,已经麻木了.期间遇到过很多错误,每一个都弄得头大,借博客的名义把平时遇到的 ...
- Redis - Keepalived + redis 主备热备切换
1. 热备方案 硬件:server两台,分别用于master-redis及slave-redis 软件:redis.keepalived 实现目标: 由keepalived对外提供虚拟IP(VIP)进 ...
- 9、SQL基础整理(两表连接exists,join on,union)
exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen ...