HDUOJ---1213How Many Tables
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9828 Accepted Submission(s): 4872
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
Author
Ignatius.L
Source
代码:
并查集,模板的运用..
1 #include<stdio.h>
2 #include<string.h>
3 #define maxn 1005
4 int father[maxn+1],rank[maxn+1];
5
6 /*³õʼ»¯*/
7 void init(int n)
8 {
9 for(int i=1;i<=n;i++)
10 {
11 father[i]=i;
12 rank[i]=1;
13 }
14 }
15
16 /*²é*/
17 int setfind(int x)
18 {
19 if(father[x]==x)
20 return x;
21 return setfind(father[x]);
22 }
23
24 /*²¢*/
25 void Union(int a,int b)
26 {
27 int x=setfind(a);
28 int y=setfind(b);
29 if(x!=y)
30 {
31 if(rank[x]>rank[y])
32 {
33 father[y]=x;
34 rank[x]+=rank[y];
35 }
36 else
37 {
38 father[x]=y;
39 rank[y]+=rank[x];
40 }
41 }
42 }
43
44 int main()
45 {
46 int t,m,n,i,a,b;
47 /* freopen("test.in","r",stdin);
48
49 freopen("test.out","w",stdout);
50 */
51 scanf("%d",&t);
52 while(t--)
53 {
54 scanf("%d%d",&n,&m);
55 init(n);
56 for(i=0;i<m;i++)
57 {
58 scanf("%d%d",&a,&b);
59 Union(a,b);
60 }
61 int cnt=0;
62 for(i=1;i<=n;i++)
63 if(father[i]==i)
64 cnt++;
65 printf("%dn",cnt);
66 }
67 return 0;
68 }
- Ajax上传图片以及上传之前先预览
- Spring Cloud中Hystrix的服务降级与异常处理
- Open vSwitch源码解析之基于VxLAN实现NSH解析功能
- Spring Cloud自定义Hystrix请求命令
- JavaScript面试问题:事件委托和this
- Spring Cloud中的断路器Hystrix
- js的隐含参数(arguments,callee,caller)使用方法
- Spring Cloud中的负载均衡策略
- Spring Cloud中负载均衡器概览
- 没有event loop的PHP
- RestTemplate的逆袭之路,从发送请求到负载均衡
- limit_area_cirle
- Spring RestTemplate中几种常见的请求方式
- 什么是客户端负载均衡
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [DeeplearningAI 笔记]第一章2.11-2.16 向量化与 python/numpy 向量说明
- 自己动手作图深入理解二叉树、满二叉树及完全二叉树
- Matlab矩阵加入新元素
- 每个Python程序员都应该知道的10个缩写
- 《剑指offer》第九天:斐波那契数列
- [tensorflow损失函数系列]sigmoid_cross_entropy_with_logits
- 0794-5.16.2-Hive和Imapla查询decimal类型结果不同异常
- 利用TFRecords存储于读取带标签的图片
- matlab sum函数
- [tensorflow损失函数系列]softmax_cross_entropy_with_logits
- RESTful API 设计最佳实践
- Spring 是如何解决循环依赖的?
- 移动webhead参数
- 看了这篇泛型,下次设计链表别傻傻的用int 表示node节点的值了
- 标准TensorFlow格式 TFRecords