zoj 2588 buring bridges (无向图求桥,含重边)

注意输出格式即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
#define N 10005
struct Edge{
    int v,next,id;
}edge[200010];
int pre[N],S[N],top,low[N],ans[200000],head[N],cnt,dfs_clock,bcc_cnt,bccno[N],bridge;

inline void add(int u,int v,int id){
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    edge[cnt].id=id;
    head[u]=cnt++;
}
void dfs(int u,int fa){
    pre[u]=low[u]=++dfs_clock;
    S[top++]=u;
    for(int i=head[u];~i;i=edge[i].next){
        if(i==(fa^1)) continue;
        int v=edge[i].v;
        if(!pre[v]){
            dfs(v,i);
            low[u]=min(low[u],low[v]);
            if(low[v]>pre[u]) ans[++bridge]=edge[i].id;
        }
        else if(!bccno[v]) low[u]=min(low[u],pre[v]);
    }
    if(low[u]==pre[u]){
        ++bcc_cnt;
        for(;;){
            int x=S[--top];
            bccno[x]=bcc_cnt;
            if(x==u) break;
        }
    }
}

inline void find_bcc(int n){
    memset(pre,0,sizeof(pre));
    memset(bccno,0,sizeof(bccno));
    dfs_clock=bcc_cnt=bridge=top=0;
    for(int i=0;i<n;++i) if(!pre[i]) dfs(i,-1);
}

int main()
{
    int i,j,n,m,x,y,flag=0,t;
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=1;i<=m;++i){
            scanf("%d%d",&x,&y);
            add(--x,--y,i);
            add(y,x,i);
        }
        find_bcc(n);
        if(flag) puts("");
        else flag=1;
        printf("%d\n",bridge);
        sort(ans+1,ans+1+bridge);
        for(i=1;i<bridge;++i) printf("%d ",ans[i]);
        if(bridge)
        printf("%d\n",ans[i]);
    }
    return 0;
}


你可能感兴趣的:(zoj 2588 buring bridges (无向图求桥,含重边))