UVA 796 Critical Links

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/C

UVA链接地址

Root    

796 - Critical Links

Time limit: 3.000 seconds


题目大意:给你一个网络要求这里面的桥。
输入数据:
n 个点
点的编号  (与这个点相连的点的个数m)  依次是m个点的

输入到文件结束。
桥输出的时候需要排序

知识汇总:
桥:   无向连通图中,如果删除某条边后,图变成不连通了,则该边为桥。
求桥:
在求割点的基础上吗,假如一个边没有重边(重边 1-2, 1->2 有两次,那么 1->2 就是有两条边了,那么 1->2就不算是桥了)。
当且仅当 (u,v) 为父子边,且满足 dfn[u] < low[v]

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>
#include <vector>
#include <map>

using namespace std;

#define N 12005
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8

struct node
{
    int x, y;
    bool friend operator < (node a, node b)
    {
        if (a.x == a.y) return a.y < b.y;
        return a.x < b.x;
    }
}bridge[N];

vector <int> G[N];
int n, low[N], dfn[N], f[N], Time, ans;

void Init ();
void solve ();
void tarjan (int u, int fa);

int main ()
{
    while (~scanf ("%d", &n))
    {
        Init ();
        for (int i=0; i<n; i++)
        {
            int a, b, m;
            scanf ("%d (%d)", &a, &m);
            while (m--)
            {
                scanf ("%d", &b);
                G[a].push_back (b);
                G[b].push_back (a);
            }
        }
        solve ();
    }
    return 0;
}

void Init ()
{
    memset (low, 0, sizeof (low));
    memset (dfn, 0, sizeof (dfn));
    memset (f, 0, sizeof (f));
    Time = 0;
    for (int i=0; i<n; i++)
        G[i].clear ();
}

void solve ()
{
    ans = 0;
    for (int i=0; i<n; i++)
        if (!low[i])
            tarjan (i, -1);

    for (int i=0; i<n; i++)
    {
        int v = f[i];
        if (v != -1 && dfn[v] < low[i])
        {
            bridge[ans].x = i;
            bridge[ans].y = v;
            if (bridge[ans].x > bridge[ans].y)
                swap (bridge[ans].x, bridge[ans].y);
            ans++;
        }
    }

    sort (bridge, bridge+ans);

    printf ("%d critical links\n", ans);
    for (int i=0; i<ans; i++)
        printf ("%d - %d\n", bridge[i].x, bridge[i].y);
    puts ("");
}

void tarjan (int u, int fa)
{
    low[u] = dfn[u] = ++Time;
    f[u] = fa;
    int len = G[u].size (), v;

    for (int i=0; i<len; i++)
    {
        v = G[u][i];
        if (!low[v])
        {
            tarjan (v, u);
            low[u] = min (low[u], low[v]);
        }
        else if (fa != v)
            low[u] = min (low[u], dfn[v]);
    }
}


你可能感兴趣的:(UVA 796 Critical Links)