Find Function Optimization:

After Path compression:

int find(int x){
return root[x] == x ? x : (root[x] = find(root[x]));
}

Avoid Stack overflow:

int find(int a){
while(root[a]!=a){
a=root[a];
}
return a;
}

 Combined with rank : (Combined with the height of tree)

int rank[MAXSIZE];

void UNION(int x, int y) {
int f1 = find(x);
int f2 = find(y);
if (rank[f1] <= rank[f2]) {
root[f1] = f2;
if (rank[f1] == rank[f2]) {
rank[f2] ++;
}
}
else root[f2] = f1;
}

  


Union Function :

void Union(int a,int b){
int ra = find(a);
int rb = find(b);
root[rb]=ra; // ra is the father of rb
}

 Species Union - Find Function:

int find(int i){
if(root[i]==i)return i;
int ans=root[i];
root[i]=find(root[i]);
dis[i]+=dis[ans];//求出节点a到根的距离
return root[i];
}
void Union(int u,int v,int root_u,int root_v,int x){
root[root_v]=root_u;
dis[root_v]=dis[u]+x-dis[v];//使用的是数学中向量计算
}
 while(m--){
scanf("%d%d%d",&u,&v,&dist);
int x=find(u),y=find(v);
if(x!=y)
Union(u,v,x,y,dist);
else
if(dis[u]+dist!=dis[v])
ans++;
}

Operation of Adjacency list :

int head[MAXSIZE];
struct node{
int cur,pre,weight;
}edge[MAXSIZE];
int cnt;//The num of Edges void initEdge(){
cnt = ;
for(int i = ; i< MAXSIZE; i++)
head[i] = -;
} void addEdge(int from , int cur){
edge[cnt].cur = cur;
edge[cnt].pre = head[from];
head[from] = cnt++;
} void print(int v){
for(int i = head[v]; i != -; i = edge[i].pre){
int cur = edge[i].cur;
printf("%d ",cur);
}
}

 Topological sorting:

//按如下建图,A在B前,则建一条A->B的有向边
//按如下策略排序:
//图中每次删除入度为0的节点,删除的节点加入已排序序列末尾
int sortlist[];
int topsort(){
queue<int>q;
int idx = ;
for(int i = ; i < n; i++)//n为要排序的节点个数
if(indgree[i] == )
q.push(i);
while(!q.empty()){
int cur = q.front;
sortlist[idx++] = cur;
q.pop();
n--;
for(int i = ; i< l[cur].size(); i++){//l[cur][i]表示以cur为起点i为终点的有向边
indgree[l[cur][i]]--;
if(!indgree[l[cur][i]])
q.push(l[cur][i]);
}
}
if(n > ) return ;//当n>0时,说明有节点未排序则表示节点关系有冲突
else return ;
}

邻接矩阵:

int indgree[MAXN], map[MAXN][MAXN];
int n;
stack <int> ss;
bool topsort(){
int i, j;
while(){
for(i = ; i <= n; ++i)
if(indgree[i] == ) break;
if(i != n + ){
for(j = ; j <= n; ++j)
if(map[i][j] == ){
map[i][j] = ;
--indgree[j];
}
indgree[i] = -;
ss.push(i);
}
else break;
}
bool flag = true;
for(i = ; i <= n; ++i){
for(j = ; j <= n; ++j){
if(map[i][j] == ){
flag = false;
}
}
}
return flag;
} void print(){
while(!ss.empty()){
printf("%d ",ss.top());
ss.pop();
}
printf("\n");
}

Union - Find 、 Adjacency list 、 Topological sorting Template的更多相关文章

  1. 【转】mysql的union、left join、 right join、 inner join和视图学习

    1.联合 union 进行多个查询语句时,要求多次查询的结果列数必须一样.此时,查询的结果以第一个sql语句的列名为准且union会自动去重复我们应该使用union all. 例...... 1.联合 ...

  2. SQL的inner join、left join、right join、full outer join、union、union all

    主题: SQL的inner join.left join.right join.full outer join.union.union all的学习. Table A和Table B表如下所示: 表A ...

  3. T-SQL 临时表、表变量、UNION

    T-SQL 临时表.表变量.UNION 这次看一下临时表,表变量和Union命令方面是否可以被优化呢? 阅读导航 一.临时表和表变量 二.本次的另一个重头戏UNION 命令 一.临时表和表变量 很多数 ...

  4. 【MySQL疑难杂症】如何将树形结构存储在数据库中(方案一、Adjacency List)

    今天来看看一个比较头疼的问题,如何在数据库中存储树形结构呢? 像mysql这样的关系型数据库,比较适合存储一些类似表格的扁平化数据,但是遇到像树形结构这样有深度的人,就很难驾驭了. 举个栗子:现在有一 ...

  5. hive的高级查询(group by、 order by、 join 、 distribute by、sort by、 clusrer by、 union all等)

    查询操作 group by. order by. join . distribute by. sort by. clusrer by. union all 底层的实现 mapreduce 常见的聚合操 ...

  6. SQL Server进阶(四):联接-cross join、inner join、left join、right jion、union、union all

    测试数据脚本 CREATE TABLE Atable ( S# INT, Sname ), Sage INT, Sfrom ) ) insert into Atable ,N,N'A' union a ...

  7. 拓扑排序(Topological Sorting)

    一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...

  8. Course Schedule课程表12(用Topological Sorting)

    [抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...

  9. 拓扑排序 (Topological Sorting)

    拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...

随机推荐

  1. JavaScript 高级程序设计(第3版)笔记——chapter5:引用类型(基本包装类型部分)

    一.介绍 为了方便操作基本类型值,ECMAScript还提供了3个特殊的引用类型:Boolean, Number, String. 实际上,每当读取一个基本类型值得时候,后台就会创建一个对应的基本包装 ...

  2. 在VS2010上使用C#调用非托管C++生成的DLL文件

    背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用.本篇文章将引导你快速理解这个调用 ...

  3. FtpManager类

    public class FtpManager { /// <summary> /// 主机名 /// </summary> string ftpServerIP; /// & ...

  4. CXF 开发 WebService

    什么是CXF: Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker ...

  5. easyui好例子,值得借鉴

    http://www.cnblogs.com/wuhuacong/p/3317223.html

  6. 我用的比较少的CSS选择器

    选择器 描述 [attribute] 用于选取带有指定属性的元素. [attribute=value] 用于选取带有指定属性和值的元素. [attribute~=value] 用于选取属性值中包含指定 ...

  7. php随笔11-Thinkphp常用系统配置大全

    Thinkphp常用配置  CHECK_FILE_CASE -- windows环境下面的严格检查大小写. /* 项目设定 */     'APP_DEBUG'    => false, // ...

  8. Java疯狂讲义(二)

  9. 蝕刻技術(Etching Technology)

    1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...

  10. redis缓存工具Jedis进行跨jvm加锁(分布式应用)--不幸暂弃用--能够做第三方锁使用

    近期使用redis碰到了多个并发处理同一个缓存的情况.在这样的情况下须要进行加锁机制. 本来想使用java自带的ReadWriteLock进行设置读写锁,这也是上家公司使用的方法. 后来经过商讨,给予 ...