How Many Tables
How Many Tables
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
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
Sample Input
2
5 3
1 2
2 3
4 5 5 1
2 5
Sample Output
2
4 并查集,找出多少个集合 #include<stdio.h>
int father[1005],depth[1005];
void init()
{
int i;
for(i = 1; i < 1005;i ++)
{
father[i] = i;
depth[i] = 0;
}
} int find(int x)
{
if(x==father[x])
return x;
else
return father[x] = find(father[x]);
} void unit(int x,int y)
{
x = find(x);
y = find(y);
if(x==y)
return ;
if(depth[x]<depth[y])
{
father[x] = father[y]; }
else
{
if(depth[x]>depth[y])
father[y] = father[x];
else
{
father[x] = father[y];
depth[y]++;
}
}
} int main()
{
int T,n,m,gr,j,a,b;
scanf("%d",&T);
while(T--)
{
gr = 0;
init();
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&a,&b);
unit(a,b);
}
for(j = 1;j <= n;j ++)
{
if(j == father[j])
gr++;
}
printf("%d\n",gr);
}
return 0;
}
How Many Tables的更多相关文章
- LOCK TABLES和UNLOCK TABLES与Transactions的交互
LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...
- 函数的使用顺序---TABLES,USING,CHANGING
SAP使用PERFORM的时候: ... [TABLES itab1 itab2 ...] [USING a1 a2 ...] [CHANGING a1 a2 ...]. E ...
- Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement
Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...
- mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@'localhost' for table 'accounts' when using LOCK TABLES
AutoMySQLBackup备份时,出现mysqldump: Got error: 1142: SELECT, LOCK TABLES command denied to user 'root'@' ...
- Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- Codeforces Round #342 (Div. 2) C. K-special Tables(想法题)
传送门 Description People do many crazy things to stand out in a crowd. Some of them dance, some learn ...
- mysql [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist (转载)
mysql报错Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 2013-11-2 ...
- MySql: show databases/tables use database desc table
1. show databases mysql> show databases;+--------------------+| Database |+--------------------+| ...
- mysql performance_schema 和information_schema.tables了解
这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql information_schema 信息相关 performance_schema 性能相关 ...
- responsive tables
以上内容原本是整理为ppt格式的,贴过来格式有点乱,请见谅. 其他responsive tables参考: http://gergeo.se/RWD-Table-Patterns/ 3种类型的代码参考 ...
随机推荐
- 判断浏览器是否支持某个css3属性的javascript方法
判断浏览器是否支持css3某个属性的方法: /** * 判断浏览器是否支持某一个CSS3属性 * @param {String} 属性名称 * @return {Boolean} true/false ...
- .Net 平台下的互联网架构新思考
上一篇<互联网应用架构谈>有很多阅读量,但后来实践中发现基于WCF服务层技术用于实现网站访问的效率是有问题的. 我的本意是以WCF中间层解决商业逻辑,供各个平台调用,然而还是带来诸多的麻烦 ...
- codeforces 633D - Fibonacci-ish 离散化 + 二分查询
Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He ...
- 2016032201 - mysql5.7.10绿色版安装
参考地址:http://jingyan.baidu.com/article/ff42efa93580c4c19e2202b6.html 其实您完全可以参考上面的百度贴吧内容搞定的,我记录只是做个笔记, ...
- ios中label的文字多种颜色显示
一 .在初始化方法中把所有需要高亮关键字的label加入到labels数组中,并且把这些label原来字体的颜色加入到 labelTextColors中 ,代码如下 self.labels = [NS ...
- thinkphp 框架的学习(1) 扩展配置文件
在config.php里面写入 1:'LOAD_EXT_CONFIG' => array('SETTINGS' => 'settings'); 系统会判断是否有参数:LOAD_EXT_CO ...
- SDC(1)–Hold Time
从以下两个论点触发可能会使Hold Time的计算理解起来更加容易: (1) H = SU – 1 ; (2) Hold Check的目的是确保Source Clock在某个边沿打出数据时,该数据不会 ...
- 洛谷 P1417 烹调方案
题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的 ...
- python eval函数
eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果. 可以把字符串转为list.tuple .dict 等数据类型 1.把字符串转为字典 ####### ...
- 取得inputStream的长度
1.网络下载文件 URL url = new URL(strUrl); HttpURLConnection httpconn = (HttpURLConnection)url.openConnecti ...