博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDUOJ---1213How Many Tables
阅读量:5059 次
发布时间:2019-06-12

本文共 2578 字,大约阅读时间需要 8 分钟。

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
2 #include
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 else37 {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
View Code

 

转载于:https://www.cnblogs.com/gongxijun/p/3401653.html

你可能感兴趣的文章
8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
一个关于vue+mysql+express的全栈项目(六)------ 聊天模型的设计
查看>>
【知识库】-数据库_MySQL 的七种 join
查看>>
.net 写文件上传下载webservice
查看>>
noSQL数据库相关软件介绍(大数据存储时候,必须使用)
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>
满世界都是图论
查看>>
配置链路聚合中极小错误——失之毫厘谬以千里
查看>>
代码整洁
查看>>
蓝桥杯-分小组-java
查看>>
Java基础--面向对象编程1(类与对象)
查看>>
Android Toast
查看>>
iOS开发UI篇—Quartz2D使用(绘制基本图形)
查看>>
docker固定IP地址重启不变
查看>>
桌面图标修复||桌面图标不正常
查看>>
JavaScript基础(四)关于对象及JSON
查看>>
关于js sort排序方法
查看>>
JAVA面试常见问题之Redis篇
查看>>