bzoj1051/poj2186

bzoj1051
每一个强连通块中的其中一个点能连到令一个点,那么这个强连通块中的每一个点都能连到这个点。
所以对块进行重构。那么,如果仅有一个块没有出度,那么这个块中点数的个数就是结果;否则就没有,即为0。

注意: 重构时..要考虑这条边的起点终点在不在一个联通块..在的话就不要加入这条边了【捂脸】

#include <cstdio>
#include <algorithm>
using namespace std; 
#define N 10010
struct node{int x,y,next;}edge[5*N];
int dfn[N],low[N],vis[N],h[N],h1[N],stack[N],belong[N],size[N],num=0,tot=0,scc=0,n,m,top=0,ans;
void tarjan(int x){
    dfn[x]=++tot;low[x]=tot;
    vis[x]=true;stack[++top]=x;
    for(int i=h[x];i;i=edge[i].next){
        int y=edge[i].y;
        if(!dfn[y]){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }else if(vis[y]) low[x]=min(low[x],dfn[y]);
    }
    if(low[x]==dfn[x]){
        scc++;vis[x]=0;belong[x]=scc;size[scc]=1;
        while(stack[top]!=x){
            vis[stack[top]]=0;
            belong[stack[top--]]=scc;
            size[scc]++;
        }top--;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        edge[++num].x=x;edge[num].y=y;edge[num].next=h[x];h[x]=num;
    }
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
    for(int i=1;i<=m;i++) if(belong[edge[i].x]!=belong[edge[i].y]) h1[belong[edge[i].x]]++;
    for(int i=1;i<=scc;i++)
        if(!h1[i]){
            if(!ans) ans=size[i];
            else{ans=0;break;}
        }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值