How Many Tables

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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<stdio.h>
int father[1005],depth[1005];
void init()
{
    int i;
    for(i = 1; i < 1005;i ++)
    {
        father[i] = i;
        depth[i] = 0;
    }
} int find(int x)
{
    if(x==father[x])
        return x;
    else
        return father[x] = find(father[x]);
} void unit(int x,int y)
{
    x = find(x);
    y = find(y);
    if(x==y)
        return ;
    if(depth[x]<depth[y])
    {
        father[x] = father[y];     }
    else
    {
        if(depth[x]>depth[y])
            father[y] = father[x];
        else
        {
            father[x] = father[y];
            depth[y]++;
        }
    }
} int main()
{
    int T,n,m,gr,j,a,b;
    scanf("%d",&T);
    while(T--)
    {
        gr = 0;
        init();
        scanf("%d%d",&n,&m);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            unit(a,b);
        }
        for(j = 1;j <= n;j ++)
        {
            if(j == father[j])
                gr++;
        }
        printf("%d\n",gr);
    }
    return 0;
}

How Many Tables的更多相关文章

  1. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  2. 函数的使用顺序---TABLES,USING,CHANGING

    SAP使用PERFORM的时候: ... [TABLES   itab1 itab2 ...]     [USING    a1 a2 ...]     [CHANGING a1 a2 ...]. E ...

  3. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

  4. mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES

    AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...

  5. Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  6. Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)

    传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...

  7. mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)

    mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...

  8. MySql: show databases/tables use database desc table

    1. show databases mysql> show databases;+--------------------+| Database |+--------------------+| ...

  9. mysql performance_schema 和information_schema.tables了解

    这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql  information_schema 信息相关  performance_schema 性能相关 ...

  10. responsive tables

    以上内容原本是整理为ppt格式的,贴过来格式有点乱,请见谅. 其他responsive tables参考: http://gergeo.se/RWD-Table-Patterns/ 3种类型的代码参考 ...

随机推荐

  1. 恶心的学校机房SQL安装

    学校机房每台PC(DELL OPTIPLEX 380)上有两个系统,分别对应XP中英文版.管理员将500G硬盘分为两部分(两个主分区,两个逻辑分区),每个系统占用一个主分区和一个逻辑分区,主分区都有冰 ...

  2. ViewState压缩

    /// <summary> ///CompressViewState 的摘要说明 /// </summary> public class CompressViewState:S ...

  3. sprintf函数php的详细使用方法

    PHP sprintf() 函数 先说下为什么要写这个函数的前言,这个是我在微信二次开发的一个token验证文档也就是示例文档看到的一个函数,当时非常不理解,于是查了百度,但是很多结果都很笼统,结果也 ...

  4. Yii Query Builder insert()、update()、delete()使用

    Yii自带的query builder还是很好用的,省去了拼sql的过程,今天在写一个语句的时候遇到这样一个问题 $connection = Yii::app()->db; $command = ...

  5. 【方言】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

    Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 几种 方言配置差异 <?xml v ...

  6. Random.Next获取随即数

    Random random = new Random(); random.Next()--------------返回非负的一个随机数. random.Next(int  maxValue)----- ...

  7. ASP.NET MVC Spring.NET NHibernate 整合

    请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...

  8. Linux安装包

    关于SWT SWT首先要在Eclipse中添加SWT的安装包:Windowsbuilder Pro.下载路径:http://www.eclipse.org/windowbuilder/download ...

  9. ThinkPHP框架安全性能分析

    http://www.freebuf.com/articles/web/59713.html 点击劫持cookie 点击劫持所有链接

  10. 6 款国外开源web oa办公系统(转)

    国外的开源产品较多,而且大多提供免费的社区版本,oa办公系统也不例外. 1.eGroupware eGroupware是一个多用户,在以PHP为基础的API上的定制集为基础开发的,以WEB为基础的工作 ...