How Many Tables HDOJ
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
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.
5 3
1 2
2 3
4 5
5 1
2 5
// 并查集 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的更多相关文章
- Hdoj 1050.Moving Tables 题解
Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a buildin ...
- HDOJ 1050 Moving Tables
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdoj 1213 How Many Tables
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdoj 1050 Moving Tables【贪心区间覆盖】
Moving Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- ACM--移动桌子--贪心--HDOJ 1050--Moving Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descript ...
- 【HDOJ】1050 Moving Tables
贪心问题,其实我觉得贪心就是合理的考虑最优情况,证明贪心可行即可.这题目没话多久一次ac.这道题需要注意房间号的奇偶性.1 3.2 4的测试数据.答案应该为20. #include <stdio ...
- HDOJ并查集题目 HDOJ 1213 HDOJ 1242
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- LOCK TABLES和UNLOCK TABLES与Transactions的交互
LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...
随机推荐
- The requested resource (/) is not available解决办法
The requested resource (/) is not available HTTP Status 404(The requested resource is not available) ...
- Android 最火开发框架 xUtils
xUtils简介 xUtils3 api变化较多, 已转至 https://github.com/wyouflf/xUtils3 xUtils 2.x对Android 6.0兼容不是很好, 请尽快升级 ...
- manacher最长回文序列c++
算法真心读不懂 #include <iostream>#include<string>#include<cstring> using namespace std;c ...
- 【校招面试 之 C/C++】第30题 C++ 11新特性(一)之auto关键字
1.自动类型推断 auto自动类型推断,用于从初始化表达式中推断出变量的数据类型.通过auto的自动类型推断,可以大大简化我们的编程工作.下面是一些使用auto的例子. #include <ve ...
- mysql的外键知识
外键的作用 1.用来约束两张表中的字段 2.外键也可以用来实现一对多 我们先举一个这样的例子,让大家对外键有一个基本的认识 当前我们有一个需求就是,需要创建一张表,这张表要包括“姓名”,“年龄”,“工 ...
- MySQL基本操作之命令行操作
MySQL基础操作 MySQL基础操作--命令行操作
- 8-组成n的1的个数
/* ones时间限制:1000 ms | 内存限制:65535 KB难度:3 描述 Given a ...
- mybatis结合redis实战二级缓存
之前的文章中我们意见分析了一级缓存.二级缓存的相关源码和基本原理,今天我们来分享下了mybatis二级缓存和redis的结合,当然mybatis二级缓存也可以和ehcache.memcache.OSC ...
- sqlserver数据库维护常用sql
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...
- DB2锁机制
相比较Oracle来说,DB2的锁机制麻烦了很多,而且这个麻烦带来的不是性能的上升而是下降,不过如果细致了解的话,只能感慨不愧是数据库理论诞生的公司,在实现数据库理论上比Oracle全面得多. ...