1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
简单并查集,统计单独成树的数量。
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <map>
#include <stdlib.h>
using namespace std; int f[];
int n,m; void init()
{
for(int i=;i<=n;i++)
f[i]=i;
} int find(int x)
{
if(x==f[x])
return f[x];
f[x]=find(f[x]);
return f[x];
} void Union(int x,int y)
{
int a=find(x);
int b=find(y);
if(a==b)
return;
f[a]=b;
} int main()
{
int t,a,b;
while(cin>>t){
while(t--){
scanf("%d %d",&n,&m);
init();
int sum=;
for(int i=;i<m;i++){
scanf("%d %d",&a,&b);
Union(a,b);
}
for(int i=;i<=n;i++){
if(i==find(i)){
sum++;
}
}
printf("%d\n",sum);
}
}
}
1213 How Many Tables(简单并查集)的更多相关文章
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU 1213 How Many Tables(并查集模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- hdu 1213 How Many Tables(并查集练习)
题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- HDU 1213 How Many Tables(并查集裸题)
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- How Many Tables 简单并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- HDU 1213 How Many Tables【并查集】
解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- 【C语言探索之旅】 第一部分第六课:条件表达式
内容简介 1.课程大纲 2.第一部分第六课:条件表达式 3.第一部分第七课预告:循环语句 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏. ...
- SQL Server数据库附加失败:错误5120和错误950
再次敲机房,想參考曾经的物理模型,结果在附加数据库这一环节出现了点问题,以下总结一下. 1.附加数据库失败,错误5120 对于这样的错误,我在网上搜集了一下,主要有下面几种解决的方法: 方法一:将要附 ...
- form 为什么上传文件enctype现场
FORM要素enctype属性指定表单数据server当提交所使用的编码类型,默认默认值它是"application/x-www-form-urlencoded". 这样的编码方式 ...
- Eamcs ditaa基于字符图形产生的图像上
ditta和artist mode这是一个好兄弟.artist mode帮我创建一个字符模式速度,ditta是java计划,字符图形可被读取,并生成图像. ditta网站:http://ditaa.s ...
- 大约session_cached_cursors在不同的db在默认不同的版本号
大约session_cached_cursors的值,不同db版本号具有不同的默认值: 9i是 0 10.1 0 10.2 是20 11.1 是50 11.2 是50 12.1 是50 值值得注意的是 ...
- CodeForces 69D Dot (游戏+记忆)
Description Anton and Dasha like to play different games during breaks on checkered paper. By the 11 ...
- 在Repeater控件中使用if语句
原文:在Repeater控件中使用if语句 .Afr_ARTICLE_TITLE { font: NORMAL BOLD 14px "Tahoma"; } .Afr_CONTENT ...
- 提升Mac os x 10.10+xcode6.1之后,Cocoapods发生故障的解决方案
提升Mac OS X 10.10+Xcode 6.1之后.Cocoapods图书馆管理也依赖于相应升级.现在最新的Release版本号是 0.34.在之前的版本号.当数据库更新和管理,你会遇到一个错误 ...
- Hsql中In没有1000的限制
SELECT * FROM user , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- String.Format in Java and C#
原文:String.Format in Java and C# JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String ...