题目链接:

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. lambda表达式不使用委托(delegate) 用FUNC

    lLambda不使用delegate关键字,而使用  Lambda运算符 => goes to l    1.Func<int,string> getInput = (int age ...

  2. Windows 7下将Tomcat Java程序设置为Windows Service

    可以参看以下资料: https://jingyan.baidu.com/article/b2c186c89f5127c46ef6ff08.html http://tomcat.apache.org/t ...

  3. Golang中的坑二

    Golang中的坑二 for ...range 最近两周用Golang做项目,编写web服务,两周时间写了大概五千行代码(业务代码加单元测试用例代码).用Go的感觉很爽,编码效率高,运行效率也不错,用 ...

  4. ASP.NET Core MVC中的 [Required]与[BindRequired]

    在开发ASP.NET Core MVC应用程序时,需要对控制器中的模型校验数据有效性,元数据注释(Data Annotations)是一个完美的解决方案. 元数据注释最典型例子是确保API的调用者提供 ...

  5. copy&deepcopy

    import copy 字典参照列表结论,看是否有深层嵌套. a = {'name':1,'age':2} b = a a['name'] = 'ff' print(a) print(b) print ...

  6. windows的ReactNative挖坑一分钟爬坑一小时

    其实开发并不需要Android Studio来开发,因为命令行都是要自己手打的,所以就开始了我的挖坑爬坑之旅 首先安装React Native要用到的git.nodejs等等这里不讲了,主要讲在手机上 ...

  7. Use Zabbix Monitor Find ‘DBCC CheckDB’ Problem

    下面是修改前后的对比截图: 如下图: 下图是确定问题并修改后对比图,左边圈是修改前,右边圈是修改后对比截图:当看到周期性的性能指数,一般是计划性任务引起:通过DMV视图,找到引起等待的原因检查数据库完 ...

  8. Python day 3 (3)

    一:判断语句: 1  if 语句 : 2  或者if 语句 : else : 3  或者if 语句 : elif 语句 : else : 4注意:的使用,缩进一般用4个空格来完成. 二:input 语 ...

  9. 理解纯CSS画三角形

    pure css draw a triangle code { display: inline-block; width: 300px; background-color: #E0E0E0 } .te ...

  10. dubbo filter链构建过程

    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException { if (Consta ...