zoukankan      html  css  js  c++  java
  • poj1515--Street Directions(边的双连通)

    给一个无向图,要求变成强连通的有向图,需要保留哪些边。

    边的双连通,对于桥保留两条边,其他的只保留一条边。求双连通的过程中记录保留边。

    /*********************************************
    Problem: 1515		User: G_lory
    Memory: 232K		Time: 32MS
    Language: C++		Result: Accepted
    **********************************************/
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define pk printf("KKK!
    ");
    
    using namespace std;
    
    const int N = 1005;
    const int M = N * N;
    
    
    struct Edge {
        int from, to, next;
        int cut;
    } edge[M];
    int cnt_edge;
    int head[N];
    void add_edge(int u, int v)
    {
        edge[cnt_edge].from = u;
        edge[cnt_edge].to = v;
        edge[cnt_edge].next = head[u];
        edge[cnt_edge].cut = 0;
        head[u] = cnt_edge++;
    }
    
    int dfn[N]; int idx;
    int low[N];
    
    int n, m;
    
    void tarjan(int u, int pre)
    {
        dfn[u] = low[u] = ++idx;
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if (edge[i].cut) continue;
            edge[i].cut = 1;
            edge[i ^ 1].cut = -1;
            if (v == pre) continue;
    
            if (!dfn[v])
            {
                tarjan(v, u);
                low[u] = min(low[u], low[v]);
                if (dfn[u] < low[v])
                    edge[i].cut = edge[i ^ 1].cut = 1;
            }
            else low[u] = min(low[u], dfn[v]);
        }
    }
    
    void init()
    {
        idx = cnt_edge = 0;
        memset(dfn, 0, sizeof dfn);
        memset(head, -1, sizeof head);
    }
    
    void solve()
    {
        for (int i = 0; i < cnt_edge; ++i)
        if (edge[i].cut == 1)
        printf("%d %d
    ", edge[i].from, edge[i].to);
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        int cas = 0;
        while (~scanf("%d%d", &n, &m))
        {
            if (n == 0 && m == 0) break;
            printf("%d
    
    ", ++cas);
            int u, v;
            init();
            for (int i = 0; i < m; ++i)
            {
                scanf("%d%d", &u, &v);
                add_edge(u, v);
                add_edge(v, u);
            }
            tarjan(1, -1);
            solve();
            printf("#
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    Web服务器和应用服务器的区别
    Java程序员应该知道的10个调试技巧
    java快捷键个人总结
    MySQL主从复制与读写分离
    CDN(内容分发网络)技术原理
    淘宝技术发展
    Monitor a Folder using Java
    Android eclipse中程序调试
    在学校教学Java是有争议的
    LINUX ubuntu 12.04 svn 1.6.7 苦逼的装了一个下午啊!! 绝对详细!正确!不吭爹!!
  • 原文地址:https://www.cnblogs.com/wenruo/p/5007998.html
Copyright © 2011-2022 走看看