Codeforces 118E Bertown roads【边双联通】

E. Bertown roads
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bertown has n junctions and m bidirectional roads. We know that one can get from any junction to any other one by the existing roads.

As there were more and more cars in the city, traffic jams started to pose real problems. To deal with them the government decided to make the traffic one-directional on all the roads, thus easing down the traffic. Your task is to determine whether there is a way to make the traffic one-directional so that there still is the possibility to get from any junction to any other one. If the answer is positive, you should also find one of the possible ways to orient the roads.

Input

The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, n - 1 ≤ m ≤ 3·105) which represent the number of junctions and the roads in the town correspondingly. Then follow m lines, each containing two numbers which describe the roads in the city. Each road is determined by two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of junctions it connects.

It is guaranteed that one can get from any junction to any other one along the existing bidirectional roads. Each road connects different junctions, there is no more than one road between each pair of junctions.

Output

If there's no solution, print the single number 0. Otherwise, print m lines each containing two integers pi and qi — each road's orientation. That is the traffic flow will move along a one-directional road from junction pi to junction qi. You can print the roads in any order. If there are several solutions to that problem, print any of them.

Examples
input
6 8
1 2
2 3
1 3
4 5
4 6
5 6
2 4
3 5
output
1 2
2 3
3 1
4 5
5 6
6 4
4 2
3 5
input
6 7
1 2
2 3
1 3
4 5
4 6
5 6
2 4
output
0

题目大意:


给出一个N个点,M条边的图,现在希望我们将这样的一个图中所有边设定成单向边,问是否能够构成一个图,使得从u能够到v,也能够从v到u。


思路:


如果在有向图中,从u能到v,也能从v到u,那么如果我们将这样一个图变成无向图的话,我们直接跑边双联通其肯定分量数量为1的。

所以我们首先假定原图为无向图,然后跑一遍边双联通即可,如果其只包含一个分量,那么就是存在解,那么对应我们将原图随便Dfs一遍输出过程即可。

过程用map<pair,int>去重。


Ac代码:


#include<stdio.h>
#include<vector>
#include<string.h>
#include<map>
using namespace std;
map<pair<int,int>,int>s;
vector<int>mp[105000];
int n,m,sig,cnt,tt;
int xx[400000];
int yy[400000];
int dfn[150050];
int low[150050];
int vis[150050];
int stack[150050];
int color[150050];
void Tarjan(int u,int from)
{
    vis[u]=1;
    dfn[u]=low[u]=cnt++;
    stack[++tt]=u;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        if(vis[v]==0)
        {
            Tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else low[u]=min(low[u],low[v]);
    }
    if(low[u]==dfn[u])
    {
        sig++;
        do
        {
            color[stack[tt]]=sig;
            vis[stack[tt]]=-1;
        }
        while(stack[tt--]!=u);
    }
}
void Dfs(int u,int from)
{
    vis[u]=1;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        else if(vis[v]==0)
        {
            vis[v]=1;
            s[make_pair(u,v)]=s[make_pair(v,u)]=1;
            printf("%d %d\n",u,v);
            Dfs(v,u);
        }
        else
        {
            if(s[make_pair(u,v)]==0)
            {
                s[make_pair(u,v)]=1;
                s[make_pair(v,u)]=1;
                printf("%d %d\n",u,v);
            }
        }
    }
}
void Slove()
{
    tt=-1;
    cnt=1;
    sig=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(stack,0,sizeof(stack));
    memset(color,0,sizeof(color));
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            Tarjan(i,-1);
        }
    }
    if(sig>1)printf("0\n");
    else
    {
        memset(vis,0,sizeof(vis));
        Dfs(1,1);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        s.clear();
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
            xx[i]=x;yy[i]=y;
        }
        Slove();
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值