题目链接:

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

Problem Description
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want 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.

 
Input
The
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.
 
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 
Sample Input
2
5 3
1 2
2 3
4 5
 
 
5 1
2 5
 
Sample Output
2
4
 题意描述:
输入人数和关系数以及关系
计算并输出有多少个独立团体
解题思路:
处理数据,使用并查集算法即可。
AC代码:
 #include<stdio.h>
void merge(int u,int v);
int getf(int v);
int f[];
int main()
{
int T,n,m,a,b,i,sum;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
f[i]=i;
while(m--)
{
scanf("%d%d",&a,&b);
merge(a,b);
} for(sum=,i=;i<=n;i++)
{
if(f[i]==i)
sum++;
}
printf("%d\n",sum);
}
return ;
}
void merge(int u,int v)
{
int t1,t2;
t1=getf(u);
t2=getf(v);
if(t1 != t2)
f[t2]=t1;
}
int getf(int v)
{
if(f[v]==v)
return v; return f[v]=getf(f[v]);
}

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

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

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

  2. hdu 1213 How Many Tables(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...

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

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

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

    How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...

  5. hdu 1213 How Many Tables(并查集练习)

    题目链接:hdu1213 赤裸裸的并查集.....水题一个.... #include<stdio.h> #include<string.h> #include<algor ...

  6. HDU 1213 How Many Tables(并查集)

    传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...

  7. HDU 1213 How Many Tables(并查集裸题)

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  8. HDU 1213 How Many Tables【并查集】

    解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  9. hdu 1213 How Many Tables(并查集求无向图有几个连通分量)

    代码: #include<cstdio> #include<cstring> using namespace std; int n,m; int father[1005]; i ...

  10. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

随机推荐

  1. ES6 let和const命令(4)

    const声明的常量只在当前代码块有效.如果想设置跨模块的常量,可以采用下面的写法. //constants.js模块 export const A = 1; export const B = 3; ...

  2. 《Django By Example》Chap 4中出现的 “RelatedObjectDoesNotExist”错误

    models.py

  3. flask 部署后并发测试

    部署后看下flask支持的并发,来个300并发看看,上代码 import threading, time, requests url = "http://www.baidu.com" ...

  4. js 向form表单中插入数据

    var newElement = document.createElement("input"); var nowtime=year+""+month+day+ ...

  5. ADO.NET查询和操作数据库

    stringbuilder 类 stringbuilder类:用来定义可变字符串 stringbulider Append(string value)   在结尾追加 stringbuilder in ...

  6. JavaScript、ES5和ES6的介绍和区别

    JavaScript由三部分组成: ECMAScript(核心) DOM(文档对象模型) BOM (浏览器对象模型) ES5(ECMAScript第五个版本) strict模式 严格模式,限制一些用法 ...

  7. TurnipBit开发板掷骰子小游戏DIY教程实例

    转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 0x00前言 下面带大家用TurnipBit开发板实现一个简单的小游戏- ...

  8. 关于php变量的赋值和引用的区别

    刚开始学习php,发现有些地方和js语法不同,所以记录下来. 这篇文章是总结php中变量赋值和引用的区别. 我们知道,js中,原始类型的赋值,是将值直接复制给变量:引用类型的赋值,是将内存地址复制给变 ...

  9. Android Studio 查看手机CPU信息

    在Android开发中,我们想要获取手机是什么CPU架构,可以通过下面方式: 1.进入adb 终端 adb shell 2.进入proc目录 cd /proc/ 3.查看cpu信息 cat cpuin ...

  10. Java集合系列[1]----ArrayList源码分析

    本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组.数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间用来进行元素的存储,由于它直接操作内存,所以数组的性能要比集 ...