概述
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
Copy
6 8 1 2 2 3 1 3 4 5 4 6 5 6 2 4 3 5
output
Copy
1 2 2 3 3 1 4 5 5 6 6 4 4 2 3 5
input
Copy
6 7 1 2 2 3 1 3 4 5 4 6 5 6 2 4
output
Copy
0
给出一个无向图,问确定每一条边的方向之后,能否从任意一点到达其他所有的点。能则输出边。
当这个无向图的边双联通分量为1时,可以。然后dfs一遍输出边的方向。
参考:https://blog.csdn.net/mengxiang000000/article/details/78022317
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include<iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 150050+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
map<pair<int,int>,int>s;
vector<int>mp[maxn];
int n,m,sig,cnt,tt;
int dfn[maxn];
int low[maxn];
int vis[maxn];
int stacks[maxn];
int color[maxn];
void Tarjan(int u,int from)
{
vis[u]=1;
dfn[u]=low[u]=cnt++;
stacks[++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[stacks[tt]]=sig;
vis[stacks[tt]]=-1;
}
while(stacks[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 %dn",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 %dn",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(stacks,0,sizeof(stacks));
memset(color,0,sizeof(color));
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
Tarjan(i,-1);
}
}
if(sig>1)printf("0n");
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);
}
Slove();
}
}
最后
以上就是贤惠乌冬面为你收集整理的Codeforces Beta Round #89 (Div. 2)--E. Bertown roads--边双联通分量的全部内容,希望文章能够帮你解决Codeforces Beta Round #89 (Div. 2)--E. Bertown roads--边双联通分量所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复