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. 软件工程导论复习 如何画系统流程图和数据流图 part1

    一.数据流图与流程图的区别 数据流程图是以图形的方式表达在问题中信息的变换和传递过程.它把系统看成是由数据流联系的各种概念的组合,用分解及抽象手段来控制需求分析的复杂性,采用分层的数据流程图来表示一个 ...

  2. 扩展C#与元编程(二)

    如果你对Windows Workflow Foundation(WF)一无所知,当看到扩展C#与元编程(一)中由MW编译器生成的FirstLook.mw.cs时,也许这么在想:我KAO,这是C#版的汇 ...

  3. Linux下启动Tomcat启动并显示控制台日志信息

    Linux下如何启动Tomcat像Windows启动并显示控制台日志信息一样? Windows下启动tomcat,一般直接运行startup.bat,启动后如下图所示: Linux下直接启动./sta ...

  4. org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"", local:"werks")。所需元素为(none)

    警告: Interceptor for {http://impl.service.ws.cxf.com/}WsStkoServiceImplService#{http://service.ws.cxf ...

  5. Hibernate 序列生成主键

    @Entity @Table(name="TABLE_NAME") public class TableName implements Serializable { @Id @Ge ...

  6. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...

  7. np.random.random()系列函数

    1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...

  8. 转)Ubuntu安装mysql5.7

    主要参考http://blog.csdn.net/q894523017/article/details/50705392 包去官网下载,解压,安装步骤如下: 上文中有错误,正确如下: sudo dpk ...

  9. centos自定义安装pip3

    题记 在之前的文章centos云服务器安装Python3记录 记录了怎么自定义安装 Python3 ,在后边测试pip3的时候发现了个问题: pip --version terminal 打印: pi ...

  10. asp.net core web 本地iis开发

    发布后打开没有问题,直接配置到本地iis时,会提示如下错误 HTTP Error 502.5 - Process Failure