点击打开链接

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. 跟我学算法-吴恩达老师的logsitic回归

    logistics回归是一种二分类问题,采用的激活函数是sigmoid函数,使得输出值转换为(0,1)之间的概率 A = sigmoid(np.dot(w.T, X) + b ) 表示预测函数 dz ...

  2. tomcat8 安全加固

    本文基于tomcat8.0.24 1.删除文档和示例程序 [操作目的]删除示例文档 [加固方法]删除webapps/docs.examples.manager.ROOT.host-manager [是 ...

  3. Putty建立隧道的方法[z]

    通过上节我们了解了SSH隧道的优点,但是无论是现实中还是网络中,隧道都是有入口和出口的,本节就以Putty隧道为例,详细介绍Putty建立隧道的方法. 模拟案例一: 端口,出口为2012端口,隧道建立 ...

  4. 运行Junit单测时遇到的问题

    现在有两个办法解决: 1.junit版本降到4.10 2.导入hamcrest-core-1.3.jar 官网:JUnit now uses the latest version of Hamcres ...

  5. MySQL之——GROUP BY分组取字段最大值

    转载自:http://blog.csdn.net/l1028386804/article/details/54657412 假设有一个业务场景,需要查询用户登录记录信息,其中表结构如下: CREATE ...

  6. python类和元类

    python 类和元类详解  小麦麦子 2016-09-06 11:11:00        今天在网上看到一篇关于python语言中类和元类(metaclass)的一些讲解和简单运用,感觉对pyth ...

  7. Centos 7.2编译安装MariaDB-10.0.xx

    系统: centos7.2 x64数据库:MariaDB-10.0.30 使用jemalloc对MySQL内存进行优化. 软件包下载地址:http://pan.baidu.com/s/1eS44OKU ...

  8. loadrunner12-用Chrome如何录制脚本

    1.下载Chrome最新版本: 2.打开需要录制的网页,按下F12按钮: 3.在弹出框中,选择Network标签,在该标签下单击红色按钮(开始录制按钮): 4.按下F5刷新页面(不管是否需要录制当前页 ...

  9. 博客搬家到blog.wu8685.com

    博客园算是我最开始来的地方了吧,当时还在学校,为了找工作会看一些理论方面的东西,所以写的都是偏理论的心得. 后来参加了工作,开始忙起来,也就没有时间来更新了.其实忙都是借口,这点还是需要反思的. 大概 ...

  10. icp算法

    https://github.com/tttamaki/ICP-test https://github.com/tttamaki/SICP-test