hdu 1213(并查集模版题)
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27455 Accepted Submission(s): 13656
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<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int parent[N];
int find(int x){
int r=x;
while(r!=parent[r])r=parent[r];
int i=x;
int j;
while(parent[i]!=r){
j=parent[i];
parent[i]=r;
i=j;
}
return r;
}
void Union(int x,int y){
x=find(x);
y=find(y);
if(x<y)parent[x]=y;
else if(x>y)parent[y]=x;
if(x==y)return;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m;
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)parent[i]=i;
for(int i=;i<=m;i++){
cin>>x>>y;
Union(x,y);
}
int ans=;
for(int i=;i<=n;i++){
if(parent[i]==i)ans++;
}
cout<<ans<<endl;
}
}
hdu 1213(并查集模版题)的更多相关文章
- HDU 1213(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【poj 1962】Corporative Network(图论--带权并查集 模版题)
P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...
- HDU 1232 并查集板子题
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...
- 畅通工程 HDU - 1232 并查集板子题
#include<iostream> #include<cstring> using namespace std; ; int p[N]; int find(int x) { ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
随机推荐
- React Native导航器Navigator
React Native导航器Navigator 使用导航器可以让你在应用的不同场景(页面)间进行切换.导航器通过路由对象来分辨不同的场景.利用renderScene方法,导航栏可以根据指定的路由来渲 ...
- PHP基础知识测试题及解析
本试题共40道选择题,10道判断题,考试时间1个半小时 一:选择题(单项选择,每题2分): 1. LAMP具体结构不包含下面哪种(A ) A:Windows系统 B:Apache服务器 C:MyS ...
- 查看/进入mac根目录的方式
1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...
- IP访问频率限制不能用数组循环插入多个限制条件原因分析及解决方案
14.IP频率限制不能用数组循环插入多个限制条件原因分析及解决方案: define("RATE_LIMITING_ARR", array('3' => 3, '6' => ...
- CAD得到自定义实体拖放夹点(com接口VB语言)
主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::getGripPoints 自定义实体事件,得到拖放夹点,详细说明如下: 参数 说明 LONGLO ...
- 常见的CPU指令集介绍
本文摘自网络 一.X86 是微处理器执行的计算机语言指令集,指一个intel通用计算机系列的标准编号缩写,也标识一套通用的计算机指令集合,属于CISC. 1.1.简介 X86指令集是美国Intel ...
- Tomcat启动失败--Several ports (8005, 8080, 8009)
启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are alre ...
- JS练习:定时器--背景颜色交替变换
代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UT ...
- 【tips】ORM - SQLAlchemy操作MySQL数据库
优先(官方文档SQLAlchemy-version1.2): sqlalchemy | 作者:斯芬克斯 推荐一(长篇幅version1.2.0b3):python约会之ORM-sqlalchemy | ...
- 详解Cookie、LocalStorage、SessionStorage
不管是笔试还是面试相信大家都会经常遇到问Cookie.LocalStorage.SessionStorage 这三个不同的,什么不说先上一波图先: 针对他们大小之分应用场景也有不同: 因为考虑到每个 ...