POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
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.
input starts with an integer T(1<=T<=25) which indicate the
number of test cases. Then T test cases follow. Each test case starts
with two integers N and M(1<=N,M<=1000). N indicates the number of
friends, the friends are marked 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.
- #include<iostream>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- #include<iomanip>
- #include<map>
- using namespace std;
- int fa[];
- bool flag[];
- int findf( int x ){
- int root = x, pos = x;
- while( root != fa[root] ){
- root = fa[root];
- }
- while( pos != root ){
- x = fa[pos];
- fa[pos] = root;
- pos = x;
- }
- return root;
- }
- int main(){
- ios::sync_with_stdio( false );
- int t, n, m, a, b, ans;
- cin >> t;
- while( t-- ){
- cin >> n >> m;
- for( int i = ; i <= n; i++ ){
- fa[i] = i;
- }
- memset( flag, false, sizeof( flag ) );
- ans = ;
- while( m-- ){
- cin >> a >> b;
- if( findf( a ) != findf( b ) ){
- fa[findf( a )] = b;
- }
- }
- for( int i = ; i <= n; i++ ){
- int temp = findf( i );
- if( !flag[temp] ){
- ans++;
- flag[temp] = true;
- }
- }
- cout << ans << endl;
- }
- return ;
- }
POJ-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 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1456 Supermarket 区间问题并查集||贪心
F - Supermarket Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
随机推荐
- Java 字符串分隔 split
Java中的我们可以利用 split 方法(Java.lang.string.split)把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项. s ...
- ES2019 / ES10有什么新功能?
ECMAScript(简称ES)是ECMA International在ECMA-262和ISO / IEC 16262中标准化的脚本语言规范.它是为了标准化JavaScript语言而创建的,以便从浏 ...
- 阿里云Linxu下的Mysql安装与配置
说明:本文主要详细介绍了关于如何在阿里云ECS服务器上安装并配置Mysql 环境:Centos 7版本,阿里云部署好系统后会默认安装mariadb数据库 1.删除阿里云自带的MariaDB # rpm ...
- python创建虚拟环境(Windows)
>>>构建Python虚拟环境的目的是为了防止真实环境被破坏!!! >>>每一个项目建议用一个虚拟环境为了防止软件版本号冲突!!! 1.在终端切换到一个新的磁盘 如 ...
- 利用ImageAI库只需几行python代码超简实现目标检测
目录 什么是目标检测 目标检测算法 Two Stages One Stage python实现 依赖 安装 使用 附录 什么是目标检测 目标检测关注图像中特定的物体目标,需要同时解决解决定位(loca ...
- Java小白进阶之值传递-引用传递
class ClassA{ int value;//成员变量 } public class TestClassA{ public static void main(String args[]){ in ...
- Java并发编程实战笔记—— 并发编程4
1.同步容器类 同步容器类都是线程安全的,但在某些情况下可能需要额外的客户端加锁保护复合操作. 容器上常见的复合操作包括但不限于:迭代(反复访问数据,直到遍历完容器中所有的元素为止).跳转(根据指定顺 ...
- android ——滑动菜单
一.DrawerLayout是一个拥有两个子控件的布局,第一个子控件是主屏幕中显示的内容,第二个子控件是滑动菜单中显示的内容: <android.support.v4.widget.Drawer ...
- 关于Oracle本地连接出现与监听有关的问题的解决方法探讨
关于Oracle本地连接出现与监听有关的问题的解决方法探讨 监听的作用: 用于应用桌面即用户与数据库服务器建立连接的媒介,客户端发送连接请求,监听识别请求并建立客户端与服务器的连接后,监听的使命并完成 ...
- spring boot 打 war包
spring boot .spring cloud打 war包,并发布到tomcat中运行 1.pom文件修改 <packaging>war</packaging> 2.< ...