所以Tarjan到底怎么读

强连通分量

  • 基本概念

    • 强连通 如果两个顶点可以相互通达,则称两个顶点强连通
    • 强连通图 如果有向图G的每两个顶点都强连通,称G是一个强连通图。
  • Tarjan

    Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。
  • 定义
    • dfn[u]: 为节点u搜索的次序编号(时间戳)
    • low[u]: 为u或u的子树能够追溯到的最早的栈中节点的次序号。
  • 判定
    • low[u]:=min(low[u],dfn[v])——(u,v)为后向边,v不是u的子树;
    • low[u]:=min(low[u],low[v])——(u,v)为树枝边,v为u的子树;
    • 当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量。

模板题:信息传递

题目描述

有n个同学(编号为1到n)正在玩一个信息传递的游戏。在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学。

游戏开始时,每人都只知道自己的生日。之后每一轮中,所有人会同时将自己当前所知的生日信息告诉各自的信息传递对象(注意:可能有人可以从若干人那里获取信息,但是每人只会把信息告诉一个人,即自己的信息传递对象)。当有人从别人口中得知自己的生日时,游戏结束。请问该游戏一共可以进行几轮?

输入

输入共2行。

第1行包含1个正整数n表示n个人。 n ≤ 200000

第2行包含n个用空格隔开的正整数T1,T2,……,Tn,其中第i个整数Ti示编号为i的同学的信息传递对象是编号为Ti的同学,Ti≤n且Ti≠i。数据保证游戏一定会结束。

输出

输出共 1 行,包含 1 个整数,表示游戏一共可以进行多少轮。

样例输入

5

2 4 2 3 1

样例输出

3

提示

游戏的流程如图所示。当进行完第 3 轮游戏后,4 号玩家会听到 2 号玩家告诉他自

己的生日,所以答案为 3。当然,第 3 轮游戏后,2 号玩家、3 号玩家都能从自己的消息

来源得知自己的生日,同样符合游戏结束的条件。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define met(a,x) memset(a,x,sizefo(a))
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2e5+10;
int n,tol,dep;
int head[maxn],low[maxn],dfn[maxn],ans=maxn;
bool vis[maxn];
stack<int>st;
struct edge
{
int v,next;
}e[maxn];
void addedge(int u,int v)
{
e[++tol].next=head[u];
e[tol].v=v;
head[u]=tol;
}
void tarjian(int u)
{
dfn[u]=low[u]=++dep;
st.push(u);
vis[u]=true;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
if(!dfn[v]){
tarjian(v);
low[u]=min(low[u],low[v]);
}else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int cnt=0;
while(1){
int now=st.top();
st.pop();
vis[u]=0;
cnt++;
if(now==u)break;
}
if(cnt>1) ans=min(ans,cnt);
}
}
int main()
{
scanf("%d",&n);
int v;
for(int i=1;i<=n;i++){
scanf("%d",&v);
addedge(i,v);
}
for(int i=1;i<=n;i++){
if(!dfn[i]){
tarjian(i);
}
}
printf("%d\n",ans);
return 0;
}

求割点

  • 基本概念

    • 割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点。
    • 若low[v]>=dfn[u],则u为割点。因low[v]>=dfn[u],则说明v通过子孙无法到达u的祖先。那么对于原图,去掉u后,必然会分成两个子图。
  • 模板
 /*
* 求无向图的割点和桥
* 可以找出割点和桥,求删掉每个点后增加的连通块。
* 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重
*/
const int MAXN = 1e5+10;
const int MAXM = 2e5+10;
struct Edge
{
int to,next;
bool cut;
} edge[MAXM];
int head[MAXN],tot=1;
int Low[MAXN],DFN[MAXN],Stack[MAXN];
int Index,top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];
int bridge;
void addedge(int u,int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = false;
head[u] = tot++;
}
void Tarjan(int u,int pre)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = 0;
int pre_cnt=0;
for(int i=head[u]; i; i=edge[i].next)
{
v = edge[i].to;
if(v == pre && pre_cnt == 0)
{
pre_cnt++;
continue;
}
if(!DFN[v])
{
son++;
Tarjan(v,u);
if(Low[u] > Low[v])
Low[u] = Low[v];
if(Low[v] > DFN[u])//桥
{
bridge++;
edge[i].cut = true;
edge[i^1].cut = true;
}
if(u != pre && Low[v] >= DFN[u])//割点
{
cut[u] = true;
add_block[u]++;
}
}
else if( Low[u] > DFN[v])
Low[u] = DFN[v];
}
if(u == pre && son > 1)//割点
{
cut[u] = true;
}
if(u == pre)
add_block[u] = son - 1;
Instack[u] = false;
top--;
}

