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种类型的代码参考 ...
随机推荐
- 安装mysql时,提示This application requires .NET framework 4.0问题
Question:双击MySQL的安装文件, 弹框提示, Answer:安装微软的 .NET Framework 4或更新的版本,下载地址:https://www.microsoft.com/zh ...
- 怎样把word直接转换成ppt
- HDU 1878(1Y) (判断欧拉回路是否存在 奇点个数为0 + 一个联通分量 *【模板】)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- JavaSE基础练习IO,字符串,循环
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...
- mediaxyz访谈录:ffmpeg的码率控制
mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方 ...
- html5--3.22 综合实例03
html5--3.22 综合实例03 学习要点 通过一个综合实例来回顾学过的内容 这一章的内容比较多,不必强求一下子全记住,多做一些练习,用得多了自然就可以记住了 可以自己找一些实例练练手,比如各网站 ...
- idea提交新项目到远程git创库
1.创建远程版本库 http://192.168.28.130:81 登陆用户:maohx/123456 版本库名称最后与本地项目名称一致 如:spring-cloud-demo 2.创建本地版本库 ...
- CNN中下一层Feature map大小计算
符号表示: $W$:表示当前层Feature map的大小. $K$:表示kernel的大小. $S$:表示Stride的大小. 具体来讲: 整体说来,和下一层Feature map大小最为密切的就是 ...
- 万径人踪灭(FFT+manacher)
传送门 这题--我觉得像我这样的菜鸡选手难以想出来-- 题目要求求出一些子序列,使得其关于某个位置是对称的,而且不能是连续一段,求这样的子序列的个数.这个直接求很困难,但是我们可以先求出所有关于某个位 ...