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 ...
随机推荐
- javascript中this之说
this是在运行时基于函数的执行环境绑定的:在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象.不过,匿名函数的执行环境具有全局性,因此其this对象通常 ...
- gtftools软件简单介绍(我自己不建议用,因为我发现不好用)
1)背景 生物信息学研究经常涉及计算或提取基因的各种特征,如基因ID作图,GC含量计算和不同类型的基因长度,通过操纵基因模型,这些模型通常以GTF格式注释,可从ENSEMBL或GENCODE数据库获得 ...
- Python错误:close failed in file object destructor
我遇到的情况: 二进制程序调shell再调Python后,shell退出,Python进程挂到init上(不是僵尸进程),但 此时二进制程序未退出,这时候中断而二进制程序出现此提示. 经查询: 应该是 ...
- 对象之int介绍
#Auther Bob #--*--conding:utf-8 --*-- #创建两个int的对象,age1和age2 age1 = 10 age2 = int(1) #查看对象的类 print(ty ...
- 300最长上升子序列 · Longest Increasing Subsequence
[抄题]: 往上走台阶 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的. 样例 给出 [5,4,1,2,3],LIS 是 [1,2 ...
- 移动端UI设计规范模板参考以及设计规范的好处
2018也快要过完了(-_-),我们的移动端的UI设计规范也层出不穷.很多APP设计师也要在年底给公司或者是团队做一个总结.那么一个像样的APP ui设计规范也是很有必要的作品回顾. 在创业公司做着一 ...
- LNMP 网站搭建
https://lnmp.org/ lnmp这个一键安装:https://lnmp.org/install.html wget -c http://soft.vpser.net/lnmp/lnmp1. ...
- scrapy官方安装方法
安装依赖 sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libss ...
- JSONModel简便应用
JSONModel, Mantle 这两个开源库都是用来进行封装JSON->Model的, 想想看, 直接向服务器发起一个请求,然后回来后,就是一个Model, 直接使用, 这是一个多么美好的事 ...
- AJAX学习必备三本书
<AJAX基础教程>AJAX必备图书之一.国内发行的第一本AJAX图书,也是目前最好的AJAX入门书,如果您是AJAX新手,此书是最好的入门图书.本书基本包括了实现Ajax需要了解的大部分 ...