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) ...
随机推荐
- 病毒&烦人的幻灯片
<病毒>传送门 <烦人的幻灯片>传送门 病毒 描述 有一天,小y突然发现自己的计算机感染了一种病毒!还好,小y发现这种病毒很弱,只是会把文档中的所有字母替换成其它字母,但并不改 ...
- BZOJ 1432
Description Input 一行两个整数n; k. Output 一行一个整数,表示n 个函数第k 层最少能由多少段组成. #include<iostream> using ...
- ros pcl sensor::pointcloud2 转换成pcl::pointcloud
#include <pcl_conversions/pcl_conversions.h> #include <pcl/point_types.h> #include <p ...
- Qt笔记——QFile,QDataStream,QTextStream
QFile #ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } clas ...
- hdu5737
首先思考一个朴素的做法 将b[]维护成一个线段树套有序表,每次修改a[]用线段树+lazy tag 并在线段树的子区间上在有序表中二分更新这段区间中a[i]>=b[i]的值,复杂度O(nlog^ ...
- 关于android studio3.0版本后引入资源问题error: failed linking file resources
最近更新阿里短视频的SDK,导入demo项目时,发现两个问题: 1.频繁报错AAPT2 error: file not found.以为直接Sync重新编译就可以,但是仍旧频繁提醒引入资源错误. 查找 ...
- 最适合2018年自学的web前端零基础系统学习视频+资料
这份资料整理花了近7天,如果感觉有用,可以分享给更有需要的人. 在看接下的介绍前,我先说一下整理这份资料的初衷: 我的初衷是想帮助在这个行业发展的朋友和童鞋们,在论坛博客等地方少花些时间找资料,把有限 ...
- (三)mysql数据库基本操作
(1)SQL语句:结构化查询语句 DDL语句 数据定义语言:数据库丶表丶视图丶索引丶存储过程丶函数丶create drop alter DML语句 数据库操作语言:插入数据insert,删除数据del ...
- HDU 6206 Apple【计算几何+高精度Java】
Problem Description Apple is Taotao's favourite fruit. In his backyard, there are three apple trees ...
- MySQL笔记之视图的使用详解
原文:http://www.jb51.net/article/36363.htm 1.什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门 ...