poj 1438--One-way Traffic(边的双连通)

给定一个图,并给定边,a b c(c==1||c==2) 表示ab之间有c条边 求把尽可能多的有向边定向变成强联通图。

先把图当做无向图,加边时记录是否有边,dfs的时候不要把本没有的边用到!因为这个错了好多次。。。。然后就简单了,记录桥就可以了。

/**************************************************
Problem: 1438		User: G_lory
Memory: 5312K		Time: 657MS
Language: C++		Result: Accepted
**************************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#define pk printf("KKK!
");

using namespace std;

const int N = 2005;
const int M = N * N;


struct Edge {
    int from, to, next;
    int flag;   // 1代表单向边 0代表没边 2代表双向边
    int cut;
} edge[M];
int cnt_edge;
int head[N];
void add_edge(int u, int v, int c)
{
    edge[cnt_edge].from = u;
    edge[cnt_edge].to = v;
    edge[cnt_edge].next = head[u];
    edge[cnt_edge].flag = c;
    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].flag == 0) continue;
        if (edge[i].cut == 0)
        {
            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 = 2;
                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].flag == 2 && (edge[i].cut == 1 || edge[i].cut == 2))
            printf("%d %d %d
", edge[i].from, edge[i].to, edge[i].cut);
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d%d", &n, &m))
    {
        if (n == 0 && m == 0) break;
        int u, v, c;
        init();
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &u, &v, &c);
            add_edge(u, v, c);
            if (c == 1) c = 0;
            add_edge(v, u, c);
        }
        tarjan(1, -1);
        solve();
    }
    return 0;
}

  

 

原文地址:https://www.cnblogs.com/wenruo/p/5008011.html