HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401
题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋友都可以坐同一张桌,没有关系的则要另开一桌,问需要多少张桌子(小明不坐,不考虑小明与其他人的关系)?
思路:常规的并查集。要求出所有人的老大,有几个老大就要几张桌子。那么有关系的都归为同一个老大。用数组实现,再顺便压缩路径。
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N=;
int pre[N]; //结点i的上级
inline void init(int n){for(int i=; i<=n; i++) pre[i]=i;} //将上级初始化为自己 int find(int x) //找x的上级
{
int k=x;
while(pre[x]!=x) x=pre[x]; //找上级
int tmp;
while(pre[k]!=x) //优化:路径压缩
{
tmp=pre[k];
pre[k]=x;
k=tmp;
}
return x;
} void joint(int a,int b) //将a和b合并为一个集合
{
a=find(a);
b=find(b);
if(a!=b) pre[a]=b;
} int check(int n) //查查到底需要几张桌子
{
set<int> sett;
for(int i=; i<=n; i++) find(i); //防漏网之鱼,将所有人的老大推到最顶。
for(int i=; i<=n; i++) sett.insert(pre[i]);
return sett.size();
}
int main()
{
//freopen("e://input.txt", "r", stdin);
int t, n, m, a, b;
cin>>t;
while(t--)
{
scanf("%d%d", &n, &m);
init(n);
for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
joint(a,b);
}
printf("%d\n",check(n));
}
return ;
}
AC代码
HDU 1213 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 ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- 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 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- 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 题意: 这个问题的一个重要规则是,如果我告诉你A知道B,B知道C,这意味着A,B,C知道对方,所以他们可以 ...
- HDU - 1213 How Many Tables 【并查集】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意 给出N个人 M对关系 找出共有几对连通块 思路 并查集 AC代码 #include < ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
随机推荐
- C# 获取属性字段上DescriptionAttribute的值
var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...
- linux进程管理之服务
init进程首先通过initable查看运行级别,然后运行rc.d下面的sysinit,然后调用rc,然后运行rc###连接到init.d下面的服务.自启动. chkconfig命令只是查看和设置服 ...
- hibernate4.0中SessionFactory的创建
创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...
- iOS开发之ARC&MRC混用
Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式. 如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签. 如果你的项目使用的是 ARC 模 ...
- threaded模式下,比prefork模式要省资源
关于nginx + fastcgi + django 2009-03-10 17:14:43 分类: 系统运维 最近用django开发了一套广告投放系统,这套系统其实是一套网络广告联盟系统,包括广告的 ...
- (int)、Convert.ToInt32()与int.Parse()的区别
1.(int)是类型转换,能够使用(int)进行强类型转换的只能是数值类型,如long.short.double等,这种转换时需要考虑精度问题. 如下的代码就行不通了: string text = & ...
- SPOJ Lexicographical Substring Search 后缀自动机
给你一个字符串,然后询问它第k小的factor,坑的地方在于spoj实在是太慢了,要加各种常数优化,字符集如果不压缩一下必t.. #pragma warning(disable:4996) #incl ...
- POJ2004 Mix and build Trie树? dp?
学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...
- Kibana
https://github.com/moonstack/moon-kibana.git
- httpclient发送multipart/form-data类型参数和用MultipartRequest接收参数
一.利用HttpClient发送基于Content-Type="multipart/form-data"形式的表单 package com.test.httpclient; imp ...