“Tarjan有三种算法 你们知道吗”——Tar乙己

void tarjan(int x)
{
low[x]=dfn[x]=++ind;
q[++top]=x;mark[x]=;
for(int i=last[x];i;i=e[i].next)
if(!dfn[e[i].to])
{
tarjan(e[i].to);
low[x]=min(low[x],low[e[i].to]);
}
else if(mark[e[i].to])
low[x]=min(low[x],dfn[e[i].to]);
if(low[x]==dfn[x])
{
int now=;scc++;
while(now!=x)
{
now=q[top--];mark[now]=;
bl[now]=scc;num[scc]++;
}
}
}

缩点

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;
int first[maxn],to[maxn],next[maxn],cnt,from[maxn];
void add(int u,int v)
{
from[++cnt]=u;
to[cnt]=v;
next[cnt]=first[u];
first[u]=cnt;
}
int dfn[maxn],low[maxn],vis[maxn],ind,scc,now,st[maxn],top;
int isc[maxn];
vector<int> bl[maxn];
int blc;
int tag[maxn];
void Tarjan_dfs(int x,int fa)
{
int son=;
low[x]=dfn[x]=++ind;
for(int i=first[x];i;i=next[i])
{
int now=to[i];
if(!dfn[now])
{
st[++top]=i;son++;
Tarjan_dfs(now,x);
low[x]=min(low[x],low[now]);
if(low[now]>=dfn[x])
{
isc[x]=;
bl[++blc].clear();
while()
{
int num=st[top--];
if(tag[from[num]]!=blc)
{
bl[blc].push_back(from[num]);
tag[from[num]]=blc;
}
if(tag[to[num]]!=blc)
{
bl[blc].push_back(to[num]);
tag[to[num]]=blc;
}
if(to[num]==now && from[num]==x)break;
}
}
}
else if(dfn[now]<dfn[x] && now!=fa)
{
st[++top]=i;
low[x]=min(low[x],dfn[now]);
}
}
if(fa== && son==)isc[x]=;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=;i<=n;i++)if(!dfn[i])Tarjan_dfs(i,-);
cout<<blc<<endl;
return ;
}

点双连通分量

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int maxn=;
int first[maxn],to[maxn],next[maxn],cnt,from[maxn];
void add(int u,int v)
{
from[++cnt]=u;
to[cnt]=v;
next[cnt]=first[u];
first[u]=cnt;
}
int dfn[maxn],low[maxn],vis[maxn],ind,scc,now,st[maxn],top;
int isb[maxn];
vector<int> bl[maxn];
int blc;
int tag[maxn];
void Tarjan_dfs(int x,int fa)
{
dfn[x]=low[x]=++ind;
for(int i=first[x];i;i=next[i])
{
int v=to[i];
if(!dfn[v])
{
Tarjan_dfs(v,x);
low[x]=min(low[x],low[v]);
if(low[v]>dfn[x])
isb[i]=isb[i^]=;
}
else if(dfn[v]<dfn[x] && v!=fa)low[x]=min(low[x],dfn[v]);
}
}
int vis[maxn];
void dfs(int x)
{
vis[x]=;
tag[x]=blc;
for(int i=first[x];i;i=next[i])
{
int v=to[i];
if(isb[v])continue;
if(!vis[i])dfs(v);
}
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
for(int i=;i<=n;i++)if(!dfn[i])Tarjan_dfs(i,-);
for(int i=;i<=n;i++)if(!vis[i]){blc++;dfs(i);}
cout<<blc<<endl;
return ;
}

边双连通分量

