zoukankan      html  css  js  c++  java
  • UVA 796 Critical Links

    #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
    ", ans);
        for (int i=0; i<ans; i++)
            printf ("%d - %d
    ", 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]);
        }
    }
  • 相关阅读:
    PHP js使用ajax异步处理方式请求PHP,解决数组中文乱码
    PHP Apache服务配置
    opencv高斯背景建模
    Opencv,腐蚀,膨胀,轮廓检测,轮廓外接多边形
    opencv删除二值图中较小的噪点色块
    opencv图像操作
    opencv统计二值图黑白像素个数
    JAVA常用工具类
    Netty4 学习笔记之四: Netty HTTP服务的实现
    分享一些JAVA相关资源
  • 原文地址:https://www.cnblogs.com/mengzhong/p/4705778.html
Copyright © 2011-2022 走看看