*无向图求割点+点双连通分量——Tarjan

今天是2017/5/31,DCDCBigBig的第十五篇博文

最近来搞搞tarjan专题

割点

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
struct edge{
    int v,next;
}a[100001];
int t=0,n,s,ss,u,v,tim=0,sum=0,tot=0,head[100001],dfn[100001],low[100001];
bool f=false,used[100001],ff[100001];
void add(int u,int v){
    a[++tot].v=v;
    a[tot].next=head[u];
    head[u]=tot;
}
void tarjan(int u,int fa){
    int v;
    dfn[u]=low[u]=++tim;
    for(int tmp=head[u];tmp!=-1;tmp=a[tmp].next){
        v=a[tmp].v;
        if(!low[v]){
            tarjan(v,u);
            if(u==1)sum++;
            low[u]=min(low[u],low[v]);
            if(u!=1&&dfn[u]<=low[v]){
                ff[u]=true;
            }
        }else if(v!=fa){
            low[u]=min(low[u],dfn[v]);
        }
    }
}
void dfs(int u){
    int v;
    used[u]=true;
    for(int tmp=head[u];tmp!=-1;tmp=a[tmp].next){
        v=a[tmp].v;
        if(!used[v])dfs(v);
    }
}
int main(){
    while(scanf("%d",&s)&&s){
        printf("Network #%d\n",++t);
        memset(head,-1,sizeof(head));
        memset(ff,0,sizeof(ff));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        tot=n=sum=tim=0;
        f=false;
        scanf("%d",&ss);
        add(s,ss);
        add(ss,s);
        n=max(n,max(s,ss));
        scanf("%d",&s);
        while(s){
            scanf("%d",&ss);
            add(s,ss);
            add(ss,s);
            n=max(n,max(s,ss));
            scanf("%d",&s);
        }
        tarjan(1,-1);
        if(sum>1)ff[1]=true;
        for(int i=1;i<=n;i++){
            if(ff[i]){
                memset(used,0,sizeof(used));
                f=used[i]=true;
                sum=0;
                for(int j=1;j<=n;j++){
                    if(!used[j]){
                        sum++;
                        dfs(j);
                    }
                }
                printf("  SPF node %d leaves %d subnets\n",i,sum);  
            }
        }
        if(!f){
            printf("  No SPF nodes\n");
        }
        printf("\n");
    }
    return 0;
}
/*
1 2
5 4
3 1
3 2
3 4
3 5
0

1 2
2 3
3 4
4 5
5 1
0

1 2
2 3
3 4
4 6
6 3
2 5
5 1
0

0
--------
Network #1
  SPF node 3 leaves 2 subnets

Network #2
  No SPF nodes

Network #3
  SPF node 2 leaves 2 subnets
  SPF node 3 leaves 2 subnets
*/

点双连通分量

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
using namespace std;
struct edge{
    int v,next;
}a[200001];
struct edge1{
    int u,v;
};
int n,m,u,v,tot=0,tim=0,bcctot=0,bccnum[100001],head[100001],dfn[100001],low[100001],iscut[100001];
vector<int>ans[100001];
stack<edge1>s;
void add(int u,int v){
    a[++tot].v=v;
    a[tot].next=head[u];
    head[u]=tot;
}
void tarjan(int u,int fa){
    edge1 t;
    int v,son;
    dfn[u]=low[u]=++tim;
    for(int tmp=head[u];tmp!=-1;tmp=a[tmp].next){
        v=a[tmp].v;
        if(v==fa)continue;
        t.u=u;
        t.v=v;
        if(!dfn[v]){
            s.push(t);
            son++;
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]){
                iscut[u]=true;
                ans[++bcctot].clear();
                while(true){
                    edge1 tt=s.top();
                    s.pop();
                    if(bccnum[tt.u]!=bcctot){
                        ans[bcctot].push_back(tt.u);
                        bccnum[tt.u]=bcctot;
                    }
                    if(bccnum[tt.v]!=bcctot){
                        ans[bcctot].push_back(tt.v);
                        bccnum[tt.v]=bcctot;
                    }
                    if(tt.u==u&&tt.v==v)break;
                }
            }
        }else if(dfn[v]<low[u]){
            s.push(t);
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(fa<0&&son==1)iscut[u]=false;
}
int main(){
    memset(iscut,0,sizeof(iscut));
    memset(head,-1,sizeof(head));
    memset(low,0,sizeof(low));
    memset(bccnum,0,sizeof(bccnum));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    tarjan(1,-1);
    for(int i=1;i<=bcctot;i++){
        printf("%d:",i);
        for(int j=0;j<ans[i].size();j++){
            printf("%d ",ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}
/*
6 7 
1 2 
2 3 
1 3 
3 4 
4 5 
3 5 
5 6 
--------
1:5 6
2:4 3 5
3:2 1 3
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值