“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. 中国程序员如何去 Facebook 工作?

    1.在Facebook,可以选择哪里工作? Facebook 在内地确实没有 Office ,但可以在https://www.facebook.com/careers/?ref=pf#location ...

  2. 关于function的一种常用用法

    关于function的一种常用用法 void Share::InitAcrossManager() { GsMgrEvent gsMgrEvents;//保存function的结构体 gsMgrEve ...

  3. maven;tomcat配置

    [说明]今天呀,上午刚刚打算写javaweb项目,服务器就出现了问题,就花了点时间搞定了:下午听老大说了任务的大致内容和意义,收获颇多:晚上去服务器上部署了maven,听说可以实现热部署 一:今天完成 ...

  4. vue中handsontable 使用

    handsontable是目前在前端界最接近excel的插件,可以执行编辑,复制粘贴,插入删除行列,排序等复杂操作 1.安装模块包 npm install handsontable-pro @hand ...

  5. android菜鸟学习笔记22----ContentProvider(二)ContentObserver的简单使用

    现在有这样一个应用A通过ContentProvider提供自己的数据给其他应用,应用B通过ContentResolver获取应用A中提供的数据,并将其展示在ListView中,而应用C通过Conten ...

  6. 一些blog地址总结整理:

    女神 python之路-网络编程初版:https://www.cnblogs.com/Eva-J/articles/8066842.html python之路-网络编程(重点看这个,更细致):http ...

  7. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  8. 搜索ABAP程序代码中的字符串

    标准程序名:RPR_ABAP_SOURCE_SCAN /BEV1/NERM07DOCS

  9. SQL优化小结

    一 背景      客户数据库经常出现死锁.超时.查询慢等问题,数据库mssql,数据量主要表大概上千W. 二 收集信息      首先是要找出IO大.查询慢.使用频率高的脚本.直接用Profiler ...

  10. mysql语句优化技巧

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引 ...