Union - Find 、 Adjacency list 、 Topological sorting Template
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的更多相关文章
- 【转】mysql的union、left join、 right join、 inner join和视图学习
1.联合 union 进行多个查询语句时,要求多次查询的结果列数必须一样.此时,查询的结果以第一个sql语句的列名为准且union会自动去重复我们应该使用union all. 例...... 1.联合 ...
- 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 ...
- T-SQL 临时表、表变量、UNION
T-SQL 临时表.表变量.UNION 这次看一下临时表,表变量和Union命令方面是否可以被优化呢? 阅读导航 一.临时表和表变量 二.本次的另一个重头戏UNION 命令 一.临时表和表变量 很多数 ...
- 【MySQL疑难杂症】如何将树形结构存储在数据库中(方案一、Adjacency List)
今天来看看一个比较头疼的问题,如何在数据库中存储树形结构呢? 像mysql这样的关系型数据库,比较适合存储一些类似表格的扁平化数据,但是遇到像树形结构这样有深度的人,就很难驾驭了. 举个栗子:现在有一 ...
- 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 常见的聚合操 ...
- 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 ...
- 拓扑排序(Topological Sorting)
一.什么是拓扑排序 在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列.且该序列必须满足下面两个 ...
- Course Schedule课程表12(用Topological Sorting)
[抄题]: 现在你总共有 n 门课需要选,记为 0 到 n - 1.一些课程在修之前需要先修另外的一些课程,比如要学习课程 0 你需要先学习课程 1 ,表示为[0,1]给定n门课以及他们的先决条件,判 ...
- 拓扑排序 (Topological Sorting)
拓扑排序(Topological Sorting) 一.拓扑排序 含义 构造AOV网络全部顶点的拓扑有序序列的运算称为拓扑排序(Topological Sorting). 在图论中,拓扑排序(Topo ...
随机推荐
- MapReduce的C#实现及单元测试(试验)
MapReduce.cs类文件代码 MapReduce的执行方法 using System; using System.Collections.Generic; //using System.Lin ...
- JPEG概述和头分析(C源码)
原创文章,转载请注明:JPEG概述和头分析(C源码) By Lucio.Yang 部分内容来自:w285868925,JPEG压缩标准 1.JPEG概述 JPEG是一个压缩标准,又可分为标准 JPE ...
- CentOS搭建PHP服务器环境(LAMP)
安装httpd mysql mysql-server php: yum install -y httpd mysql mysql-server php php-devel 安装php的扩展 yum i ...
- PHP 导入excel
db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0, PHP代码: header("Content-type:text/html; ...
- [LeetCode]题解(python):013-Roman to Integer
题目来源: https://leetcode.com/problems/roman-to-integer/ 题意分析: 这道题目和上一题目相反,是将罗马数字转化成阿拉伯数字. 题目思路: 只要知道罗马 ...
- 1 初级.net web工程师,在工作中都做些什么
初级.Net Web工程师,在工作中都做些神马? 职责 初级.Net Web工程师的主要职责,就是按比较详细的要求去完成代码. 比较详细的要求是指:一般会把页面式样.功能的描述.数据库结构.性能要 ...
- Test class should have exactly one public zero-argument constructor
java.lang.Exception: Test class should have exactly one public zero-argument constructor at org.juni ...
- c++ 实现将数字转换为中文数字输出
实现如下函数: void printInChinese(int num); 这个函数输入一个小于100000000(一亿)的正整数,并在屏幕上打印这个数字的中文写法. 例如: 17 -> 一十七 ...
- 十句话教你学会Linux数据流重定向
1.看到重定向一下子就想起了web里面的redirect,没错,但是Linux数据流重定向的作用不是跳到另一个网页,而是用来存储重要的屏幕信息.将不必要的屏幕信息输出到文件里或者“黑洞”里.将错误信息 ...
- 使用CAShapeLayer来实现圆形图片加载动画[译]
原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...