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. Universal-Image-Loader(UIL)使用方法&流程图&源码分析 ----- 未完

    GitHub源码: Android-Universal-Image-Loader Features Multithread image loading (async or sync) 多线程加载(同步 ...

  2. VC++2010添加菜单

    1  资源视图下面右键添加资源 选择menu 2  创建你想要的menu 3  找到CDialog::OnInitDialog();在后面添加代码. CMenu menu; menu.LoadMenu ...

  3. RasAPI函数实现PPPOE拨号

    unit uDial; interface uses Windows,Messages, SysUtils, Ras;// Classes; var //EntryName,UserName,Pass ...

  4. 开发设计模式(八)抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式主要有以下角色: 抽象工厂角色:担任这个角色的是工厂方法模式的核心,它是与应用系统的商业逻辑无关的.通常使用接口或抽象类实现.    具体工厂角色:这个角色直接在客户端的调用下创建产品的实 ...

  5. componentsJoinedByString 和 componentsSeparatedByString 的方法的区别

    将string字符串转换为array数组 NSArray  *array = [Str componentsSeparatedByString:@","]; 将array数组转换为 ...

  6. DB天气app冲刺第七天

    今天估计得分应该是最差的了. 今天因为完全没准备所以在界面UI上面根本就是相当于没弄.结果今天的各组报告里自己做得很不好.不过好在只是项目的中间期,不碍大事. 今天也完成了任务.可以说超额了也.因为吃 ...

  7. Java练习题

    1.实现一个类似于ConcurrentHashMap的分段加锁 import java.util.HashMap; import java.util.Map; import java.util.con ...

  8. 关于 js 2个数组取差集怎么取

    关于 js 2个数组取差集怎么取? 例如求var arr1 = [1]; var arr2 = [1,2];的差集方法一: Array.prototype.diff = function(a) { r ...

  9. Automotive Security的一些资料和心得(8):Hardware Security Module (HSM)

    1. Introduction - 保护软件的安全性措施,作为值得信赖的安全锚,- 安全地生成,存储和处理安全性关键材料屏蔽任何潜在的恶意软件,?- 通过运用有效的限制硬件篡改攻击的可能性篡改保护措施 ...

  10. Android中数据库Sqlite的性能优化

    1.索引简单的说,索引就像书本的目录,目录可以快速找到所在页数,数据库中索引可以帮助快速找到数据,而不用全表扫描,合适的索引可以大大提高数据库查询的效率.(1). 优点大大加快了数据库检索的速度,包括 ...