首页 > 代码库 > UVA 796 - Critical Links (求桥按序输出)

UVA 796 - Critical Links (求桥按序输出)

tanjar求图中的桥,然后排序输出。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

const int eps=0.00000001;
const int maxn=10005;
const int maxm=200006;

int first[maxn],low[maxn],dfn[maxn],fa[maxn],if_add[maxn];
int nex[maxm],u[maxm],v[maxm],w[maxm];
int belong[maxn],stck[maxn];
int n,m,ecnt,indx,bridge,block,top,ecntt;
bool ins[maxn],vcut[maxn],ecut[maxm];

void tarjan(int s,int pre)
{
	dfn[s]=low[s]=++indx;
	stck[top++]=s;
	ins[s]=1;
	int son=0;
	for(int e=first[s];~e;e=nex[e])
    {
        if(v[e]==pre)continue;
        if(!dfn[v[e]])
        {
            son++;
            tarjan(v[e],s);
            low[s]=min(low[s],low[v[e]]);
            if(low[v[e]]>dfn[s])
            {
                bridge++;
                ecut[e]=1;
                ecut[e^1]=1;
            }
            if(s!=pre&&low[v[e]]>=dfn[s])
            {
                vcut[s]=1;
                if_add[s]++;
            }
        }
        else if(ins[v[e]])low[s]=min(low[s],dfn[v[e]]);
    }
    if(s==pre&&son>1)vcut[s]=1,if_add[s]=son-1;
    if(low[s]==dfn[s])
    {
        int vv;
        block++;
        do
        {
            vv=stck[--top];
            ins[vv]=0;
            belong[vv]=block;
        }while(vv!=s);
    }
}
void add_(int a,int b,int c)
{
	u[ecnt]=a;
	v[ecnt]=b;
	w[ecnt]=c;
	ecut[ecnt]=0;
	nex[ecnt]=first[a];
	first[a]=ecnt++;
}
void solve()
{
    clr(dfn,0);
    clr(if_add,0);
    clr(vcut,0);
    indx=bridge=block=top=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i,i);
}

struct node
{
    int a,b;
}da[maxm];
bool cmp(const node &c,const node &d)
{
    if(c.a==d.a)return c.b<d.b;
    return c.a<d.a;
}
int main()
{
    int a,b,c,cas=1;
    while(~scanf("%d",&n))
    {
        clr(first,-1);ecnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%*c%*c%d%*c)",&a,&c);a++;
            while(c--)
            {
                scanf("%d",&b);b++;
                if(a!=b)add_(a,b,0),add_(b,a,0);
            }
        }
        solve();
        printf("%d critical links\n",bridge);
        int ans=0;
        for(int i=0;i<ecnt;i++)
            if(ecut[i]&&u[i]<v[i])da[ans].a=u[i]-1,da[ans++].b=v[i]-1;
        sort(da,da+ans,cmp);
        for(int i=0;i<ans;i++)
            printf("%d - %d\n",da[i].a,da[i].b);
        printf("\n");
    }
}


UVA 796 - Critical Links (求桥按序输出)