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 ...
随机推荐
- root运行chrome
os:centos7 edit file : /usr/bin/google-chrome Add "--user-data-dir" (without the quotes) a ...
- (二)iOS如何把所有界面的状态栏的字体颜色都设置为白色
第一步:在info.plist中添加一个字段:view controller -base status bar 设置为NO 第二步:在一个所有界面都继承的父类里添加: if (IOS7_OR_LATE ...
- (转) function与感叹号
原文:http://blog.sina.com.cn/s/blog_775f158f01016j12.html function与感叹号(转)(2012-08-29 12:29:12) 最近有空可以让 ...
- hdu 2000
ps:刚开始学C++...用C++来试试.. 代码: #include <iostream> using namespace std; int main(){ ],t,i,j; ]> ...
- C++11 std::copy
这个函数并不是简单的 while(first != last) { *result = *first; result++; first++; } 事实上这种写法是最具普适性的,值要求inputIter ...
- 0911 Socket网络编程
1.实现ftp上传.下载功能 1.1 循环接收数据直到接收完毕 server端接收client发送的命令(比如说ifconfig),然后server端将命令执行结果反馈给客户端,这时候有个问题,ser ...
- php中数据库的操作
1.Mysql客户端介绍,命令行:这种方法不友好. 2.Mysql客户端介绍,Web形式的可视化界面(phpMyAdmin) 优点:只要有浏览器就可以操作数据库 缺点: a)创建数据库
- ios openURL的使用(调用系统电话、浏览器、地图、邮件等)
Safari Any URL starting with http:// which does not point to maps.google.com or www.youtube.com is s ...
- 完美解决方案,可排除DATASET不支持System.Nullable错误
完美解决方案,可排除DATASET不支持System.Nullable错误 using System; using System.Collections.Generic; using System.L ...
- GridView按钮事件
1.html代码 <asp:TemplateField HeaderText="操作"> <ItemTemplate> <div style=&quo ...