UVA 796 连通图求桥

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86270#problem/C#include<iostream>

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
#define min(a, b) a<b?a:b;
#define swap(a, b) {t=a; a=b; b=t;};
#define N 10105
vector<vector<int> > G;
struct node
{
    int x, y;
} maps[N];
bool cmp(node a, node b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
int n, visit[N], rode[N], father[N];
int Time, ans;
void Init()
{
    G.clear();
    G.resize(n);
    Time=ans=0;
    memset(visit, 0, sizeof(visit));
    memset(rode, 0, sizeof(rode));
    memset(father, 0, sizeof(father));
}
void Tarjan(int u, int fu)
{
    visit[u]=rode[u]=++Time;
    father[u]=fu;
    int len=G[u].size();
    int v;
    for(int i=0; i<len; i++)
    {
        v=G[u][i];
        if(!visit[v])
        {
            Tarjan(v, u);
            rode[u]=min(rode[v], rode[u]);
        }
        else if(fu!=v)
            rode[u]=min(visit[v], rode[u]);
    }
}
void solve()
{
    int k=0, t;
    for(int i=0; i<n; i++)
        if(!visit[i])
            Tarjan(i, -1);
    for(int i=0; i<n; i++)
    {
        int v=father[i];
        if(v!=-1&&visit[v]<rode[i])
        {
            maps[k].x=v;
            maps[k].y=i;
            if(maps[k].x>maps[k].y)
                swap(maps[k].x, maps[k].y);
            k++;
        }
    }
    sort(maps, maps+k, cmp);
    ans=k;
    printf("%d critical links\n", ans);
    for(int i=0; i<k; i++)
    {
        printf("%d - %d\n", maps[i].x, maps[i].y);
    }
    printf("\n");
}
int main()
{
    while(scanf("%d", &n)!=EOF)
    {
        Init();
        for(int i=0; i<n; i++)
        {
            int a, b, l;
            scanf("%d (%d)", &a, &l);
            while(l--)
            {
                scanf("%d", &b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
        }
        solve();
    }
    return 0;
}
posted @ 2015-08-07 11:27  花开须臾  阅读(166)  评论(0编辑  收藏  举报