[学习笔记]连通分量与Tarjan算法
所以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算法的更多相关文章
- 【转】BYV--有向图强连通分量的Tarjan算法
转自beyond the void 的博客: https://www.byvoid.com/zhs/blog/scc-tarjan 注:红色为标注部分 [有向图强连通分量] 在有向图G中,如果两个顶点 ...
- 机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集
机器学习实战(Machine Learning in Action)学习笔记————08.使用FPgrowth算法来高效发现频繁项集 关键字:FPgrowth.频繁项集.条件FP树.非监督学习作者:米 ...
- 机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析
机器学习实战(Machine Learning in Action)学习笔记————07.使用Apriori算法进行关联分析 关键字:Apriori.关联规则挖掘.频繁项集作者:米仓山下时间:2018 ...
- 机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN)
机器学习实战(Machine Learning in Action)学习笔记————02.k-邻近算法(KNN) 关键字:邻近算法(kNN: k Nearest Neighbors).python.源 ...
- [ML学习笔记] 朴素贝叶斯算法(Naive Bayesian)
[ML学习笔记] 朴素贝叶斯算法(Naive Bayesian) 贝叶斯公式 \[P(A\mid B) = \frac{P(B\mid A)P(A)}{P(B)}\] 我们把P(A)称为"先 ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- HMM模型学习笔记(前向算法实例)
HMM算法想必大家已经听说了好多次了,完全看公式一头雾水.但是HMM的基本理论其实很简单.因为HMM是马尔科夫链中的一种,只是它的状态不能直接被观察到,但是可以通过观察向量间接的反映出来,即每一个观察 ...
- 【视频编解码·学习笔记】8. 熵编码算法:基本算法列举 & 指数哥伦布编码
一.H.264中的熵编码基本方法: 熵编码具有消除数据之间统计冗余的功能,在编码端作为最后一道工序,将语法元素写入输出码流 熵解码作为解码过程的第一步,将码流解析出语法元素供后续步骤重建图像使用 在H ...
- poj1523求割点以及割后连通分量数tarjan算法应用
无向图,双向通道即可,tarjan算法简单应用.点u是割点,条件1:u是dfs树根,则u至少有2个孩子结点.||条件2:u不是根,dfn[u]=<low[v],v是u的孩子结点,而且每个这样的v ...
随机推荐
- UVA 10054 The Necklace 转化成欧拉回路
题意比较简单,给你n个项链碎片,每个碎片的两半各有一种颜色,最后要把这n个碎片串成一个项链,要求就是相邻碎片必须是同种颜色挨着. 看了下碎片总共有1000个,颜色有50种,瞬间觉得普通方法是无法在可控 ...
- HZNU-ACM寒假集训Day3小结 搜索
简单搜索 1.DFS UVA 548 树 1.可以用数组方式实现二叉树,在申请结点时仍用“动态化静态”的思想,写newnode函数 2.给定二叉树的中序遍历和后序遍历,可以构造出这棵二叉树,方法是根据 ...
- iOS基础——通过案例学知识之xib、plist、mvc
透过案例学习xib的使用.plist的使用.mvc在iOS的使用,今天要做的案例效果图 1.xib和nib xib文件可以被XCode编译成nib文件,xib文件本质上是一个xml文件,而nib文件就 ...
- Day 11:静态导入、增强for循环、可变参数的自动装箱与拆箱
jdk1.5新特性-------静态导入 静态导入的作用: 简化书写. 静态导入可以作用一个类的所有静态成员. 静态导入的格式:import static 包名.类名.静态的成员: 静态导入要注意的 ...
- 为什么ApplePay在中国一直火不起来?
今年7月,易观发布<中国第三方移动支付市场2018年第1季度监测报告>.报告显示,2018年第一季度支付宝以53.76%的市场份额占据移动支付头把交椅,腾讯金融(微信支付)则以38.95% ...
- FFmpeg命令大全(更新中)
1.视频抽取音频: ffmpeg -i 3.mp4 -vn -y -acodec copy 3.aacffmpeg -i 3.mp4 -vn -y -acodec copy 3.m4a
- 18 12 29 css background
background属性 属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个 ...
- MySQL表的几个简单查询语句
1. 创建数据库CREATE DATABASE database-name 2. 删除数据库drop database dbname 3. 创建新表create table tabname(co ...
- HDU 2795 Billboard 线段树活用
题目大意:在h*w 高乘宽这样大小的 board上要贴广告,每个广告的高均为1,wi值就是数据另给,每组数组给了一个board和多个广告,要你求出,每个广告应该贴在board的哪一行,如果实在贴不上, ...
- 对PHP-GC(垃圾回收)的一点理解
一直对php的垃圾回收机制不明不白故自己开贴记录下. 首先,说到垃圾回收,得先知道什么情况下才能产生垃圾. 如果一个变量refcount在增加,说明在被使用,不是垃圾. 如果一个变量的refcount ...