Union found
Use array.
class UnionFound {
public:
vector<int> v;
int cnt;
UnionFound(int n) {
v = vector<int>(n);
for (int i = ; i < n; i++)
v[i] = i;
cnt = n;
}
int findParent(int i) {
if (v[i] == i) return i;
v[i] = findParent(v[i]);
return v[i];
}
void Union(int p, int c) {
int pp = findParent(p);
int cp = findParent(c);
if (pp != cp) {
v[cp] = pp;
cnt--;
}
}
};
Use map.
class UnionFound {
public:
unordered_map<int,int> parents;
int cnt = ;
void AddItem(int i) {
parents[i] = i;
cnt++;
}
int GetParent(int i) {
if (parents[i] == i) return i;
return parents[i] = GetParent(parents[i]);
}
void Union(int p, int c) {
int pp = GetParent(p);
int cp = GetParent(c);
if (pp != cp) {
parents[cp] = pp;
cnt--;
}
}
};
Union found的更多相关文章
- SQL Server-聚焦UNIOL ALL/UNION查询(二十三)
前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...
- SQL 提示介绍 hash/merge/concat union
查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...
- LINQ to SQL语句(8)之Concat/Union/Intersect/Except
适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...
- SQLServer-----Union,Union All的使用方法
转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...
- 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢
比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name = ...
- sql union和union all的用法及效率
UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- sql with as union all
WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...
- Oracle 中 union 和union all 的简单使用说明
1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...
- LINQ系列:LINQ to SQL Concat/Union
1. Concat 单列Concat var expr = (from p in context.Products select p.ProductName) .Concat( from c in c ...
随机推荐
- spring开发配置编码
在pom.xml中添加属性project.build.sourceEncoding就可以设置工程的编码 <properties> <!-- 文件拷贝时的编码 --> <p ...
- .net笔试题一(简答题)
1. 简述 private. protected. public. internal 修饰符的访问权限答:private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类 ...
- NeatUpload 的使用
1 <httpModules> 2 <add name="UploadHttpModule" type="Brettle.Web.NeatUpload. ...
- python复数
复数的概念在很久以前,数学家们被下面的等式困扰.x2=-1这是因为任何实数(无论正负)乘以自己总会得到一个非负数.一个数怎么可以乘以自己得到一负数?没有这样的实数存在.就这样18世纪,数学家们发了一个 ...
- 使用Kubernetes的java-client实现Deployment的部署及更新操作
1. 背景介绍 需求: 针对多种协议SDK构造探针,测试公司接入机服务状况(每一个探针应对单一接入机,接入机数量可能会动态变化). 难点: 大多数协议SDK均不支持多实例运行,且部分SDK通过生成文件 ...
- eclipse调试(转)
step into : 单步执行,遇到子函数就进入并且继续单步执行(F5) step over: 在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完在停止,也就是把子函 ...
- WPF动画的几种模式
最近在用WPF做简单动画,以下是几点经验总结: 1. 使用DispatcherTimer做动画 VB6的年代大家就用Timer做动画了,不用多解释,这个DispatcherTimer和本身的Timer ...
- 第2章 TCP-IP的工作方式
第2章 TCP-IP的工作方式 TCP/IP协议系统 为了实现TCP的功能,TCP/IP的创建者使用了模块化的设计.TCP/IP协议系统被分为不同的组件,每个组件分别负责通信过程的一个步骤.这种模块化 ...
- eros 修改 android上原生picker的颜色的呢
修改选中颜色和文字颜色 修改文件如下 修改窗口底色
- badboy页面脚本发生错误,解决方案
1.参考网址:https://jingyan.baidu.com/article/e9fb46e17537797520f76645.html?from=qqbrowser061108 本人亲自测试,方 ...