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

Problem Description
Today
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
The
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.
 
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
 
Sample Input
2
5 3
1 2
2 3
4 5

5 1
2 5

 
Sample Output
2
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(并查集模版题)的更多相关文章

  1. HDU 1213(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. 【poj 1962】Corporative Network(图论--带权并查集 模版题)

    P.S.我不想看英文原题的,但是看网上题解的题意看得我 炒鸡辛苦&一脸懵 +_+,打这模版题的代码也纠结至极了......不得已只能自己翻译了QwQ . 题意:有一个公司有N个企业,分成几个网 ...

  3. HDU 1232 并查集板子题

    某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可). ...

  4. 畅通工程 HDU - 1232 并查集板子题

    #include<iostream> #include<cstring> using namespace std; ; int p[N]; int find(int x) { ...

  5. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  6. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  7. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  8. poj1182 食物链(并查集 好题)

    https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...

  9. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

随机推荐

  1. nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist

    Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [j ...

  2. GeckoWebBrowser设置cookie

    var uri = new Uri("http://www.aa.com"); //often cookies are stored on domain level, so &qu ...

  3. Photoshop如何实现图片相对画布居中

    先按ctrl+A,再选择要居中的图层,然后就会发现居中按钮被激活了

  4. Python总结2

    时间:25日上午'''列表定义:在[]内,可以存放多个任意类型的值,并以逗号隔开''''students=['sb','2b']print(students[1])student_info=['min ...

  5. Django - 数据获取

    Django - 数据获取 1.radio值获取 2.checkbox获取 3.select 获取 select 获取值,需要根据前端multiple来获取,get or getlist; 4.上传文 ...

  6. Linux之强大的selinux

    简单点说,SELinux就是用来加强系统安全性的.它给一些特定程序(这些程序也在不断增加)做了一个沙箱,它将文件打上了一个安全标签,这些标签属于不同的类,也只能执行特定的操作,也就是规定了某个应用程序 ...

  7. Problem 28

    Problem 28 https://projecteuler.net/problem=28 Starting with the number 1 and moving to the right in ...

  8. 腾讯云,搭建Java开发环境

    搭建 JAVA 开发环境 任务时间:18min ~ 20min 此实验教大家如何配置 JDK .Tomcat 和 Mysql 安装 JDK JDK 是开发Java程序必须安装的软件,我们查看一下 yu ...

  9. 2.1 Java开发工具包

        Java专业术语                   术语名   缩写                                                             ...

  10. HOG特征过程解释(转)

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...