(step5.1.3)hdu 1213( How Many Tables——1213)
题目大意:输入两个整数n,m。分别表示点数、对点的操作的次数。在接下来的m行中,每行有两个整数a,b。表示a和b是好朋友。(不是好朋友的不能坐在同一个桌子上)
。求需要多少张桌子
解题思路:并查集
1)用total来记录所需要的桌子数。如果没合并一次total就减1.
代码如下:
/*
* 1213_1.cpp
*
* Created on: 2013年8月23日
* Author: Administrator
*/ #include <iostream> using namespace std; int father[1000];
int total; int find(int x){
int r,i,j; r = x;
while( r != father[r]){
r = father[r];
} i = x;
while( i != r){
j = father[i];
father[i] = r;
i = j;
} return r;
} void join(int x , int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
father[fx] = fy;
total--;
}
} void make_set(int n){
int i;
for(i = 1 ; i <= n ; ++i){
father[i] = i;
}
} int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
scanf("%d%d",&n,&m);
make_set(n);
total = n; int i;
for( i = 1 ; i <= m ; ++i){
int a,b;
scanf("%d%d",&a,&b);
join(a,b);
}
printf("%d\n",total);
}
}
(step5.1.3)hdu 1213( How Many Tables——1213)的更多相关文章
- 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(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- 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 < ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- 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 (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- OC中使用 static 、 extern、 const使用
static static用于定义静态变量,静态变量只会被初始化一次,并且直到程序销毁时才会释放 static NSString *str = @"asdfa"; const co ...
- mysql安装详细步骤图解
本文转自http://blog.csdn.net/fanyunlei/article/details/21454645 别看图多,其实mysql的安装十分简单,一路next即可,只是注意倒数第三步,设 ...
- Windows 配置JAVA的环境变量
Java是由Sun公司开发的一种应用于分布式网络环境的程序设计语言,Java语言拥有跨平台的特性,它编译的程序能够运行在多种操作系统平台上,可以实现“一次编写,到处运行”的强大功能. 工具/原料 JD ...
- android sdk Manager path
- 远程连接MySQL 不允许
报错:1130-host ... is not allowed to connect to this MySql server 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在localhost ...
- 理解Python的with as语句
简单的说, with open(filepath, 'wb') as file: file.write("something") 等价于: file = open(filepath ...
- ipc$爆破密码
FOR /L %%i IN (1,1,99) DO net use \\192.168.1.1\ipc$ /user:test %%i && echo %%i>1.txt
- Protel中放置汉字工具的使用图示
首先先到网上下载Protel中放置汉字工具ProtelHz.然后把ProtelHz中的文件全部解压到Protel99se安装目录X:\Program Files\Design Explorer 99 ...
- Python单元测试:unittest使用简介
一.概述 本文介绍python的单元测试框架unittest,这是Python自带的标准模块unittest.unittest是基于java中的流行单元测试框架junit设计的,其功能强大且灵活,对于 ...
- BZOJ 1023
program bzoj1023; uses math; ; maxn=; maxm=; type edge=record togo,next:longint; end; var n,m,cnt,in ...