求桥

  • 基本概念

    • 桥(割边):删掉它之后,图必然会分裂为两个或两个以上的子图。
    • 原理:若low[v]>dfn[u],则(u,v)为桥。由割点同理可得。但是由于可能存在重边,需要把一条无向边拆成的两条标号相同的有向边,记录每个点的父亲到它的边的标号,如果边(u,v)是v的父亲边,就不能用dfn[u]更新low[v]。这样如果遍历完v的所有子节点后,发现low[v]=dfn[v],说明u的父亲边(u,v)为割边。

点双连通分量

对于点双连通分支,实际上在求割点的过程中就能顺便把每个点双连通分支求出。建立一个

栈,存储当前双连通分支,在搜索图时,每找到一条树枝边或后向边(非横叉边),就把这条

边加入栈中。如果遇到某时满足DFN(u)<=Low(v),说明u 是一个割点,同时把边从栈顶一

个个取出,直到遇到了边(u,v),取出的这些边与其关联的点,组成一个点双连通分支。割点

可以属于多个点双连通分支,其余点和每条边只属于且属于一个点双连通分支。

模板题 Go around the Labyrinth

题目描述

Explorer Taro got a floor plan of a labyrinth. The floor of this labyrinth is in the form of a two-dimensional grid. Each of the cells on the floor plan corresponds to a room and is indicated whether it can be entered or not. The labyrinth has only one entrance located at the northwest corner, which is upper left on the floor plan. There is a treasure chest in each of the rooms at other three corners, southwest, southeast, and northeast. To get a treasure chest, Taro must move onto the room where the treasure chest is placed.

Taro starts from the entrance room and repeats moving to one of the enterable adjacent rooms in the four directions, north, south, east, or west. He wants to collect all the three treasure chests and come back to the entrance room. A bad news for Taro is that, the labyrinth is quite dilapidated and even for its enterable rooms except for the entrance room, floors are so fragile that, once passed over, it will collapse and the room becomes not enterable. Determine whether it is possible to collect all the treasure chests and return to the entrance.

输入

The input consists of at most 100 datasets, each in the following format.

N M

c1,1...c1,M

...

cN,1...cN,M

The first line contains N, which is the number of rooms in the north-south direction, and M, which is the number of rooms in the east-west direction. N and M are integers satisfying 2 ≤ N ≤ 50 and 2 ≤ M ≤ 50. Each of the following N lines contains a string of length M. The j-th character ci,j of the i-th string represents the state of the room (i,j), i-th from the northernmost and j-th from the westernmost; the character is the period ('.') if the room is enterable and the number sign ('#') if the room is not enterable. The entrance is located at (1,1), and the treasure chests are placed at (N,1), (N,M) and (1,M). All of these four rooms are enterable. Taro cannot go outside the given N × M rooms.

The end of the input is indicated by a line containing two zeros.

输出

For each dataset, output YES if it is possible to collect all the treasure chests and return to the entrance room, and otherwise, output NO in a single line.

这题是训练赛的一道,算是裸模板题

左上角的点能到达右上角,右下角,左下角,然后回到原点。这题采用Tarjian求,然后判断四个角属不属于一个双连通分量,然后把割点也标记一下。因为割点属于任何一双连通分量。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxm=1e5+10;
const int maxn=5e5+10;
struct Edge
{
int v,next;
}e[maxm];
int head[maxn],tol,cur,top,block;
bool instack[maxn];
int low[maxn],dfn[maxn],Stack[maxn],belong[maxn],cnt;
void addedge(int u,int v)
{
e[++tol].v=v; e[tol].next=head[u];
head[u]=tol;
}
void Tarjian(int u,int pre)
{
int v;
low[u]=dfn[u]=++cur;
Stack[top++]=u;
instack[u]=true;
for(int i=head[u];i;i=e[i].next){
v=e[i].v;
if(v==pre) continue;
if(!dfn[v]){
Tarjian(v,u);
if(low[u]>low[v])
low[u]=low[v];
if(low[v]>=dfn[u]){
block++;
int vn;
do{
vn=Stack[--top];
belong[vn]=block;
instack[vn]=false;
}while(vn!=v);
belong[u]=block;
}
}
else if(instack[v]&&low[u]>dfn[v]){
low[u]=dfn[v];
} }
}
int num[55][55];
void init()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(head,0,sizeof(head));
memset(belong,0,sizeof(belong));
memset(instack,0,sizeof(instack));
memset(num,0,sizeof(num));
tol=top=cur=block=0;
cnt=0;
}
int mapp[55][55]; int main(){
int n,m;
char s;
while(scanf("%d%d",&n,&m)&&n&&m){
init();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf(" %c",&s);
num[i][j]=++cnt;
if(s=='.'){
mapp[i][j]=1;
}
else{
mapp[i][j]=0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(num[i-1][j]&&mapp[i-1][j]){
addedge(num[i][j],num[i-1][j]);
}
if(num[i+1][j]&&mapp[i+1][j]){
addedge(num[i][j],num[i+1][j]);
}
if(num[i][j+1]&&mapp[i][j+1]){
addedge(num[i][j],num[i][j+1]);
}
if(num[i][j-1]&&mapp[i][j-1]){
addedge(num[i][j],num[i][j-1]);
}
}
}
for(int i=1;i<=cnt;i++)if(!dfn[i])
Tarjian(i,-1);
int a,b,c,d;
a=belong[num[1][1]];
b=belong[num[1][m]];
c=belong[num[n][1]];
d=belong[num[n][m]];
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
if(a==b&&b==c&&c==d){
puts("YES");
}
else puts("NO");
}
return 0;
}

