LightOJ 1026 - Critical Links (tarjan 找桥)

题意:找出所有的桥,并输出。

水题,中午肚子痛,查不出错了,晚上一看,tarjan写错了!!!!


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
const int N = 10009;
const int M = 100009;
struct LT{
    int to,nex;
} L[M<<1];
int F[N],cnt;
void add(int f,int t)
{
    L[cnt].nex = F[f];
    L[cnt].to = t;
    F[f] =cnt++;
}
int n,m;
struct nod{
    int f,t;
    bool operator<(const nod tt)const {
        return f<tt.f||(f==tt.f&&t<tt.t);
    }
} ans[M];
int count_ans;
int dfn[N],low[N],ind;
void tdfs(int k,int f)
{
    low[k] = dfn[k] = ind++;
    for(int i=F[k];i;i=L[i].nex)
    {
        int to = L[i].to;
        if(!dfn[to])
        {
            tdfs(to,k);
            low[k] = min(low[k],low[to]);
            if(dfn[k]<low[to])
            {
                ans[count_ans].f = k;
                ans[count_ans].t = to;
                if(k>to)
                swap(ans[count_ans].f,ans[count_ans].t);
                count_ans++;
            }
        }else if(to!=f)
        low[k] = min(low[k],dfn[to]);
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    int cas,T=1;
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d",&n);
        int a,b,c;
        memset(F,0,sizeof(F));
        cnt = 1;count_ans = 0;
        m = -1;
        char ch[50];
        for(int i=0;i<n;i++)
        {
            scanf("%d%s",&a,ch);
            if(m<a) m=a;
            sscanf(ch+1,"%d",&b);
            while(b--)
            {
                scanf("%d",&c);
                add(a,c);
            }
        }
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        ind = 1;
        for(int i=0;i<=m;i++)
        if(!dfn[i]) tdfs(i,-1);
        sort(ans,ans+count_ans);
        printf("Case %d:\n",T++);
        printf("%d critical links\n",count_ans);
        for(int i=0;i<count_ans;i++)
        {
            printf("%d - %d\n",ans[i].f,ans[i].t);
        }
    }
    return 0;
}


你可能感兴趣的:(LightOJ 1026 - Critical Links (tarjan 找桥))