码迷,mamicode.com
首页 > 其他好文 > 详细

UVA610 - Street Directions(Tarjan)

时间:2014-10-13 17:35:19      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

题目链接


题意: 给出一张无向图,尽量多的使边成为单向边,改变之后的图仍然强连通。

思路:找出所有的桥,桥肯定是不能改变成为单向边,之后不是桥的边能组成n个连通块,按照dfs的顺序规定方向即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

const int MAXN = 10005;

struct Edge{
    int to, next;
    bool cut, vis;
}edge[MAXN * 10];

int head[MAXN], tot;
int Low[MAXN], DFN[MAXN];
int Index, top;
int bridge;

vector<pair<int, int> > ans; 

void addedge(int u, int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    edge[tot].vis = false;
    head[u] = tot++;
}

void Tarjan(int u, int pre) {
    int v;
    Low[u] = DFN[u] = ++Index;
    for (int i = head[u]; i != -1; i = edge[i].next) {
        v = edge[i].to;
        if (v == pre) continue; 
        if (edge[i].vis) continue;
        edge[i].vis = edge[i^1].vis = true;
        ans.push_back(make_pair(u, v));
        if (!DFN[v]) {
            Tarjan(v, u); 
            if (Low[u] > Low[v]) Low[u] = Low[v];
            if (Low[v] > DFN[u]) {
                ans.push_back(make_pair(v, u));
            }
        } 
        else if (Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
}

void init() {
    memset(head, -1, sizeof(head));
    memset(DFN, 0, sizeof(DFN)); 
    tot = Index = 0;
}

int main() {
    int n, m, t = 1; 
    while (scanf("%d%d", &n, &m)) {
        if (n == 0 && m == 0) break;
        init();
        int u, v;
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v); 
            addedge(u, v); 
            addedge(v, u);  
        }

        ans.clear();
        for (int i = 1; i <= n; i++) {
            if (!DFN[i])
                Tarjan(i, i);
        }

        printf("%d\n\n", t++);
        for (int i = 0; i < ans.size(); i++) 
            printf("%d %d\n", ans[i].first, ans[i].second);
        printf("#\n");
    }
    return 0;
}



UVA610 - Street Directions(Tarjan)

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://blog.csdn.net/u011345461/article/details/40046393

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!