UVA 796 Critical Links (桥)

来源:互联网 发布:淘宝助理密码错误 编辑:程序博客网 时间:2024/05/17 23:38

题意:

给出一张无向图,按顺序输出图中的桥


套模版搞搞,熟悉一下桥


#include<cstring>#include<string>#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#include<map>#include<cstdlib>#include<cmath>#include<vector>//#pragma comment(linker, "/STACK:1024000000,1024000000");using namespace std;#define INF 0x3f3f3f3f#define maxn 200004int lay,dfn[maxn],low[maxn];int fir[maxn],nex[maxn],v[maxn],e_max;void init(){    memset(fir,-1,sizeof fir);    e_max=0;}void add_edge(int s,int t){    int e=e_max++;    v[e]=t;    nex[e]=fir[s];    fir[s]=e;}struct node{    int x,y;    friend bool operator < (node A, node B)    {        if(A.x==B.x) return A.y<B.y;        return A.x<B.x;    }    friend bool operator == (node A,node B)    {        if(A.x==B.x&&A.y==B.y) return true;        if(A.x==B.y&&A.y==B.x) return true;        return false;    }};vector<node>ans;void tarjandfs(int k,int pre){    dfn[k]=++lay;    low[k]=dfn[k];    int son=0;    for(int i=fir[k];~i;i=nex[i])    {        int e=v[i];        if(e==pre) continue;        if(!dfn[e])        {            son++;            tarjandfs(e,k);            if(low[k]>low[e]) low[k]=low[e];            if(low[e]>dfn[k])            {                node temp;                temp.x=k;                temp.y=e;                if(temp.x>temp.y) swap(temp.x,temp.y);                ans.push_back(temp);            }        }        else low[k]=min(low[k],dfn[e]);    }}void tarjan(int n){    ans.clear();    lay=0;    memset(dfn,0,sizeof fir);    for(int i=0;i<n;i++)    {       if(!dfn[i]) tarjandfs(i,i);    }    sort(ans.begin(),ans.end());    ans.erase(unique(ans.begin(),ans.end()),ans.end());    printf("%d critical links\n",ans.size());    for(int i=0;i<ans.size();i++)    {        printf("%d - %d\n",ans[i].x,ans[i].y);    }    puts("");}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        init();        for(int i=0;i<n;i++)        {            int st,l;            scanf("%d (%d)",&st,&l);            for(int j=0;j<l;j++)            {                int a;                scanf("%d",&a);                add_edge(st,a);            }        }        tarjan(n);    }    return 0;}


0 0