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 ...
随机推荐
- 【BZOJ】【3158】千钧一发
网络流/最小割 这题跟BZOJ 3275限制条件是一样的= =所以可以用相同的方法去做……只要把边的容量从a[i]改成b[i]就行了- (果然不加当前弧优化要略快一点) /************** ...
- Leetcode#81 Search in Rotated Sorted Array II
原题地址 如果不存在重复元素,仅通过判断数组的首尾元素即可判断数组是否连续,但是有重复元素的话就不行了,最坏情况下所有元素都一样,此时只能通过线性扫描确定是否连续. 设对于规模为n的问题的工作量为T( ...
- [原] perforce 获取本地最近更新的Changelist
获取perforce客户端最后一次sync的changelist, 前提是中间没有任何代码提交: http://stackoverflow.com/questions/47007/determinin ...
- RAD项目结构
- Java学习第一篇:变量,数据类型,运算符,流程控制(简介)
一.安装和配置jdk 1.jdk是什么? (1).jdk全称是Java Development Kit, Java开发工具包; (2).jdk是sun公司开发的; (3).jdk主要包括:jre(Ja ...
- 使用NodeJs,实现数据抓取
学习笔记 前言 近期做一个数据抓爬工具,最开始使用的是C#控制台应用,同时正则表达式去过滤数据,看着还行,可每次运行都依附于.net framework很是不爽,于是想整点其他的方法.本人还是比较喜欢 ...
- linux 查看局域网内ip
$ sudo apt-get install nmap $ nmap -sP 192.168.1.1/24 windows 下直接arp -a就能看到.
- Project Euler 97 :Large non-Mersenne prime 非梅森大素数
Large non-Mersenne prime The first known prime found to exceed one million digits was discovered in ...
- Project Euler 83:Path sum: four ways 路径和:4个方向
Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...
- 欧拉工程第72题:Counting fractions
题目链接:https://projecteuler.net/problem=72 真分数;n/d 当d ≤ 1,000,000时候的真分数有多少个 public class P72{ void run ...