题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子。

题目思路:利用并查集对相互认识的人进行集合的划分,划分完毕后进行遍历,如果发现一个点的根节点是本身,证明找到一个集合,统计集合数便是答案了。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
#define MAX 1000005 using namespace std; int father[MAX]; int Find(int x)
{
while(x!=father[x])
{
x=father[x];
}
return x;
} int main()
{
int T,i,j,a,b,n,m; scanf("%d",&T); while(T--)
{
scanf("%d%d",&n,&m); for(i=;i<=n;i++)
father[i]=i; for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b); int x=Find(a);
int y=Find(b); if(x!=y)
{
father[x]=y;
}
} int sum=; for(i=;i<=n;i++)
{
if(Find(i)==i)//如果一个点的根节点是本身,证明找到一个新的集合
sum++;
}
printf("%d\n",sum);
}
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 求连通分量(并查集模板题)

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

  6. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU 1213 How Many Tables(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

  8. HDU 1213 How Many Tables(并查集模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...

  9. HDU - 1213 How Many Tables 【并查集】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...

随机推荐

  1. jquery-图片轮播(新手请大神指教一下)

    这是我刚学jquery写的,感觉效果不是很好. #scrollPics{ height: 330px; width: 980px; margin-bottom: 10px; overflow: hid ...

  2. android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V

    今天在看布局文件的时候出现 android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[ ...

  3. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  4. VoIP的话音质量测量方法

    严重的呼叫质量和性能管理问题会影响VoIP (Voice over IP)系统的运作.网络管理员等人需要理解基本的呼叫质量测量技术才能很好地监测.管理和诊断在VoIP中出现的这些问题.本文介绍了常用的 ...

  5. Chapter 1 First Sight——29

    I was anxious not to be late for class on my first day. 在我第一天上学的时候我非常焦虑我会上课迟到. One of my new acquain ...

  6. Codeforces Round #364 (Div. 2) C.They Are Everywhere

    C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. A - 娜娜梦游仙境系列——诡异的钢琴

    A - 娜娜梦游仙境系列——诡异的钢琴 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...

  8. js中的clientWidth offsetWidth scrollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. 给table加边框的两种方法

    <!DOCTYPE html><html><head><style>.a { border-spacing: 1px; background-color ...

  10. Perl的调试方法

    来源: http://my.oschina.net/alphajay/blog/52172 http://www.cnblogs.com/baiyanhuang/archive/2009/11/09/ ...