所以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. 简单LCS HDU_1503

    学了一下最长公共子串,它是属于dp里面的 dp=max{(i,j-1),(i-1,j),(i-1,j-1)+d}问题,不得不说,规划方向确实厉害,当然这只适用于两个字符串匹配的问题,n个字符串的话,我 ...

  2. 干货 | 基于Go SDK操作京东云对象存储OSS的入门指南

    前言 本文介绍如何使用Go语言对京东云对象存储OSS进行基本的操作,帮助客户快速通过Go SDK接入京东云对象存储,提高应用开发的效率. 在实际操作之前,我们先看一下京东云OSS的API接口支持范围和 ...

  3. 《打造扛得住的MySQL数据库架构》第3章 MySQL基准测试

    3-1 什么是基准测试 测量系统性能,优化是否有效?MySQL基准测试. 定义:基准测试是一种测量和评估软件性能指标的活动,用于建立某个时刻的性能基准,以便当系统发生软硬件 变化时重新进行基准测试以评 ...

  4. bootstrap 基础表单

    表单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文本域和按钮等.其中每个控件所起的作用都各不相同,而且不同的浏览器对表单控件渲染的风格都各有不同. ☑   LESS版本:对应源文 ...

  5. Springboot数据校验

    SpringBoot中使用了Hibernate-validate校验框架 1.在实体类中添加校验规则 校验规则: @NotBlank: 判断字符串是否为null或者是空串(去掉首尾空格).@NotEm ...

  6. JDK11 JAVA11下载安装与快速配置环境变量教程

    https://blog.csdn.net/weixin_40928253/article/details/83590136 1.到Oracle官网下载jdk11,并安装.搜索“jdk",选 ...

  7. 谈谈 Act 的依赖注入 和 模板输出 - 回答 drinkjava 同学提问

    1. 背景 依赖注入工具 jBeanBox 的作者 drinkjava 同学最近在 Actframework gitee 项目 的提出了如下评论: 你这个DI工具的出发点可能有问题,一个MVC工具为什 ...

  8. selector的使用,android:clickable="true"

    <ImageView android:id="@+id/patrol_buzzer_btn" android:layout_width="80dp" an ...

  9. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  10. tensorflow实现卷积层的几种方式

    #coding:utf-8 #第一种实现 tf.nn import tensorflow as tf import tensorflow.contrib.slim as slim tf.reset_d ...