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. php实现单链表

    <?php /** * 单链表 */ class Demo { private $id; public $name; public $next; public function __constr ...

  2. DELPHI关于文件的操作

    caption:= ExtractFileExt(‘带路径,带扩展名的文件名’);//返回的就是扩展名 caption:=  ChangeFileExt(ExtractFileName(‘带路径,带扩 ...

  3. mirantis fuel puppet执行顺序 和 对整个项目代码的执行流程理解

    stage执行顺序 stage {'zero': } -> stage {'first': } -> stage {'openstack-custom-repo': } -> sta ...

  4. Angular2-之开发环境搭建/调试环境配置

    很多网友发表了学习Angualr2的方法,今天我也分享我的学习过程,这篇文章暂且谈谈用到的工具. 1. Visual Studio Code 2. NodeJs 如果你不在天朝请直接去第三步.否则你还 ...

  5. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  6. nutch 采集到的数据与实际不符

    现象,这个网站我总计能抽取将近500个URL,但实际只抽取了100条 解析:nutch默认从一个页面解析出的链接,只取前 100 个. <property> <name>db. ...

  7. [XJOI NOI2015模拟题13] A 神奇的矩阵 【分块】

    题目链接:XJOI NOI2015-13 A 题目分析 首先,题目定义的这种矩阵有一个神奇的性质,第 4 行与第 2 行相同,于是第 5 行也就与第 3 行相同,后面的也是一样. 因此矩阵可以看做只有 ...

  8. jmeter linux使用经验小结

    1. 确认务必安装了JDK,并且把路径配置OK.否则执行会报错. 2. 当做负载机时,在hosts 配置上    你的ip   你的hostname  或者使用./bin/jmeter-server ...

  9. nginx 配置的server_name参数(转)

    转自:http://www.sklinux.com/373 nginx中的server_name指令主要用于配置基于名称虚拟主机. 一 匹配顺序,server_name指令在接到请求后的匹配顺序如下: ...

  10. [dp]Codeforces30C Shooting Gallery

    题目链接 题意: 给n个点 每个点的坐标 x y 出现的时间t 射中的概率 从i点到j点的时间为它们的距离. 求射中个数的最大期望 很水的dp  坑点就是要用LL #include <cstdi ...