C - How Many Tables
#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的更多相关文章
- 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种类型的代码参考 ...
随机推荐
- HDOJ 4704 Sum 规律 欧拉定理
规律 欧拉定理: 找规律 2^n-1 ,n 非常大用欧拉定理 Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13 ...
- 4.改变 HTML 图像
<!DOCTYPE html><html><body><script>function changeImage(){element=document.g ...
- Strans 内表-xml文件
一.内表到xml文件 <?sap.transform simple?> <tt:transform xmlns:tt="http://www.sap.com/transfo ...
- SAP 系统账期开关
(1)OB52 财务账期-C 财务维护表 T001B[维护表T001B] (2)OB29 -C FI 财政年变式 (3)MMPV / MMRV -物料账期 MMPV 商品会计期间设置-结帐期间 [ 如 ...
- Appnium安装-Mac平台
Appium的安装-MAC平台 其实Appium的安装方式主要有两种: 1)自己安装配置nodejs的环境,然后通过npm进行appium的安装 2)直接下载官网提供的dmg进行安装,dmg里面已 ...
- 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role
hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...
- 【转】看C++文档的小知识
转载:http://www.ggv.com.cn/forum/clib/ctype/isspace.html 函数isspace 原型:extern int isspace(int c); 用法 ...
- 「IOI1998」「LuoguP4342」Polygon(区间dp
P4342 [IOI1998]Polygon - 洛谷 题意翻译 题目可能有些许修改,但大意一致 多边形是一个玩家在一个有n个顶点的多边形上的游戏,如图所示,其中n=4.每个顶点用整数标记,每个边用符 ...
- Linux 设备树 dts
1. dtb反编译成dts文件命令:./kernel-4.4/scripts/dtc/dtc_overlay -I dtb -O dts out/target/product/m863ur100_p0 ...
- RTP Payload Format for Transport of MPEG-4 Elementary Streams over http
1.SDP (1)Http Request GET /getSdpForUrl?HttpUrl=nphMpeg4/g726-640x480 HTTP/1.0/r/n Host: 58.63.71.90 ...