How Many Tables

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

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
 
  典型的并查集题目,将树结构搞好以后就能得到tables
Accept
//    并查集

import java.util.Scanner;

public class Main{

    static int T;
static int tree[];
static int n;
static int m; public static int findFather(int node){
if(node!=tree[node]){
return tree[node]=findFather(tree[node]);
}
return node;
} public static void main(String args[]){ Scanner reader=new Scanner(System.in);
T=reader.nextInt();
while(T>=1){
n=reader.nextInt();
m=reader.nextInt();
tree=new int[n+1]; for(int i=1;i<=n;i++){
tree[i]=i;
}
int max=n;
for(int i=1;i<=m;i++){
int one=reader.nextInt();
int two=reader.nextInt();
int oneFather=findFather(one);
int twoFather=findFather(two);
if(oneFather!=twoFather){ //父结点不同
if(oneFather>twoFather){ //指向更大的结点
tree[twoFather]=oneFather;
}else{
tree[oneFather]=twoFather;
}
max--;
}
} System.out.println(max);
T--;
}
} }

How Many Tables HDOJ的更多相关文章

  1. Hdoj 1050.Moving Tables 题解

    Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a buildin ...

  2. HDOJ 1050 Moving Tables

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

  3. hdoj 1213 How Many Tables

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

  4. hdoj 1050 Moving Tables【贪心区间覆盖】

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

  5. Hdoj 1213.How Many Tables 题解

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

  6. ACM--移动桌子--贪心--HDOJ 1050--Moving Tables

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Descript ...

  7. 【HDOJ】1050 Moving Tables

    贪心问题,其实我觉得贪心就是合理的考虑最优情况,证明贪心可行即可.这题目没话多久一次ac.这道题需要注意房间号的奇偶性.1 3.2 4的测试数据.答案应该为20. #include <stdio ...

  8. HDOJ并查集题目 HDOJ 1213 HDOJ 1242

    Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...

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

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

随机推荐

  1. .Spark Streaming(上)--实时流计算Spark Streaming原理介

    Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍 http://www.cnblogs.com/shishanyuan/p/474 ...

  2. Python基础语法题库

    引言: 语法练习包括Python基础语法.数据类型.字符编码和简单文件操作等内容. 正文(参考答案附录在题目下方): 1.Python 里用来告知解释器跳过当前循环中的剩余语句,然后继续进行下一轮循环 ...

  3. mysql允许远程登录

    Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...

  4. Python运维开发基础10-函数基础

    一,函数的非固定参数 1.1 默认参数 在定义形参的时候,提前给形参赋一个固定的值. #代码演示: def test(x,y=2): #形参里有一个默认参数 print (x) print (y) t ...

  5. mysql中binlog_format的三种模式

    mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复 ...

  6. LeanCloud

    [Nodejs 访问 LeanCloud] 代码中使用 SDK: var AV = require('avoscloud-sdk') AV.initialize('AppID', ''AppKey) ...

  7. 通过jquery,从json中读取数据追加到html中

    1.下载安装jquery   可通过下面的方法引入在线版本的js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jque ...

  8. socket.io api 总结

    server 端const io = require('socket.io')(8000, { // path: '/socket', pingInterval: 10000, pingTimeout ...

  9. [SCOI2007]修车(建图好题)

    [SCOI2007]修车 https://www.lydsy.com/JudgeOnline/problem.php?id=1070 Time Limit: 1 Sec  Memory Limit:  ...

  10. ECMAScript5新特性之isFrozen、freeze

    对象被冻结后: 1 不能添加属性. 2 不能删除属性. 3 不能修改属性.(赋值) 4 不能修改属性描述符.(会抛异常) var fruit = { name : '苹果', desc : '红富士' ...