点击打开链接

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

这个为什么TEL

Select Code

#include <stdio.h>
#include <string.h>
#define Maxn 1005
int par[Maxn],count=0;
void init(int n)
{
for(int i=0; i<n; i++)
par[i]=i;
}
int find(int x)
{
int r=x;
while(par[r]!=r)
{
r=par[r];
}
int i=x,j;
while(i!=r)
{
j=par[r];
par[r]=i;
i=j;
}
return r;
} void join(int x, int y)
{
int fx=find(x), fy=find(y);
if(fx!=fy)
{
par[fx]=fy;
count++;
}
}
int main()
{
int i,j,a,b,n;
scanf("%d",&n);
while(n--)
{
count=0;
scanf("%d%d",&a,&b);
init(a);
while(b--)
{
scanf("%d%d",&i,&j);
join(i,j);
}
printf("%d\n",a-count);
} return 0;
}

AC代码

Select Code

#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring> using namespace std; int pre[1100]; int findset(int v)
{
int t1,t2=v;
while(v!=pre[v]) v=pre[v];
while(t2!=pre[t2])
{
t1=pre[t2];
pre[t2]=v;
t2=t1;
}
return v;
} void unions(int x,int y)
{
int t1=findset(x);
int t2=findset(y);
if(t1!=t2) pre[t1]=t2;
} int main()
{
int T,n,m;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) pre[i]=i;
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
unions(u,v);
}
int ans=0;
for(int i=1;i<=n;i++)
if(pre[i]==i) ans++;
cout<<ans<<endl; }
}

D - How Many Tables (并查集)(水题)的更多相关文章

  1. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. HDU 1213 How Many Tables 并查集 水~

    http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...

  3. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  4. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  5. 【PAT-并查集-水题】L2-007-家庭房产

    L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...

  6. POJ2524并查集水题

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  7. HDU1863(Kruskal+并查集水题)

    https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...

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

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

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

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

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

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

随机推荐

  1. 拦截TextBox 双击消息

    Option Explicit Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLo ...

  2. VS2010生成的文件在别的机器上运行提示“丢失MSVCR100D.dll”<转>

    用vs2010编写的程序经常会发生的一个问题.在自己的机器上运行的好好的,但是在别的机器上就会发生没有找到MSVCR100D.dll.这是 个很头疼的问题.对于一些代码量几百行的小程序,我不可能要求其 ...

  3. 最全互联网Linux工作规划!

    首先祝贺你选择学习Linux,你可能即将踏上Linux的工作之旅,出发之前,让我带你来看一看关于Linux和Linux运维的一切. Linux因其高效率.易于裁剪.应用广等优势,成为了当今中高端服务器 ...

  4. The web application [/zzti] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver

    先说下题目:因为是在我进行处理项目升级时同时遇到了上面的问题,一般都会遇到,所以就一并说了 不罗嗦了,正题: 在本地服务器中提供tomcat6,然后在myeclipse中配置severs tomcat ...

  5. MyBatis ehcache二级缓存

    ehcache二级缓存的开启步骤: 1.导入jar 2.在映射文件中指定用的哪个缓存 3.加一个配置文件,这个配置文件在ehcache jar包中就有 使增删改对二级缓存不刷新: 对一级缓存没有用的, ...

  6. PHP中SQL查询语句的id=%d解释

    在SQL语句中有一些写的是这样的: 'SELECT id FROM dbname WHERE xx_id = %d;', $bl['student_id'] 其中的“xx_id = %d”,这里的%d ...

  7. Django模板层

    一:模板简介 二:模板语法值变量 三: 模板之过滤器 四: 模板之标签 五:自定义标签和过滤器   一:模板简介 def current_datetime(request): now=datetime ...

  8. web应用安全权威指南(文摘)

    第1章 什么是web应用的安全隐患 第3章 Web安全基础,HTTP,会话管理,同源策略 content_length 字节数 content_type mime类型 百分号编码 referer :请 ...

  9. Webservice初级问题: FAILED TO READ WSDL document

    这个问题是说明,这个版本的没法下载 犯错的图样 处理方法一: 将网页上xml文档下载,保存在本地,然后错误提示的这几行删除,保存文档,然后从本地调用 (1)右键另存为 保存为文件名a.xml (2)打 ...

  10. Hdu3549 Flow Problem 2017-02-11 16:24 58人阅读 评论(0) 收藏

    Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a ...