#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笔记之自定义对话框

    效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...

  2. SpringBoot-(3)-RestController接口参数

    一,无参接口: //无参接口 @RequestMapping("/appSecret") public String secret() { return "EK125EK ...

  3. virtualbox Units specified don't exist. SHSUCDX can't install.

    version infomatin: virtual box: 5.1.12 platform: win10 x64 target OS: win7 x64 问题 在win10系统上,使用virtua ...

  4. charles刷分微信跳一跳小程序对https的理解

    以前以为只要安装了https 客户端与服务端的数据会被加密就安全了 事实上 只要任意一款抓包工具 并伪造证书  就可以解密这个被所谓https加密的数据 如  可以下载charles的根证书  作为伪 ...

  5. codeforces Codeforces Round #273 (Div. 2) 478B

    B. Random Teams time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. poj3295 Tautology —— 构造法

    题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...

  7. IOC入门1

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  8. hdu 1047 Integer Inquiry(大数)

    题意:整数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<stdlib.h> #include ...

  9. 实现虚拟机VMware上linux与windows互相复制与粘贴

    from:http://blog.csdn.net/u012243115/article/details/40454063 系统环境: win7系统,虚拟机VMwareWorkstation上运行的C ...

  10. js数组,在遍历中删除元素(用 for (var i in arr)是无效的 )

    /** * 有效的方式 - 改变下标,控制遍历 */ for (var i = 0; i < arr.length; i++) { if (...) { arr.splice(i, 1); // ...