How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20974    Accepted Submission(s): 10382

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
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
#define Max 1000+10
int per[Max];
int n,m;
void init()
{
for(int i=;i<=n;i++)
per[i]=i;
}
int find(int a)
{
if(a==per[a])
return a;
/*int root=a,x=a,j;
while(root!=per[root]) root=per[root];
while(x!=per[x])
{
j=per[x];
per[x]=root;
x=j;
}*/
return per[a]=find(per[a]);
}
int unite(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
per[a]=b;
return ;
}
int main()
{
int t,a,b;
freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--)
{
int ans=;
scanf("%d%d",&n,&m);
init();
for(int i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
unite(a,b);
}
for(int i=;i<=n;i++)
if(per[i]==i)
ans++;
printf("%d\n",ans);
}
}
Author
Ignatius.L

How Many Tables(POJ 1213 求连通分量)的更多相关文章

  1. hdu 1213 求连通分量(并查集模板题)

    求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...

  2. poj 2524 求连通分量(并查集模板题)

    求连通分量 Sample Input 10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0Sample Output Case 1: 1Case 2: ...

  3. A - Network of Schools - poj 1236(求连通分量)

    题意:学校有一些单向网络,现在需要传一些文件,1,求最少需要向几个学校分发文件才能让每个学校都收到,2,需要添加几条网络才能在任意一个学校分发都可以传遍所有学校. 分析:首先应该求出来连通分量,进行缩 ...

  4. 并查集---体会以及模板&&How Many Tables - HDU 1213

    定义&&概念: 啥是并查集,就是将所有有相关性的元素放在一个集合里面,整体形成一个树型结构,它支持合并操作,但却不支持删除操作 实现步骤:(1)初始化,将所有节点的父亲节点都设置为自己 ...

  5. POJ 2117 Electricity(割点求连通分量)

    http://poj.org/problem?id=2117 题意:求删除图中任意一个顶点后的最大连通分量数. 思路: 求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的. 这道题目我wa了 ...

  6. POJ 2117 (割点+连通分量)

    题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这 ...

  7. POJ 1523 (割点+连通分量)

    题目链接:http://poj.org/problem?id=1523 题目大意:连通图,找图中割点,并计算切除该割点后,图中的连通分量个数. 解题思路: POJ的数据很弱. Tarjan法求割点. ...

  8. poj 3177 边连通分量

    思路: dfs求出所有点的low值,然后对每个连通分量进行缩点,可以通过low来进行缩点.虽然在同一连通分量里可能存在不同的low值,但这并不影响缩点.将每个连通分量缩为一个点后,只要求出这个缩点后的 ...

  9. G - Strongly connected - hdu 4635(求连通分量)

    题意:给你一个图,问最多能添加多少条边使图仍为不是强连通图,如果原图是强连通输出 ‘-1’ 分析:先把求出连通分量进行缩点,因为是求最多的添加边,所以可以看成两部分 x,y,只能一部分向另外一部分连边 ...

随机推荐

  1. 每次从vss获取文件都是只读

    在 Visual Studio 2008 中,使用 VSS 作为源码管理器,把文件签入后,文件会自动变为只读状态.在公司习惯是在某个开发人员的机器上获取最新的代码后编译,编译完之后服务器上再从 VSS ...

  2. Solr简单部署

    因为公司最近涉及这个项目,想了解一下,以便提供更好的支持.. 参考URL: http://www.centoscn.com/CentosServer/test/2014/1127/4193.html ...

  3. leetcode_question_114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

  4. Mac OS X Mavericks使用手册

    基本信息 作者: 施威铭研究室 出版社:清华大学出版社 ISBN:9787302386018 上架时间:2014-12-30 出版日期:2015 年1月 开本:16 版次:1-1 所属分类: 计算机 ...

  5. NOI2012 Day1

    NOI2012 Day1 随机数生成器 题目描述:给出数列\(X_{n+1}=(aX_n+c)mod m\),求\(X_n mod g\) solution: 矩阵乘法,但数有可能在运算时爆\(lon ...

  6. jQuery Ajax 实例 ($.ajax、$.post、$.get)转

    Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. 推荐一篇不错的jQuery Ajax 实例文章,忘记了可以去看看,地址为:ht ...

  7. POJ1664(简单动态规划)

    #include<iostream> #include<string> #include<cstring> using namespace std; ][]; vo ...

  8. Pythoner | 你像从前一样的Python学习笔记

    Pythoner | 你像从前一样的Python学习笔记 Pythoner

  9. PHP 字符串替换 substr_replace 与 str_replace 函数

    PHP 字符串替换 用于从字符串中替换指定字符串. 相关函数如下: substr_replace():把字符串的一部分替换为另一个字符串 str_replace():使用一个字符串替换字符串中的另一些 ...

  10. Python 获取Facebook用户Friends的爱好类别中的Top10

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...