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. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  2. 在mac本上折腾android 开发环境

    众所周知的原因,google的很多网站在国内无法访问,苦逼了一堆天朝程序员,下是在mac本上折腾android 开发环境的过程: 一.先下载android sdk for mac 给二个靠谱的网址: ...

  3. linux串口编程(c)

    //linux c: 串口设置//串口操作无非以下几个://1 打开                       //2 设置串口属性//3 read write //struct termios能够 ...

  4. 【MFC相关】图片显示

    CPaintDC dc(this); if (!m_hBitmap) return; image.Attach(m_hBitmap); CRect rect; GetClientRect(&r ...

  5. table中的换行问题

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. Spring AOP基于xml配置实例

    SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象.为了更好地理解记忆,这里几下我自己的通俗的理解. 切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法 ...

  7. HTTP status codes

    响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行.响应码分五种类型,由它们的第一位数字表示:1.1xx:信息,请求收到,继续处理2.2xx:成功,行为被成功地接受.理解和采纳3 ...

  8. django最佳实践

    导入的时候使用绝对导入或者清晰的相对导入 相对导入用法: from __future__ import absolute_import from .models import what_u_need ...

  9. Java疯狂讲义(四)

  10. .net运行时和核心类库源码(部分源码)微软官方下载

    部分类库代码:http://referencesource.microsoft.com/download.html 运行时clr源码: http://www.microsoft.com/en-us/d ...