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 ...
随机推荐
- 软件工程导论复习 如何画系统流程图和数据流图 part1
一.数据流图与流程图的区别 数据流程图是以图形的方式表达在问题中信息的变换和传递过程.它把系统看成是由数据流联系的各种概念的组合,用分解及抽象手段来控制需求分析的复杂性,采用分层的数据流程图来表示一个 ...
- 扩展C#与元编程(二)
如果你对Windows Workflow Foundation(WF)一无所知,当看到扩展C#与元编程(一)中由MW编译器生成的FirstLook.mw.cs时,也许这么在想:我KAO,这是C#版的汇 ...
- Linux下启动Tomcat启动并显示控制台日志信息
Linux下如何启动Tomcat像Windows启动并显示控制台日志信息一样? Windows下启动tomcat,一般直接运行startup.bat,启动后如下图所示: Linux下直接启动./sta ...
- org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"", local:"werks")。所需元素为(none)
警告: Interceptor for {http://impl.service.ws.cxf.com/}WsStkoServiceImplService#{http://service.ws.cxf ...
- Hibernate 序列生成主键
@Entity @Table(name="TABLE_NAME") public class TableName implements Serializable { @Id @Ge ...
- cf451C-Predict Outcome of the Game
http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS ...
- np.random.random()系列函数
1.np.random.random()函数参数 np.random.random((1000, 20)) 上面这个就代表生成1000行 20列的浮点数,浮点数都是从0-1中随机. 2.numpy.r ...
- 转)Ubuntu安装mysql5.7
主要参考http://blog.csdn.net/q894523017/article/details/50705392 包去官网下载,解压,安装步骤如下: 上文中有错误,正确如下: sudo dpk ...
- centos自定义安装pip3
题记 在之前的文章centos云服务器安装Python3记录 记录了怎么自定义安装 Python3 ,在后边测试pip3的时候发现了个问题: pip --version terminal 打印: pi ...
- asp.net core web 本地iis开发
发布后打开没有问题,直接配置到本地iis时,会提示如下错误 HTTP Error 502.5 - Process Failure