题目地址

http://acm.hdu.edu.cn/showproblem.php?pid=1213

 #include<iostream>
using namespace std;
#define MAX 100005
int fa[MAX]; int findHead(int x)
{
while(x!=fa[x])
x=fa[x];
return x;
} void Union(int x,int y)
{
int fa_x=findHead(x);
int fa_y=findHead(y);
if(fa_x!=fa_y) fa[fa_x]=fa_y;
} int main()
{
int T,N,M,A,B,count;
cin>>T;
while(T--){
count=;
cin>>N>>M;
for(int j=;j<=N;j++) fa[j]=j;
for(int i=;i<M;i++){
cin>>A>>B;
Union(A,B);
}
for(int i=;i<=N;i++)
if(fa[i]==i) count++;
cout<<count<<endl;
}
return ;
}

HDU1213最简单的并查集问题的更多相关文章

  1. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  2. The Suspects 简单的并查集

    Description 严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not ...

  3. The Suspects(简单的并查集)

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

  4. hdu 1182 A Bug's Life(简单种类并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...

  5. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  6. hdu 1213 (How Many Tables)(简单的并查集,纯模板)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 1213 How Many Tables 简单的并查集问题

    my code: #include <cstdio>#include <cstring>#include<iostream>using namespace std; ...

  9. HDU--1213并查集

    题目传送门:HDU--1213 //题意:ignatius过生日,客人来到,他想知道他需要准备多少张桌子.然而一张桌子上面只能坐上相互熟悉的人, //其中熟悉可定义成为A与B认识,B与C认识,我们就说 ...

随机推荐

  1. linux/centos系统如何使用yum安装vi/vim?

    yum安装vim最简单的命令, yum -y install vim* 然后就可以使用vi命令了. 网上的文章: 要使用vim, 使用yum看了一下,发现有4个 vim-common.i386     ...

  2. js 获取最后一个字符

    方法一: str.charAt(str.length - 1) 方法二: str.subStr(str.length-1,1) 方法三:    var str = "123456" ...

  3. AJPFX总结线程创建的两种方法

    创建线程的第一种方式:继承Thread ,由子类复写run方法.步骤:1,定义类继承Thread类:2,目的是复写run方法,将要让线程运行的代码都存储到run方法中:3,通过创建Thread类的子类 ...

  4. spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...

  5. 一个Java编写的小玩意儿---多人在线聊天工具

    这个在线聊天工具小项目使用JAVA编写,用JAVA来做图形界面本来就是出了名的低效和丑陋.不过这不是重点.写这个小项目的目的在于串一串J2SE的知识,把当时写这个项目的时候的思路梳理一下.时间有点久了 ...

  6. OCP 11g 第二章练习

    练习 2-1 在Windows计算机上安装SQL Developer 在本练习中,将在Windows计算机上安装SQL Developer 1. 从以下URL下载当前SQL Developer版本: ...

  7. $("xxx").attr添加属性的时候不好用

    今天在工作中碰到了使用$(this).attr("selected","selected")为option属性添加默认值时发现时而好用 时而不好用,后经百度发现 ...

  8. ubuntu 下service php5-fpm restart 报错 stop: Unknown instance: 解决

    问题描述: 在安装完扩展后,重启php-fpm,发现一直停止报错 stop: Unknown instance: 通过查看进程,也查询不到该主进程 解决办法: 干掉现在正在执行的进程 pkill ph ...

  9. VBA 从sql存储过程-记录集-导入

    cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...

  10. c# 从DataGridVieew导出到excel

    public static bool DataGridViewToExcel(DataGridView dataGridView, bool isShowExcel) { int rowsQty = ...