http://acm.hdu.edu.cn/showproblem.php?pid=1213

题意:

这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以留在一个桌子。

例如:如果我告诉你A知道B,B知道C,D知道E,所以A,B,C可以留在一个桌子中,D,E必须留在另一个桌子中。所以Ignatius至少需要2个桌子。

思路:

并查集模板题。

#include<iostream>
using namespace std; int p[]; int find(int x)
{
return p[x] == x ? x : find(p[x]);
} void Unions(int x, int y)
{
p[x] = y;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int T, a, b, n, m;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = ; i <= n; i++)
p[i] = i;
for (int i = ; i < m; i++)
{
cin >> a >> b;
int x = find(a);
int y = find(b);
if (x != y)
Unions(x, y);
}
int ans = ;
for (int i = ; i <= n;i++)
if (p[i] == i) ans++;
cout << ans << endl;
}
return ;
}

HDU 1213 How Many Tables(并查集模板)的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

  4. HDU 1213 How Many Tables (并查集,常规)

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  5. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  6. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  7. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  8. 【2018寒假集训Day 8】【并查集】并查集模板

    Luogu并查集模板题 #include<cstdio> using namespace std; int z,x,y,n,m,father[10001]; int getfather(i ...

  9. POJ-图论-并查集模板

    POJ-图论-并查集模板 1.init:把每一个元素初始化为一个集合,初始化后每一个元素的父亲节点是它本身,每一个元素的祖先节点也是它本身(也可以根据情况而变). void init() { for ...

随机推荐

  1. Innodb引擎状态查看

    我们的MySQL数据库内的表一般都是Innodb表类型的. mysql>show engine  innodb status; (低版本用: show innodb status;)   === ...

  2. [vue]vue双向绑定$on $emit sync-模态框

    双向绑定实现($on $emit) 关于父子之间数据更新同步, 如果单向绑定, 子修改了,父却没有修改, 这种一般不符合规范 正常更新数据的套路是: 1. 子通知父更新数据 2. 子自动刷新获取最新数 ...

  3. springboot中的日志配置

    日志方式:每天日志存放在一个文件中,info和warn日志存放一个文件,error存放一个文件 创建文件 logback-spring.xml <?xml version="1.0&q ...

  4. jconsole监控远程 spring boot程序

    监控java 程序 增加启动参数 java  \ -Djava.rmi.server.hostname=192.168.1.97 \ -Dcom.sun.management.jmxremote \- ...

  5. 获取多达 16GB 的 Dropbox 免费空间!

    Dropbox官网

  6. JS在不同js文件中互相调用

    例如有这样一个html,里面有一个按钮,当按下时调用b.js文件中的方法b().而b()中又要调用a.js文件中的方法a().若要实现这个功能,必须注意,将要引入的Js文件代码放在</body& ...

  7. GridView 点滴

    绑定数据时.在后台给GridView添加事件 protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) { //当前 ...

  8. Leetcode: Longest Consecutive Sequence && Summary: Iterator用法以及ConcurrentModificationException错误说明

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. 《Bilateral Multi-Perspective Matching for Natural Language Sentences》(句子匹配)

    问题: Natural language sentence matching (NLSM),自然语言句子匹配,是指比较两个句子并判断句子间关系,是许多任务的一项基本技术.针对NLSM任务,目前有两种流 ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON TestRegionPoint1

    zw版[转发·台湾nvp系列Delphi例程]HALCON TestRegionPoint1 unit Unit1;interfaceuses Windows, Messages, SysUtils, ...