HDU 1213(并查集)
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27812 Accepted Submission(s): 13801
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 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.
5 3
1 2
2 3
4 5
5 1
2 5
4
/*
并查集入门题,无坑点。
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
int fa[maxn];
int T, N, M;
int u, v;
int Find(int u)
{
if(u == fa[u]) return fa[u];
else return fa[u] = Find(fa[u]);
}
void adde(int u, int v)
{
int x = Find(u);
int y = Find(v);
if(x != y) fa[x] = fa[y];
}
int main ()
{
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i = ; i <= N; i++)
fa[i] = i;
for(int i = ; i <= M; i++)
{
scanf("%d%d", &u, &v);
adde(u, v);
}
int ans = ;
for(int i = ; i <= N; i++)
{
if(fa[i] == i) ans++;
}
printf("%d\n", ans);
}
return ;
}
HDU 1213(并查集)的更多相关文章
- hdu 1213(并查集模版题)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
随机推荐
- Spring Boot中使用MongoDB数据库
前段时间分享了关于Spring Boot中使用Redis的文章,除了Redis之后,我们在互联网产品中还经常会用到另外一款著名的NoSQL数据库MongoDB. 下面就来简单介绍一下MongoDB,并 ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- python-cookbook读书笔记
今天开始读<python-cookbook>,书里有许多python优雅的写法,可以作为python的一本进阶书. 感谢译者.项目地址: https://github.com/yidao6 ...
- Sublime快捷键(一)
最近在工作中,遇到的sublime的快捷键,以后再工作中用到的我会稍后增加的~ 快捷键: 1.切换标签页: Ctrl + Tab 切换标签页: Ctrl + Shift + Tab 返回刚切 ...
- 简单的企业会议管理cms后台模板——后台
链接:http://pan.baidu.com/s/1eRAVAka 密码:olr1
- JS 判断手机操作系统代码
还是利用UA, 返回值: ios, android, unknown function getMobileType () { var ua = window.navigator.userAgent.t ...
- Linux SCIM/fcitx/ibus 输入法
现在很多发行版linux一般都是装好scim scim-tables-zh 重启就行 但有时重启后还是不能调用 可以用如下方法: 添加文件: sudo gedit /etc/X11/xinit/xin ...
- Django 自定义分页类
分页类代码: class Page(object): ''' 自定义分页类 可以实现Django ORM数据的的分页展示 输出HTML代码: 使用说明: from utils import mypag ...
- Android的休眠与唤醒
Android 休眠(suspend),在一个打过android补丁的内核中,state_store()函数会走另外一条路,会进入到request_suspend_state()中,这个文件在earl ...
- 64.Minimum Path Sum---dp
题目链接:https://leetcode.com/problems/minimum-path-sum/description/ 题目大意:从左上到右下的路径中,找出路径和最小的路径(与62,63题相 ...