poj 1515 Street Directions 【tarjan 求割边】【确定无向图边的方向 将其变成有向强连通图】

Street Directions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 753 Accepted: 408 Special Judge

Description

According to the Automobile Collision Monitor (ACM), most fatal traffic accidents occur on two-way streets. In order to reduce the number of fatalities caused by traffic accidents, the mayor wants to convert as many streets as possible into one-way streets. You have been hired to perform this conversion, so that from each intersection, it is possible for a motorist to drive to all the other intersections following some route. 

You will be given a list of streets (all two-way) of the city. Each street connects two intersections, and does not go through an intersection. At most four streets meet at each intersection, and there is at most one street connecting any pair of intersections. It is possible for an intersection to be the end point of only one street. You may assume that it is possible for a motorist to drive from each destination to any other destination when every street is a two-way street. 

Input

The input consists of a number of cases. The first line of each case contains two integers n and m. The number of intersections is n (2 <= n <= 1000), and the number of streets is m. The next m lines contain the intersections incident to each of the m streets. The intersections are numbered from 1 to n, and each street is listed once. If the pair i j is present, j i will not be present. End of input is indicated by n = m = 0. 

Output

For each case, print the case number (starting from 1) followed by a blank line. Next, print on separate lines each street as the pair i j to indicate that the street has been assigned the direction going from intersection i to intersection j. For a street that cannot be converted into a one-way street, print both i j and j i on two different lines. The list of streets can be printed in any order. Terminate each case with a line containing a single `#' character. 

Note: There may be many possible direction assignments satisfying the requirements. Any such assignment is acceptable. 

Sample Input

7 10
1 2
1 3
2 4
3 4
4 5
4 6
5 7
6 7
2 5
3 6
7 9
1 2
1 3
1 4
2 4
3 4
4 5
5 6
5 7
7 6
0 0

Sample Output

1

1 2
2 4
3 1
3 6
4 3
5 2
5 4
6 4
6 7
7 5
#
2

1 2
2 4
3 1
4 1
4 3
4 5
5 4
5 6
6 7
7 5

#

题意:给你一个N个点和M条边的无向图,让你确定边的方向将图变成强连通。若无法确定某条边的方向使图变成强连通可以正反向输出这条边 以代表两条 有向边。

思路:割边的方向是无法确定的,所以必定是双向的。

一:tarjan求出割边,标记它和它的反向边。

二:按DFS顺序确定边的方向 并标记符合该方向的边。

三:最后遍历所有边,输出标记的边即可。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 1000+10
#define MAXM 1000000+100
using namespace std;
struct Edge
{
    int from, to, mark, next;
    //起点 终点 标记该边是否输出 -1代表还未遍历 1输出 0不输出
};
Edge edge[MAXM];
int head[MAXN], edgenum;
int low[MAXN], dfn[MAXN];
int dfs_clock;
int N, M;//点数 和 边数
int k = 1;//测试组数
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int m)
{
    Edge E = {u, v, m, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
void getMap()
{
    int a, b;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d", &a, &b);
        addEdge(a, b, -1);//初始化-1表示还没有标记
        addEdge(b, a, -1);
    }
}
//DFS处理过程中,标记割边的反向边
void tarjan(int u, int fa)
{
    low[u] = dfn[u] = ++dfs_clock;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == fa) continue;//去反向边
        if(edge[i].mark != -1) continue;//已经标记过
        edge[i].mark = 1;//按DFS顺序选择方向
        edge[i^1].mark = 0;//反向边先不考虑
        if(!dfn[v])
        {
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
            if(low[v] > dfn[u])//割边
                edge[i^1].mark = 1;//标记反向边
        }
        else
            low[u] = min(low[u], dfn[v]);
    }
}
void find_cut(int l, int r)
{
    memset(low, 0, sizeof(low));
    memset(dfn, 0, sizeof(dfn));
    dfs_clock = 0;
    for(int i = l; i <= r; i++)
        if(!dfn[i]) tarjan(i, -1);
}
void solve()
{
    printf("%d\n\n", k++);
    for(int i = 0; i < edgenum; i++)
    {
        if(edge[i].mark)//输出标记的边
            printf("%d %d\n", edge[i].from, edge[i].to);
    }
    printf("#\n");
}
int main()
{
    while(scanf("%d%d", &N, &M), N||M)
    {
        init();
        getMap();
        find_cut(1, N);
        solve();
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值