id=19354" target="_blank" style="color:blue; text-decoration:none">HDU - 1213

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

id=19354" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-block; position:relative; padding:0px; margin-right:0.1em; vertical-align:middle; overflow:visible; text-decoration:none; font-family:Verdana,Arial,sans-serif; border:1px solid rgb(211,211,211); color:blue; font-size:12px!important">Submit Status

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
题目目的非常easy就是让你求有几个集合
所以直接模板飘过
/*
Author: 2486
Memory: 1420 KB Time: 0 MS
Language: G++ Result: Accepted
*/
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1000+5;
int a,b,T,N,M,par[maxn],ranks[maxn];
void init(int sizes) {
for(int i=1; i<=sizes; i++) {
par[i]=i;
ranks[i]=1;
}
}
int find(int x) {
return par[x]==x? x:par[x]=find(par[x]);
}
bool same(int x,int y) {
return find(x)==find(y);
}
void unite(int x,int y) {
x=find(x);
y=find(y);
if(x==y)return ;
if(ranks[x]>ranks[y]) {
par[y]=x;
} else {
par[x]=y;
if(ranks[x]==ranks[y])ranks[x]++;
}
}
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d",&N);
scanf("%d",&M);
init(N);
for(int i=0; i<M; i++) {
scanf("%d%d",&a,&b);
unite(a,b);
}
int ans=0;
for(int i=1; i<=N; i++) {
if(par[i]==i)ans++;
}
printf("%d\n",ans);
}
return 0;
}

 

How Many Tables-并查集的更多相关文章

  1. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  2. C - How Many Tables 并查集

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

  3. hdu1213 How Many Tables(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. POJ-1213 How Many Tables( 并查集 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...

  5. HDU 1213 How Many Tables(并查集,简单)

    题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...

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

    并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...

  7. HDU 1213 How Many Tables 并查集 寻找不同集合的个数

    题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...

  8. hdu1213 How Many Tables 并查集的简单应用

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...

  9. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  10. 并查集-F - How Many Tables

    F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...

随机推荐

  1. Codeforces Round #363 (Div. 2) C dp或贪心 两种方法

    Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...

  2. Django使用js,css等静态文件的时候,出现mime类型问题

    使用adminLTE模板, return render(request, 'AdminLTE/index.html') 的时候报如下错误且页面渲染异常,css没有效果: Resource interp ...

  3. PowerDesigner反向生成数据库模型(MySql篇)

    目录: 数据库的反向生成模型 模型的Cooment注释显示 步骤一:下载odbc驱动并进行安装: (1)下载 mysql-connector-odbc-5.3.4-win32 注意:不管电脑是32位, ...

  4. 汕头市队赛 SRM10 dp只会看规律 && bzoj1766

    dp只会看规律 SRM 10 描述 平面上有n个点(xi,yi),用最少个数的底边在x轴上且面积为S的矩形覆盖这些点(在边界上也算覆盖) 输入格式 第一行两个整数n,S接下来n行每行两个整数xi,yi ...

  5. js5:框架的使用,使框架之间无痕连接

    原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <base target="js4" ...

  6. hdu 1847 Good Luck in CET-4 Everybody! 组合游戏 找规律

    题目链接 题意 有\(n\)张牌,两人依次摸牌,每次摸的张数只能是\(2\)的幂次,最后没牌可摸的人为负.问先手会赢还是会输? 思路 0 1 2 3 4 5 6 7 8 9 10 11 -- P N ...

  7. usb 2.0 支援的速度

    from http://www.usb.org/developers/docs/usb20_docs/ high speed : 480 Mb/s full speed : 12 Mb/s low s ...

  8. Python Challenge 第十四关

    14关页面上是两张图,一张是一个卷面包,一张类似条形码的东西.没任何提示,就看源代码,果然,有一行注释: <!-- remember: 100*100 = (100+99+99+98) + (. ...

  9. Codeforces 898 B.Proper Nutrition

    B. Proper Nutrition   time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. Codeforces Gym101473 F.Triangles-前缀和 (2013-2014 ACM-ICPC Brazil Subregional Programming Contest)

    前缀和. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include< ...