题目链接:

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. git pull与git fetch的区别

    git pull: 取回远程主机某个分支的更新,再与本地的指定分支合并. 用法: git pull <远程仓库> <远程分支名>:<本地分支名> // 如 git ...

  2. 【http转https】其之二:申请Let's Encrypt颁发SSL证书

    文:铁乐猫 2017年1月12日 申请Let's Encrypt颁发SSL证书 由 ISRG(Internet Security Research Group,互联网安全研究小组)提供服务, ISRG ...

  3. SVN添加用户权限

    点击properties

  4. 房上的猫:java基础知识部分知识点

    1.Java常见的注释有哪些,语法是怎样的? 1)单行注释用//表示,编译器看到//会忽略该行//后的所文本  2)多行注释/* */表示,编译器看到/*时会搜索接下来的*/,忽略掉/* */之间的文 ...

  5. Nginx优化use参数epoll,kqueue,rtsig,eventport,poll

    转自:http://blog.sina.com.cn/s/blog_5eaf88f10100gkrq.html Nginx use参数分析对比 下图对比了poll select epoll和kqueu ...

  6. angular4.0中form表单双向数据绑定正确姿势

    issue:用[(ngModel)]="property"指令双向数据绑定,报错. reason1:使用ngModel绑定数据需要注入FormsModule模块,在app.modu ...

  7. yield next和yield* next的区别

    yield next和yield* next之间到底有什么区别?为什么需要yield* next?经常会有人提出这个问题.虽然我们在代码中会尽量避免使用yield* next以减少新用户的疑惑,但还是 ...

  8. [Spark内核] 第29课:Master HA彻底解密

    本课主题 Master HA 解析 Master HA 解析源码分享 [引言部份:你希望读者看完这篇博客后有那些启发.学到什么样的知识点] 更新中...... Master HA 解析 生产环境下一般 ...

  9. docker with flannel

    ** 原创文章,请勿转载 ** docker的单host,多container环境下,是使用host的docker0网桥进行通信的.如果跨host, container之间要通信怎么办呢?答案是fla ...

  10. 利用appium-1.5.3.dmg安装Appium. doctors时,提示 Could not detect Mac OS X Version from sw_vers output: '10.12'

    发生这种错误的原因是因为:appium不支持mac 10.12版本. 解决方法: https://stackoverflow.com/questions/40129794/how-to-fix-err ...