[学习笔记]连通分量与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;
}
posted @ 2018-09-02 23:53  house_cat  阅读(453)  评论(0编辑  收藏  举报