【裸的并查集】POJ 1611 The Suspects
http://poj.org/problem?id=1611
【Accepted】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <string>
using namespace std;
const int maxn=3e4+;
int fa[maxn];
int n,m;
int a[maxn]; void init()
{
for(int i=;i<n;i++)
{
fa[i]=i;
}
}
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
} void mix(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
fa[fx]=fy;
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==)
{
break;
}
init();
for(int i=;i<m;i++)
{
int k;
scanf("%d",&k);
if(k>=)
{
scanf("%d",&a[]);
for(int j=;j<k;j++)
{
scanf("%d",&a[j]);
mix(a[],a[j]);
}
}
}
int sum=;
for(int i=;i<n;i++)
{
if(find(i)==fa[])
{
sum++;
}
}
printf("%d\n",sum);
}
return ;
}
【不太明白】
为啥find(i)==fa[0]能A,fa[i]==fa[0]就是WA。find(0)一定等于fa[0]?
【裸的并查集】POJ 1611 The Suspects的更多相关文章
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- 并查集 (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: 21472 Accepted: 10393 De ...
- POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...
- POJ 1611 The Suspects (并查集)
The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 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(第一道并查集)
题意: 有N个学生,编号为0-n-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染, 并且这些人如果还参加了别的社团,他所在的社团照样全部感染,社团个数为m,求感染的人数. 输入: n代表人 ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
随机推荐
- 题解报告:hdu 2087 剪花布条(KMP入门)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...
- [译]curl_multi_add_handle
NAMEcurl_multi_add_handle - add an easy handle to a multi session添加easy handle到multi session中 SYNOPS ...
- 原创 SqlParameter 事务 批量数据插入
不错,很好,以后防注入批量事务提交虽然麻烦点研究了几个小时,但不会是问题了 SqlCommand cmd; HelpSqlServer helps = new HelpSqlServer(); //定 ...
- java之java.sql.SQLException: ResultSet is from UPDATE. No Data.
问题解释:java调用存储过程的时候,查询结果不能通过ResultSet来查询,需要通过CallableStatement来查询, 比如: ResultSet rs = callableStateme ...
- 《基于Node.js实现简易聊天室系列之项目前期工作》
前期工作主要包括:项目的创建,web服务器的创建和数据库的连接. 项目创建 网上关于Node.js项目的创建的教程有很多,这里不必赘述.Demo所使用的Node.js的框架是express,版本为4. ...
- java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to
在做android解析服务器传来的json时遇到的错误. 服务器传来的数据格式 [{"," id":"7ef6815938394fce88a5873312b66 ...
- javscript 导出html中的table到excel
<script language="JavaScript" type="text/javascript"> /* * 默认转换实现函数,如果需要其他 ...
- 怎么学好XXX
怎么学好数据库是一个比较大题目,数据库不仅仅是写SQL那么简单,即使知道了SQL怎么写,还需要很清楚的知道这条SQL他大概扫描了多少数据,返回多少数据,是否需要创建索引.至于SQL优化是一个比较专业的 ...
- HDU_3792_(素数筛+树状数组)
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- jquery 点击切换div
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...