图的Tarjan算法的更多相关文章

  1. tarjan算法 POJ3177-Redundant Paths

    参考资料传送门 http://blog.csdn.net/lyy289065406/article/details/6762370 http://blog.csdn.net/lyy289065406/ ...

  2. #图# #SPFA# #Tarjan# ----- BZOJ1179

    SPFA算法 SPFA(Shortest Path Faster Algorithm)(队列优化)算法是求单源最短路径的一种算法. 判负环(在差分约束系统中会得以体现).如果某个点进入队列的次数超过N ...

  3. Tarjan算法:求解图的割点与桥(割边)

    简介: 割边和割点的定义仅限于无向图中.我们可以通过定义以蛮力方式求解出无向图的所有割点和割边,但这样的求解方式效率低.Tarjan提出了一种快速求解的方式,通过一次DFS就求解出图中所有的割点和割边 ...

  4. 图之强连通、强连通图、强连通分量 Tarjan算法

    原文地址:https://blog.csdn.net/qq_16234613/article/details/77431043 一.解释 在有向图G中,如果两个顶点间至少存在一条互相可达路径,称两个顶 ...

  5. 【强联通图 | 强联通分量】HDU 1269 迷宫城堡 【Kosaraju或Tarjan算法】

      为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明 ...

  6. Tarjan算法:求解无向连通图图的割点(关节点)与桥(割边)

    1. 割点与连通度 在无向连通图中,删除一个顶点v及其相连的边后,原图从一个连通分量变成了两个或多个连通分量,则称顶点v为割点,同时也称关节点(Articulation Point).一个没有关节点的 ...

  7. 图之强连通--Tarjan算法

    强连通分量 简介 在阅读下列内容之前,请务必了解图论基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Components ...

  8. 图的连通性——Tarjan算法&割边&割点

    tarjan算法 原理: 我们考虑 DFS 搜索树与强连通分量之间的关系. 如果结点 是某个强连通分量在搜索树中遇到的第⼀个结点,那么这个强连通分量的其余结点肯定 是在搜索树中以 为根的⼦树中. 被称 ...

  9. 图的连通性--Tarjan算法

    一些概念 无向图: 连通图:在无向图中,任意两点都直接或间接连通,则称该图为连通图.(或者说:任意两点之间都存在可到达的路径) 连通分量: G的 最大连通子图 称为G的连通分量. 有向图 (ps.区别 ...

随机推荐

  1. 使用Swoole加速Laravel(正式环境中)

    1 Laravel的速度瓶颈在哪? 1.1 已有的一些优化方法 1.1.1 laravel官方提供了一些优化laravel的优化方法 php artisan optimize php artisan ...

  2. hive深入使用

    Hive表的创建和数据类型 https://cwiki.apache.org/confluence/display/Hive/Home 管理表和外部的区别 # 管理表 1. 内部表也称之为MANAGE ...

  3. Devexpress GridView 常用操作总结

    一:Clone返回新的 DataTable Clone返回新的 DataTable,与当前的 DataTable 具有相同的架构:Copy:返回新的 DataTable,它具有与该 DataTable ...

  4. Python高级入门01-property

    JAVA中存在对变量 私有化,公开,保护... 私有化时候,需要提供一个公开的get 和 set方法对外公开,让别人进行调用 python中同样存在    私有化变量定义是__是这个双下划线,eg:_ ...

  5. 核函数 深度学习 统计学习 强化学习 神经网络 xx

  6. PAT 1061. 判断题(15)

    判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过100的正整数N和M,分别是学生人数和判断题数量.第二行给出M个不超过5的正 ...

  7. 移动端 触摸事件 ontouchstart、ontouchmove、ontouchend、ontouchcancel[转]

    转:http://www.cnblogs.com/irelands/p/3433628.html 1.Touch事件简介pc上的web页面鼠 标会产生onmousedown.onmouseup.onm ...

  8. 认识CoreData—使用进阶

    之前两篇文章都比较偏理论,文字表达比较多一些,但都是干货!学习时先理解理论知识,才能更好的帮助后面的理解.在这篇文章中,将会涉及关于CoreData的一些复杂操作,这些操作会涉及分页查询.模糊查询.批 ...

  9. maven导入项目时,缺少部分source folder

    今天导入公司的maven项目时,少了一些source folder,运行启动正常,但是页面打不开,找不到对应的目录文件,使用maven更新项目,重启编辑器也无效. 问题描述如图所示,缺少了图中的2个目 ...

  10. Data Structure Graph: cycle in a directed graph

    geeks上的解答复杂了些,用回溯就行了 #include <iostream> #include <vector> #include <algorithm> #i ...