hdoj 1213 How Many Tables
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16616 Accepted Submission(s):
8137
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.
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.
Ignatius needs at least. Do NOT print any blanks.
#include<stdio.h>
int set[2100];
int find(int fa) //找到根节点
{
int ch=fa;
int t;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
int mix(int x,int y) //合并已有人数
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,table,people,s;
scanf("%d",&n);
while(n--)
{
scanf("%d %d",&people,&table);
for(i=1;i<=people;i++)
set[i]=i;
for(i=1;i<=table;i++)
{
scanf("%d %d",&m,&j);
mix(m,j);
}
s=0;
for(i=1;i<=people;i++)
{
if(set[i]==i)
s++; //需要的桌子数
}
printf("%d\n",s);
}
return 0;
}
hdoj 1213 How Many Tables的更多相关文章
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- HDOJ并查集题目 HDOJ 1213 HDOJ 1242
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 1213 How Many Tables(并查集)
传送门 Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Igna ...
- hdu 1213 How Many Tables 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
随机推荐
- C#一个简单下载程序实例(可用于更新)
运行时的界面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- Spring MVC 与 web开发
转载:http://coderbee.net/index.php/java/20140719/959 项目组用了 Spring MVC 进行开发,觉得对里面的使用方式不是很满意,就想,如果是我来搭建开 ...
- php实现自动运行文件
autoexec.php <?php ignore_user_abort(); //即使Client断开(如关掉浏览器),PHP脚本也可以继续执行. set_time_limit(30); // ...
- Unicode汉字编码表
U+ 0 1 2 3 4 5 6 7 8 9 A B C D E F ------------------------------------------ ...
- 摄像头(3)调用系统拍照activity来拍照
import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager ...
- git log
http://git-scm.com/book/zh/v2 https://backlogtool.com/git-guide/tw/contents/ http://gitbook.liuh ...
- JXL读取Excel日期时间不准确
XL读取Excel日期时间多出了8个小时. Cell c = rs.getCell(j, i); if (c.getType() == CellType.DAT ...
- 函数flst_add_last
/********************************************************************//** Adds a node as the last no ...
- UVa 1644 (筛素数 + 二分) Prime Gap
题意: 给出一个整数n,如果n是素数输出0,否则输出它后一个素数与前一个素数的差值. 分析: 首先用筛法把前十万个素数都筛出来,然后放到数组里.用二分找到不大于n的最大的素数的下标,如果这个素数等于n ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...