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的更多相关文章

  1. SQL Server-聚焦UNIOL ALL/UNION查询(二十三)

    前言 本节我们来看看有关查询中UNION和UNION ALL的问题,简短的内容,深入的理解,Always to review the basics. 初探UNION和UNION ALL 首先我们过一遍 ...

  2. SQL 提示介绍 hash/merge/concat union

    查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...

  3. LINQ to SQL语句(8)之Concat/Union/Intersect/Except

    适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1.简单形式: var q = ( from c in db ...

  4. SQLServer-----Union,Union All的使用方法

    转载: http://blog.csdn.net/kiqinie/article/details/8132485 select a.Name from Material as a union sele ...

  5. 假如 UNION ALL 里面的子句 有 JOIN ,那个执行更快呢

    比如: select id, name from table1 where name = 'x' union all select id, name from table2 where name =  ...

  6. sql union和union all的用法及效率

    UNION指令的目的是将两个SQL语句的结果合并起来.从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION的一个限制是两个 ...

  7. 【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 ...

  8. sql with as union all

    WITH RPL (FId,Fname,Forder) AS ( SELECT ment.deptno,ment.deptname,ment.orderno FROM JTERP..fg_depart ...

  9. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  10. LINQ系列:LINQ to SQL Concat/Union

    1. Concat 单列Concat var expr = (from p in context.Products select p.ProductName) .Concat( from c in c ...

随机推荐

  1. UICollectionView基础API笔记

    UICollectionView系列API,属性含义笔记.在UICollectionView笔记1中我们了解了UICollectionView是什么,以及可以做什么:在UICollectionView ...

  2. hibernate课程 初探单表映射1-9 创建关系映射文件

    创建关系映射文件:(把实体类映射成一个表) 1 右键src==>new==>other==>hibernate==>hbm.xml==>Student==>Fini ...

  3. 创建一个自己的GitHub,创建自己的开源项目

    作者是一个大学在读学生,自己在平时的学习中,GitHub上的开源项目给自己提供了很大的帮助.GitHub是目前使用最广泛的分布式项目管理软件,GitHub上面托管了许多非常优秀的开源项目.我觉得每一个 ...

  4. C#启动或停止 计算机中“服务”

    第一.要添加一个引用System.ServiceProcess 第二.要在程序中使用命名空间ServiceProcess 代码片段: using System.ServiceProcess; Serv ...

  5. 时序js插件cubism使用

    document http://iwantmyreal.name/blog/2012/09/16/visualising-conair-data-with-cubism-dot-js https:// ...

  6. CRM User Status profile中Business Transaction字段的用途

    有朋友问到User Status profile中Business Transaction字段的用途,如下图INPR, FINI所示. 实际上,这个字段作为一个桥梁,连接了User Status和Sy ...

  7. window.onload中调用函数报错的问题

    今天练习js,忽然遇到了一个问题,就是window.onload加载完成后,调用其中的函数会报错, 上一段简单的代码: 报错信息: 报错原因: 当window.onload加载完成后,第一个alert ...

  8. WordPress企业建站心得

    回头聊聊我用WordPress做企业网站的事.说是企业网站,其实就是一个小的企业展示网站.事情要从我爸开了一家自行车店开始说起,自从他开了自行车店,不但开始学着玩起了微信(因为要做微信营销),又想到了 ...

  9. 根据图片的URL来实例化图片

    正常的Image图片类实例化的时候都需要使用本地的虚拟路径而不能使用URL,如果使用URL就会出现   不支持 URI 格式  这样的问题,正确的写法如下: HttpWebRequest reques ...

  10. Android(java)学习笔记89:Bundle和Intent类使用和交互

    1. Bundle 和 Intent:    Bundle只是一个信息的载体 将内部的内容以键值对组织 ,Intent负责Activity之间的交互自己是带有一个Bundle的.Intent.putE ...