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种类型的代码参考 ...
随机推荐
- 1 Maven简介
一.构建(build) 清理.编译.测试.打包.部署等一系列操作. 二.maven介绍: maven是一个强大的构建工具,能够帮助我们自动化构建过程:从清理(clean).编译(com ...
- 对私有API提交的注意事项
1.这个等于堵死了调试断点.关闭就不能断点调试了. 2.对于敏感的函数名要做一个对称加密处理. 防止二进制文件的静态扫描. 3.对于调用私有函数的方法,可以做一个宏定义包装. #define 你的正常 ...
- XMPP学习笔记 -- RFC 6120
XMPP - Extensible Messaging and Presence Protocol 1. 中文版3920 http://wiki.jabbercn.org/RFC3920 2. 大部分 ...
- Tomcat Session Clustering
搭建 Tomcat 集群需要解决很多的问题,其中之一就是要解决 Session 共享问题.小规模集群可以使用 Tomcat 提供的 Session Clustering 来解决. For the im ...
- @GetMapping和@PostMapping接收参数的格式
一.1.使用@Controller 注解,在对应的方法上,视图解析器可以解析return 的jsp,html页面,并且跳转到相应页面 若返回json等内容到页面,则需要加@ResponseBody注解 ...
- 使用php ffmpeg处理视频
工作中遇到video加载视频的问题,但是视频封面在手机上无法取到视频的第一帧,video标签无法在手机上取到第一帧,经过几天的研究于搜索终于找到比较好用的办法,就是php ffmped 插件,该插件的 ...
- 拓展gcd求不定方程通解
void gcd(LL a,LL b,LL &d,LL &x,LL &y){ ){d=a;x=;y=;return;} gcd(b,a%b,d,x,y); int t=x; x ...
- bzoj4485: [Jsoi2015]圈地
思维僵化选手在线被虐 其实应该是不难的,题目明显分成两个集合,要求是不同集合的点不能联通 先假设全选了,然后二分图最小割,相邻两个点直接连墙的费用就可以了 #include<cstdio> ...
- tomcat启动项目被重新加载,导致资源初始化两遍
之前没有遇到过这个问题,配了三天的项目了,惊人啊!!!各种怪问题全被我赶上了.真有种骂人的冲动. tomcat启动项目时,项目资源被加载两遍. 原因:配置虚拟目录导致,项目被重新加载. <Hos ...
- 【转载】Myeclipse中实现js的提示
近期需要大量使用JS来开发,但是MyEclipse2014自带的JS编辑器没有代码提示的功能,开发效率有点低,所以安装了一个Spket的插件,过程非常简单,SVN插件的安装比这个更简单. Spket插 ...