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的更多相关文章

  1. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  2. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  3. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  4. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  5. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  6. poj 1611 The Suspects(并查集)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21598   Accepted: 10461 De ...

  7. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  8. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  9. poj 1611 The Suspects(第一道并查集)

    题意: 有N个学生,编号为0-n-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染, 并且这些人如果还参加了别的社团,他所在的社团照样全部感染,社团个数为m,求感染的人数. 输入: n代表人 ...

  10. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

随机推荐

  1. ArrayList、HashMap 与 员工类(程序员、经理的结合使用) 相当于集合与继承的总结

    package Day28ketangzuoye; import java.util.ArrayList; import java.util.HashMap; import java.util.Map ...

  2. Normal equations 正规方程组

    前面我们通过Gradient Descent的方法进行了线性回归,但是梯度下降有如下特点: (1)需要预先选定Learning rate: (2)需要多次iteration: (3)需要Feature ...

  3. VS2012出现加载失败时的解决办法 win7同样适用

    今天更新了WIN8系统补丁,然后就出现了大量的问题,特别是经常用的软件像VS2012 老是加载失败,还说是缺少什么包一类的,弄了好长段时间终于解决了,现在将经验分享下 工具/原料 VS2012+WIN ...

  4. 5.1点击4个按钮显示相应的div

    事件:onclick 属性:display,className 用到for语句,index标记,this当前事件 先清空后附加 <!DOCTYPE html><html>< ...

  5. Android应用开发细节点

    1.如果handler是在主线程声明,就属于主线程,handleMessage属于引用handler的那个线程:2.ByteArrayOutputStream/ByteArrayInputStream ...

  6. window_c++_socket编程_winsock2.h

    1.初始化动态链接库 WSAStartup: The WSAStartup function initiates use of the Winsock DLL by a process. WSASta ...

  7. WebService 生成客户端

    webservice 生成客户端 wsdl2java -encoding UTF-8 -p com.trm -d D:\happywork -client http://localhost/trm-t ...

  8. 循环和递归的区别(以前以为递归就是for循环!错的!)

    这里直接上代码!!!! //代码1:(for循环实现的代码) void main() { ; ; i<;i++) { n++; } printf("%d",n); } //代 ...

  9. Tensorflow入门-上

    前置准备 在阅读本文之前,请确定你已经了解神经网络的基本结构以及前向传播.后向传播的基本原理,如果尚未了解,可以查看下文. 深度学习之神经网络 什么是TensorFlow? TensorFlow是Go ...

  10. windows sdk编程为应用程序添加图标

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...