原题链接:http://poj.org/problem?id=1611

简单记录下并查集的模板

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#define ll long long
#define pi 3.1415927
using namespace std;
int fat[];
int finds(int n)
{ //查找
if(fat[n]==n)
return n;
return fat[n]=finds(fat[n]); //路径压缩
}
void join(int a, int b)
{ //合并
int j=finds(a), k=finds(b);
if(j<k)
fat[k]=j;
else
fat[j]=k;
}
int main ()
{
int n,m,i,t,j,k,n2,a,b;
while(scanf("%d %d",&n,&m)&&n+m)
{
for(i=;i<n;++i)
fat[i]=i;
while(m--)
{
scanf("%d %d",&n2,&a);
for(i=;i<n2;++i){
scanf("%d",&b);
join(a,b);
}
}
int sum=;
for(i=;i<n;++i)
if(finds(i)==)
sum++;
printf("%d\n",sum);
}
return ;
}

并查集 (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(并查集,经典题)

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

  3. POJ 1611 The Suspects (并查集)

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

  4. poj 1611 The Suspects(并查集)

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

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

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

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

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

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

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

  8. POJ 1611 The Suspects (并查集求数量)

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

  9. poj 1611 :The Suspects经典的并查集题目

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

随机推荐

  1. 如何通过SVN管理好代码

    来自:http://blog.csdn.net/baronyang/article/details/6942434 ------------------------------------------ ...

  2. CSS代码命名惯例语义化的方法

    CSS代码的命名惯例一直是大家热门讨论的话题.今天暴风彬彬想通过分析一个流行三栏布局中的必要元素,来为大家讲解关于使用语义化方法替代结构化方法来命名CSS类的建议和指导. 您还可以参考彬Go的相关文章 ...

  3. kafka 入门

    李克华 云计算高级群: 292870151 195907286 交流:Hadoop.NoSQL.分布式.lucene.solr.nutch  kafka入门:简介.使用场景.设计原理.主要配置及集群搭 ...

  4. android的webView内部https/http混合以及自适应屏幕

    两种请求都有的情况下,会导致WebView加载不出来. //自适应屏幕 settings.setUseWideViewPort(true); settings.setLoadWithOverviewM ...

  5. arm-linux-gcc 的使用

    1. 编译 C 文件,生成 elf 可执行文件 h1.c 源文件 #include <stdio.h> void hellofirst(void) { printf("The f ...

  6. 360自动抢票还不够,几行js代码设置无人值守

    360就是牛逼哄哄的...... 但是最近在使用360浏览器抢票的时候还是发现了一些体验不好的地方,比如搞着搞着就退出了登录,有时候能帮你自动登录进去,但是自动登录之后又不会帮你自动开始抢.然后验证码 ...

  7. MAMP 安装 php 扩展

    1.官网下载所对应的php 版本http://php.net/get/php-5.3.29.tar.gz/from/a/mirror 2.解压 找到需要的扩展目录 例如我要的是shmopcd ~/Do ...

  8. linux 将子文件夹的文件复制到 当前目录中

    linux 将子文件夹的文件复制到 当前目录中,如 目录结构大概是 -sh |__ db_backup |___ test |____ 2018_01_01_00_00_00 |_____ 2018_ ...

  9. Java迷宫代码,广度优先遍历,最短路径

    使用一个队列,采用层层扩张的方式,寻找迷宫最优的路径信息,再用一个迷宫节点数组记录行走信息方向常量定义: public interface Constant { // 右方向 int RIGHT = ...

  10. Python 输入字符串找(String)下标 没有返回-1

    str = "abcdefg123456"a = input("请输入一个字母或数字:")num = 0result = -1while num < le ...