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 ...
随机推荐
- java基础-001
一.区分final . finally . finalize 1.关键字final Java语言的关键字final可以用于变量.类或方法,但是含义会有所不同. -用于变量:一旦初始化,变量值就不能修 ...
- (五)CoreData 使用 (转)
第一次真正的使用CoreData,因此也会写下体会和心得...等有时间 Core Data数据持久化是对SQLite的一个升级,它是ios集成的,在说Core Data之前,我们先说说在CoreDat ...
- FragmentActivity+FragmentTabHost+Fragement instead of TabActibvity+TabHost+Activity
http://www.tuicool.com/articles/NzeMJz http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragm ...
- linux基础命令学习(一)
pwd 输出当前工作路径tree 以树状图列出目录的内容ctrl+c 取消命令的执行clear 清空屏幕ls 列出文件目录 蓝色是目录,白色是普通文件alias cls=clear 别名终端:本地终端 ...
- 配置VS2010具有代码提示功能
Visual Assist X是一款非常好的Microsoft Visual Studio插件,可以支持Microsoft Visual Studio 2003,Microsoft Visual St ...
- 4、SQL基础整理(规范函数)
规范函数: 绝对值 select abs(-5) print abs(-5) 表中取绝对值的方法: select code,name,abs(chinese)as yuwen from xueshen ...
- C# asp.net IIS 在web.config和IIS中设置Session过期时间
有时候在web.config设置sessionState 或者类文件里设置Session.Timeout,在IIS里访问时每次都是达不到时间就超时,原因是因为在IIS中设置了Session的超时时间, ...
- GridView 分页方法
要实现GrdView分页的功能. 操作如下: 1.更改GrdView控件的AllowPaging属性为true. 2.更改GrdView控件的PageSize属性为 任意数值(默认为10) 3.更改G ...
- 我的第一个unity3d Shader, 很简单,基本就是拷贝
Shader "Castle/ColorMix" { Properties { // 基本贴图 _MainTex ("Texture Image", 2D) = ...
- linux下C语言中的flock函数用法 【转】
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指 ...