How Many Tables 简单并查集
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.
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.
OutputFor 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
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
int n , m ;
int f[] ;
void init()
{
for(int i = ; i <= n ; i++)
{
f[i] = i ;
}
}
int getf(int x)
{
if(f[x] != x)f[x]=getf(f[x]);
return f[x] ;
}
int merge(int x,int y)
{
f[getf(y)]=getf(x);
}
int main()
{
int T,x,y,c;
scanf("%d" , &T);
while(T--)
{
c=;
scanf("%d%d",&n,&m);
init();
for(int i = ; i <= m ; i++)
{
scanf("%d %d",&x,&y);
merge(x,y);
}
for(int i = ; i <= n ; i++)
if(f[i]==i)c++;
printf("%d\n",c);
}
}
How Many Tables 简单并查集的更多相关文章
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU——1213How Many Tables(并查集按秩合并)
J - How Many Tables Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- Python mysql-数据库基础知识
2017-09-05 19:10:55 一.数据库定义 从本质上讲,数据库就是信息的集合,它可以存在很长时间,往往是很多年.一般来讲,"数据库"这个词指的是有数据库管理系统管理的数 ...
- Java EE、Java SE和Java ME
Java SE=Java Standard EditionJava EE=Java Enterprise EditionJava ME=Java Mobile Edition SE主要用于桌面程序,控 ...
- Java 访问控制关键字
public, private, protected 在控制上有什么区别和不同请参考下面的说明. 请参考下图的说明. 和下面的一个说明: │ Class │ Package │ Subclass │ ...
- Enduring Exodus CodeForces - 655C (二分)
链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...
- IE6中CSS常见BUG全集及解决方案——摘自网友
IE6中CSS常见BUG全集及解决方案 IE6双倍边距bug 当页面内有多个连续浮动时,如本页的图标列表是采用左浮动,此时设置li的左侧margin值时,在最左侧呈现双倍情况.如外边距设置为10px, ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- jsp jsp常用指令
jsp指令是为jsp引擎设计的,他们并不直接产生任何可见输出,而只是告诉引擎如何处理jsp页面中的其余部分. jsp中的指令 page指令 include指令 taglib指令 jsp指令的基本语法 ...
- install rabbitvcs in ubuntu16.04
reference: https://github.com/rabbitvcs/rabbitvcs how to install : sudo apt-get install rabbitvcs-cl ...
- AE项目打包
Holinz AE项目打包 打包详细信息:Setup Factory 7.0打包软件,VS2005+AE92下的Winform项目1.依赖项: Dot Net Framework20 AO ...
- 在ant编译java文件时产生debug信息
使用ant编译java文件时,如果没有设置debug属性,则不会产生编译信息,ant的默认设置是不打印编译信息. 如果想在编译过程中显示编译信息,需设置debug属性为true,并且设置debugLe ...