#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; #define MAXN 5000
int fa[MAXN]; //父节点
int Rank[MAXN]; //数的高度
int ans; void init(int n) //初始化
{
for (int i = ; i < n; i++)
{
fa[i] = i; //每个人的根都是自己
Rank[i] = ; //每颗树的高度都为0
}
} int find(int x) //查(查询根节点)
{
if(fa[x] == x)
return x;
else
{
fa[x] = find(fa[x]); //递归求根节点
return fa[x];
}
} void set_union(int x, int y) //(并)
{
x = find(x);
y = find(y);
if (x == y)
return;
if (Rank[x] < Rank[y])
fa[x] = y;
else
fa[y] = x;
if (Rank[x] == Rank[y])
Rank[x]++; //树的高度加一
} bool same(int x, int y) //判断两个节点是否属于同一个集合
{
return find(x)==find(y);
} int main()
{
int n, m, t;
scanf("%d", &t);
while (t--)
{
scanf("%d %d", &n, &m);
memset(fa, , sizeof(fa));//对父节点进行清零
ans = n; //最开始的时候ans = 人数
init(n);
for (int i = ; i < m; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if (same(x, y))
continue;
else
{
set_union(x, y);
ans--;
}
}
printf("%d\n", ans);
}
//system("pause");
return ;
}

C - How Many Tables的更多相关文章

  1. LOCK TABLES和UNLOCK TABLES与Transactions的交互

    LOCK TABLES对事务不安全,并且在试图锁定表之前隐式提交任何活动事务. UNLOCK TABLES只有在LOCK TABLES已经获取到表锁时,会隐式提交任何活动事务.对于下面的一组语句,UN ...

  2. 函数的使用顺序---TABLES,USING,CHANGING

    SAP使用PERFORM的时候: ... [TABLES   itab1 itab2 ...]     [USING    a1 a2 ...]     [CHANGING a1 a2 ...]. E ...

  3. 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 ...

  4. 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'@' ...

  5. Neutron 理解 (4): Neutron OVS OpenFlow 流表 和 L2 Population [Netruon OVS OpenFlow tables + L2 Population]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  6. 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 ...

  7. 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 ...

  8. MySql: show databases/tables use database desc table

    1. show databases mysql> show databases;+--------------------+| Database |+--------------------+| ...

  9. mysql performance_schema 和information_schema.tables了解

    这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql  information_schema 信息相关  performance_schema 性能相关 ...

  10. responsive tables

    以上内容原本是整理为ppt格式的,贴过来格式有点乱,请见谅. 其他responsive tables参考: http://gergeo.se/RWD-Table-Patterns/ 3种类型的代码参考 ...

随机推荐

  1. Android开发之onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法

    onMeasure()函数由包含这个View的具体的ViewGroup调用,因此值也是由其ViewGroup中传入的.子类View的这两个参数widthMeasureSpec, heightMeasu ...

  2. vue组件挂载到全局方法

    在最近的项目中,使用了bootstrap-vue来开发,然而在实际的开发过程中却发现这个UI提供的组件并不能打到我们预期的效果,像alert.modal等组件每个页面引入就得重复引入,并不像eleme ...

  3. zookeeper+dubbo【转载】

    转载地址:http://ahua186186.iteye.com/blog/1912421 注:zookeeper集群是myid文件是没有后缀名的. 转自: http://www.verydemo.c ...

  4. reactjs的一些笔记

    1.使用虚拟DOM作为其不同的实现.同时可以由服务器node.js渲染,从而不需要过重的浏览器DOM支持.   2.虚拟DOM:在浏览器端用javascript实现了一套DOM API.用react开 ...

  5. cowboy跨域请求处理

    这几日在使用cowboy开发https服务器的过程中碰到几个问题,这里随手记录一下. 1)如果返回错误ERR_EMPTY_RESPONSE,那么可能是web服务器被关闭了. 2)如果返回错误ERR_C ...

  6. Rsync+Sersync同步

    Rsync+Sersync同步特点: (1):sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字:(2):rsync在同步的时候,只同步发生变化的这 ...

  7. 【转】澄清P问题、NP问题、NPC问题

    首先,原文链接.(这篇文章让我第一次有了感谢腾讯,感谢微信,感谢微信公众号的冲动.总之,非常感谢作者的分享.) 然后:结论图如下 担心万一哪天原网站把这篇文章下线,所以原文内容复制过来. 澄清P问题. ...

  8. python中的linspace,meshgrid,concatenate函数

    linspace可以用来实现相同间隔的采样. numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) ...

  9. C结构体、C++结构体、C++类的区别

    先来说说C和C++中结构体的不同 a) C语言中的结构体不能为空,否则会报错 1>d:\myproject\visual studio 2013\projects\myc++\main.c(71 ...

  10. ASP.NET:Global.asax

    ylbtech-.Net-ASP.NET:Global.asax 1.返回顶部 1. 一.定义:Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选的文件,该文件包含响应 A ...