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.

InputThe 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. 
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 这个题做麻烦了,其实可以直接遍历pre数组,其中pre[i]==i的个数就是所有集合的个数!
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 30000
#define INF 0x3f3f3f3f
/*
并查集
*/
set<int> s;
int pre[MAXN],n,m;
int find(int x)
{
if(x==pre[x])
return x;
else
return pre[x] = find(pre[x]);
}
void mix(int x,int y)
{
int fx = find(x),fy = find(y);
if(fx!=fy)
pre[fy] = fx;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)
pre[i] = i;
s.clear();
for(int i=;i<m;i++)
{
cin>>x>>y;
mix(x,y);
}
int cnt = ;
for(int i=;i<=n;i++)
{
int fi = find(i);
if(!s.count(fi))
{
cnt++;
s.insert(fi);
}
}
cout<<cnt<<endl;
}
return ;
}

C - 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. hdu1213 How Many Tables(并查集)

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. 并查集-F - How Many Tables

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

随机推荐

  1. FTP文件服务器

    import java.io.InputStream; import java.io.Serializable; import lombok.Data; @Data public class FtpB ...

  2. Linux 常规操作指南

    1.修改Linux服务器别名 临时修改: vim /etc/hostname  修改别名 永久修改: vim  /etc/sysconfig/network  添加 HOSTNAME=别名 重启服务器 ...

  3. C++ 类中的3种权限作用范围

    三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员函数访问 #include <iostream> #in ...

  4. 题解报告:hdu 1035 Robot Motion(简单搜索一遍)

    Problem Description A robot has been programmed to follow the instructions in its path. Instructions ...

  5. C# 相关概念

    解决方案 在磁盘上由 .sln 文件表示,是一个或多个相关项目的容器. 例如,如果为 Python 应用程序编写 C++ 扩展,该 C++ 项目可以驻留在同一解决方案中. 解决方案还可以包含 Web ...

  6. DataGridView 动态绑定 CheckBox

    下面演示如何在 DataGridView 中动态绑定 CheckBox: public class Test { /// <summary> /// 构造器 /// </summar ...

  7. [转]ASP .NET MVC 之Entity Framework- code first

    本文转自:http://www.cnblogs.com/tomin/archive/2012/02/29/MVC_EntityFramework.html 最近,用到了ASP.NET  MVC Ent ...

  8. NHibernate3.2学习笔记

    一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.涉及第三方程序集 NHibernate.dll:版本3.2 Iesi.C ...

  9. Quartz.Net学习笔记(2)-简介

    一.Quartz.Net是什么 1.来源 Quartz.Net是一个开源的作业调度框架: 2.下载地址 官网地址:http://www.quartz-scheduler.net/documentati ...

  10. leetcode375 Guess Number Higher or Lower II

    思路: dp. https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/ 实现: class Solution { ...