[学习笔记]连通分量与Tarjan算法的更多相关文章

  1. 【转】BYV--有向图强连通分量的Tarjan算法

    转自beyond the void 的博客: https://www.byvoid.com/zhs/blog/scc-tarjan 注:红色为标注部分 [有向图强连通分量] 在有向图G中,如果两个顶点 ...

  2. 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集

    机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...

  3. 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析

    机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...

  4. 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)

    机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...

  5. [ML学习笔记] 朴素贝叶斯算法(Naive Bayesian)

    [ML学习笔记] 朴素贝叶斯算法(Naive Bayesian) 贝叶斯公式 \[P(A\mid B) = \frac{P(B\mid A)P(A)}{P(B)}\] 我们把P(A)称为"先 ...

  6. Effective STL 学习笔记 31:排序算法

    Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  7. HMM模型学习笔记(前向算法实例)

    HMM算法想必大家已经听说了好多次了,完全看公式一头雾水.但是HMM的基本理论其实很简单.因为HMM是马尔科夫链中的一种,只是它的状态不能直接被观察到,但是可以通过观察向量间接的反映出来,即每一个观察 ...

  8. 【视频编解码·学习笔记】8. 熵编码算法:基本算法列举 & 指数哥伦布编码

    一.H.264中的熵编码基本方法: 熵编码具有消除数据之间统计冗余的功能,在编码端作为最后一道工序,将语法元素写入输出码流 熵解码作为解码过程的第一步,将码流解析出语法元素供后续步骤重建图像使用 在H ...

  9. poj1523求割点以及割后连通分量数tarjan算法应用

    无向图,双向通道即可,tarjan算法简单应用.点u是割点,条件1:u是dfs树根,则u至少有2个孩子结点.||条件2:u不是根,dfn[u]=<low[v],v是u的孩子结点,而且每个这样的v ...

随机推荐

  1. JDBC批处理方法

    每次新建Connection的时候相当于建了一座桥,每次一辆车(PreparedStatement)运送货物(ResultSet)成本太高! 每次都建立网络连接的时间远远大于本地的时间,为了处理大量的 ...

  2. HDU—4699 Editor 双向链表+子集和

    惨.今天聪哥选了2013 多校10做训练,结果一题都没做出来.这个题目是数据结构,正好是我强项 如果只是插入 删除 光标左右移动,那都小菜,用链表全解决,关键是那个Q k 要求 a1到aq的连续子序列 ...

  3. Flink:动态表上的连续查询

    用SQL分析数据流 越来越多的公司在采用流处理技术,并将现有的批处理应用程序迁移到流处理或者为新的应用设计流处理方案.其中许多应用程序专注于分析流数据.分析的数据流来源广泛,如数据库交易,点击,传感器 ...

  4. Java 14 都要来了,为什么还有这么多人固守Java8?

    作者:刘欣 从Java 9开始,Java版本的发布就让人眼花缭乱了. 每隔6个月,都会冒出一个新版本出来,Java 10 , Java 11, Java 12, Java 13, 到2020年3月份, ...

  5. PHP 的变量类型,变量检测

    1.PHP的变量类型: 整型       浮点型 字符串 布尔型 数组 对象 null 资源类型 一个变量就是一个盒子,类型可以看做盒子的标签,变量的值就是盒子里的内容 null 是没有类型的空盒子, ...

  6. centos socket通信时 connect refused 主要是防火墙问题

    centos socket通信时 connect refused 主要是防火墙问题,可以关闭防火墙,或者开放程序中的端口

  7. ref与out区别

    ref与out   out.ref都是传递引用(内存地址),使用后都将改变原来参数的数值.   ref 当调用方法时,在方法中会对ref传入的参数数值进行改变,若使用ref参数,则方法定义和调用方法都 ...

  8. rewrite例子集合

    在 httpd 中将一个域名转发到另一个域名 虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记.这时需要将原来的域名 webhosting-world.com, 以及论坛 ...

  9. HTML超链接实例介绍

    <html><head><title>第六节课</title><meta charset="UTF-8"></he ...

  10. VNC/XRDP/XDMCP尝试

    (记得安装X Window System等 可参考链接https://www.linuxidc.com/Linux/2017-10/147646.htm) 看本文档时可以参考 https://blog ...