hdoj-1213-How Many Tables【并查集】
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17892 Accepted Submission(s): 8794
to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
2
5 3
1 2
2 3
4 5 5 1
2 5
2
4
pid=1856" target="_blank">
1856
pid=1325" target="_blank">
1325
#include<stdio.h>
int root[1010];
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int i,int j){
if(find(i)<=find(j)) root[j]=find(i);
else root[i]=find(j);
return;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,i,x,y,nu=0;
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i) root[i]=i;
for(i=1;i<=m;++i){
scanf("%d%d",&x,&y);
if(find(x)!=find(y))
unio(find(x),find(y));
//unio(x,y); //此处写法是错误的,在这里WA了多次! !!!! !! !。!!!!改动的应是各自的根
}
for(i=1;i<=n;++i){
if(root[i]==i) nu++;
}
printf("%d\n",nu);
}
return 0;
}
hdoj-1213-How Many Tables【并查集】的更多相关文章
- 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 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- hdu1213 How Many Tables 并查集的简单应用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- hdu1213 How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 使用chardet模块判断网页编码
import chardet import urllib.request url='http://stock.sohu.com/news/' html = urllib.request.urlopen ...
- Mysql缺少可执行的命令
MySQL问题解决:-bash:mysql:command not found 问题: [root@linux115 /]# mysql -uroot -p -bash: m ...
- python带cookie提交表单自动登录
import urllib import urllib2 import cookielib login_url = "xxxxxxxxxxxxx" cj = cookielib.C ...
- spark技术热点问题互动问答2
决胜云计算大数据时代” Spark亚太研究院100期公益大讲堂 [第3期互动问答分享] Q1: groupbykey是排好序的吗?分组排序怎么实现? groupByKey在一个由(K,V)对组成的数据 ...
- (2)java安装配置
java 分为三大类 javasSE,javaEE,javaME. javaSE:一般用于开发桌面软件,是java EE的基础类库 javaEE:用于开发网站 javaME:手机软件程序 javaSE ...
- 洛谷 P3371 【模板】单源最短路径 【链式前向星+SPFA】
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- 递归输入与引用传值(UVa839 Not so Mobile)
题目的大意是一个树形天平,输入给出样例的个数,然后空一行,每行4个数W1,D1,W2,D2,分别代表天平左侧的重量.力臂和天平右侧的重量.力臂.如果W1或者W2为0,则代表该节点有左子树或右子树,如果 ...
- java中的3大特性之多态
一.多态:一个对象具有多种表现形态(父类的引用类型变量指向了子类的对象) 二.多态的满足条件:1.必须要有继承关系2.必须要有方法的重写 三.int[]a; //a引用类型变量-->//引用in ...
- 【推广】实用命令——tldr
碎碎念 如题,通常遇到一个新的命令需要查询其帮助的时候,一般使用`command -h`或者`man command`来查询,但是,有时候仅仅想知道这个命令怎么用,并不想知道具体含义啊(这个命令可能不 ...
- Xamarin XAML语言教程基本视图ContentViewg构架范围框架
Xamarin XAML语言教程基本视图ContentViewg构架范围框架 ContentView视图基本上有三个作用,下面依次介绍. (1)范围框架:ContentView视图可以构建一个范围框架 ...