hdu_1213_How Many Tables_201403091126
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11038 Accepted Submission(s): 5447
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.
#include <stdio.h>
int pre[];
int find(int x)
{
int i,r,t;
t=r=x;
while(r!=pre[r])
{
r=pre[r];
}
while(t!=r)
{
i=pre[t];
pre[t]=r;
t=i;
}
return r;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m,n,i,j,a,b,pa,pb,total;
scanf("%d %d",&n,&m);
total=n;
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<m;i++)
{
scanf("%d %d",&a,&b);
pa=find(a);
pb=find(b);
if(pa!=pb)
{
pre[pb]=pa;
total--;
}
}
printf("%d\n",total);
}
return ;
}
//并查集简单应用
hdu_1213_How Many Tables_201403091126的更多相关文章
随机推荐
- 浅谈IO优化
三层结构 磁盘(存储).VM(卷管理)和文件系统.专有名词不好理解,打个比方说:磁盘就相当于一块待用的空地:LVM相当于空地上的围墙(把空地划分成多个部分):文件系统则相当于每块空地上建的楼房(决定了 ...
- ORA-00907: 缺失右括号问题或com.alibaba.druid.sql.parser.ParserException: TODO :IDENTIFIER的原因
以上只是说明错误的原因的一种.
- 禁用backspace网页回退功能
<script language="JavaScript">document.onkeydown = check;function check(e) { var cod ...
- 去掉myeclipse的预览窗口
1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...
- MySQL的安装和启动
一.MySQL各类安装方法的比较 在Linux系统下,MySQL有3种主要的安装方式,分别是:RPM安装.二进制安装.源码安装.三种安装方式的优缺点如下表所示: RPM安装 二进制安装 源码安装 ...
- mybatis中打印sql语句
在mybatis-config.xml中properties节点下,配置一个settings节点 <settings> <setting name="cacheEnable ...
- 我正在学英语是用learning english还是用studying english?
学一门语言用 learn. study 表示深入研究,一般指在大学里.如果大学里的专业是英语,就可以说 study English. 1. If you study hard, you will le ...
- Jquery 实现列表的显示和隐藏
本人github源码下载地址:https://github.com/liuyanpeng521/ListChange.git
- [ SDOI 2009 ] HH的项链 & [ HEOI 2012 ] 采花
\(\\\) \(Description\) 给出一个长为\(N\)的序列,\(M\)次询问区间\([L_i,R_i]\)内不同数字的个数. \(N\in [1,5\times 10^4]\),\(M ...
- TensorFlow-Gpu环境搭建——Win10+ Python+Anaconda+cuda
参考:http://blog.csdn.net/sb19931201/article/details/53648615 https://segmentfault.com/a/1190000009803 ...