How Many Tables(并查集)
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17411 Accepted Submission(s):
8527
点我
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 <iostream>
#include <cstdio>
using namespace std;
int f[];
int find(int x)//查找x的祖先结点
{
while(x!=f[x])
x=f[x];
return x;
}
int merge(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)//判断他们是不是在一个集合里
f[fx]=fy;//合并
}
int main()
{
int n;
freopen("in.txt","r",stdin);
cin>>n;
while(n--)
{
int num,i,j,rel,a,b,count=;
cin>>num>>rel;
for(i=;i<=num;i++)
f[i]=i;
for(i=;i<rel;i++)
{
cin>>a>>b;
merge(a,b);
}
for(i=;i<=num;i++)
{
if(f[i]==i)
count++;
}
if(n!=)
getchar();
cout<<count<<endl;
}
}
How Many Tables(并查集)的更多相关文章
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- hdu1213 How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- POJ-1213 How Many Tables( 并查集 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu1213 How Many Tables 并查集的简单应用
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单的并查集 代码: #include<iostream> #include< ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- 并查集-F - How Many Tables
F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...
随机推荐
- nginx之依据IP做限制
环境如下: [root@localhost ~]# cat /etc/issueCentOS release 6.5 (Final)Kernel \r on an \m[root@localhost ...
- EF 拉姆达 动态拼接查询语句
EF 动态拼接查询语句 using System; using System.Collections.Generic; using System.IO; using System.Linq; usin ...
- Unity3D自定义地形的笔刷,刷出别样地形
是不是很简单呀,大家可以发挥想象刷出特殊的地形,小鸡呀,或者其他的logo之类(顶视图看上去效果很棒)的地形. 最后把我找的笔刷上传,Gizmos 注意: 如果文件夹及图片导入后,地形系统的笔刷无 ...
- scheme代码高亮
(defun iedit-symbol-in-defun () "Enter `iedit-mode' to rename the symbol in current function, o ...
- springMVC+ freemark多视图配置
<!--通用视图解析器--> <bean id="viewResolverCommon" class="org.springframework.web. ...
- HtmlAgilityPack - 简介
HtmlAgilityPack是.net下的一个HTML解析类库.支持用XPath来解析HTML.这个意义不小,为什么呢?因为对于页面上的元素的xpath某些强大的浏览器能够直接获取得到,并不需要手动 ...
- C# 计划任务
计划任务 : 个人理解, 就是后台控制程序要求在一定的时间执行相应的任务. 直接上代码: 之前,因为工作需要. 要求每天在凌晨2:00,执行要做的数据 1.1 声明一个接口 Ijob 方法: 执行 ...
- 国际化标签 <fmt:bundle>&<fmt:message>的使用
国际化标签 <fmt:bundle>&<fmt:message>的使用 Message.properties文件: name=www.gis520.com #info= ...
- HDOJ-1017 A Mathematical Curiosity(淼)
http://acm.hdu.edu.cn/showproblem.php?pid=1017 # include <stdio.h> int find(int n, int m) { in ...
- android中通过自定义xml实现你需要的shape效果 xml属性配置
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的 ...