原题链接: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. java.sql.SQLException: validateConnection false

    -- :: --- [Create-] com.alibaba.druid.pool.DruidDataSource : create connection error java.sql.SQLExc ...

  2. !important的用法及作用

    定义及语法 !important,作用是提高指定样式规则的应用优先权(优先级).语法格式{ cssRule !important },即写在定义的最后面,例如:box{color:red !impor ...

  3. sql 保存,性能高

    INSERT INTO TABLE( Id, Name) VALUES ( 4, 'A'), ( 5, 'P' ),( 6, 'U') ; INSERT INTO TABLE( Id, Name) S ...

  4. gcc 4步编译过程

    一. gcc编译过程  1. 预处理: 主要进行宏替换以及头文件的展开  gcc  -E   *.c  -o  *.i 2.  编译::编译生成汇编文件,会检查语法错误   gcc  -S   *.i ...

  5. Sublime Text自定制代码片段(Code Snippets)

    在编写代码的整个过程中,开发人员经常会一次又一次的改写或者重用相同的代码段,消除这种重复过程的方法之一是把我们经常用到的代码保存成代码片段(snippets),这使得我们可以方便的检索和使用它们. 为 ...

  6. C盘清理记——罪魁Visual Studio

    话不啰嗦,单刀直入:在C:\ProgramData\Microsoft Visual Studio文件夹下,VS会自动记录IntelliTrace File,久而久之,会无限消耗磁盘空间,直接到塞满C ...

  7. Apache虚拟目录实现同一个IP绑定多个域名

    在前:我使用的是Xampp,所以路径可能不同 找到apache\conf\extra\httpd-vhosts.conf, 如果没有的话请自己新建httpd-vhosts.conf文件, 并且在htt ...

  8. Docker保存日志到本地

    其实很简单 docker logs +你需要添加的额外参数 + 容器id >文件名称 然后查看这个文件就可以了,也可以通过ftp协议下载到本地

  9. python模块typing的作用

    一.介绍 Python是一门弱类型的语言,很多时候我们可能不清楚函数参数类型或者返回值类型,很有可能导致一些类型没有指定方法,在写完代码一段时间后回过头看代码,很可能忘记了自己写的函数需要传什么参数, ...

  10. hibernate_04_hibernate多对多的关系映射

    1.实体类的多对多的关系映射 一个用户可以有多个角色 User.java public class User { private Long user_id; private String user_c ...