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 BeautifulSoup的使用
2017-07-24 22:39:14 Python3 中的beautifulsoup引入的包是bs4 import requests from bs4 import * r = requests.g ...
- Java基础十一--多态
Java基础十一--多态 一.多态定义 简单说:就是一个对象对应着不同类型. 多态在代码中的体现: 父类或者接口的引用指向其子类的对象. /* 对象的多态性. class 动物 {} class 猫 ...
- 筛选datatable
当从数据库里取出一些数据,然后要对数据进行整合,很容易就会想到: DataTable dt = new DataTable();//假设dt是由"SELECT C1,C2,C3 FROM T ...
- English trip -- MC(情景课)3 D
xu言: have a nice weekend... sentences How many people are there in you family? they are 3 people in ...
- 【模板/经典题型】带有直线限制的NE Latice Path计数
平移一下,变成不能接触y=x+1. 注意下面的操作(重点) 做点p=(n,m)关于这条直线的对称点q=(m-1,n+1). ans=f(p)-f(q). 其中f(x)为从(0,0)到点x的方案数.
- sql表联接
1.join: 表:erp_orders和erp_orders_products SELECT * FROM erp_orders_products AS products, erp_orders A ...
- dp入门求最大公共子序列
#include "bits/stdc++.h" using namespace std; ],b[]; ][]; int main() { cin >> a > ...
- arc路径例子-磊哥
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> & ...
- oracle12c中新能优化新特性之热度图和自动数据优化
1. Oracle12c热度图和自动数据优化 信息生命周期管理(ILM)是指在数据生命周期内管理它们的策略.依赖于数据的年龄和对应用的业务相关性,数据能被压缩,能被归档或移到低成本的存储上.简言之,I ...
- SQL基础用法(实例一)
/* 2006年10月01日 SQL Server 数据库的基本操作 (1) 数据库的创建 (2) 数据表的创建以及相关约束的指定(含临时表) (3) 数据的添/删/改 (4) 数据的查询 */ () ...