118E Bertown roads

题目:把无向图指定边的方向,使得原图变成有向图,问能否任意两点之间互达

分析:显然如果没有桥的话,存在满足题意的方案。输出答案时任意从一个点出发遍历一遍即可。

求桥的话,利用tarjan算法的low和dfn值判断一下即可。

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 1e6+5; int dfn[MAXN],low[MAXN],dep;
int po[MAXN],tol;
int n,m; struct node{
int x,y,id,next;
}edge[MAXN*2]; bool dfs(int x,int fa){
low[x] = dfn[x] = ++ dep;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==fa)continue;
if(!dfn[y]){
if(!dfs(y,x))
return false;
cmin( low[x],low[y] ); if(low[y]>dfn[x])
return false;
}else
cmin( low[x],dfn[y] );
}
return true;
} void out(int x,int fa){
low[x] = 1;
for(int i=po[x];i;i=edge[i].next){
int y = edge[i].y;
if(y==fa)continue;
//cout<<"dsa "<<x<<" "<<y<<endl;
if( abs(edge[i].id)==1){
edge[i].id = 2;
edge[i^1].id = -2;
}
if(!low[y])
out(y,x);
}
} void add(int x,int y,int id){
edge[++tol].y = y;
edge[tol].x = x;
edge[tol].id = id;
edge[tol].next = po[x];
po[x] = tol;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif while(cin>>n>>m){
Clear(po);
tol = 1; int x,y;
rep1(i,m){
RD2(x,y);
add(x,y,1);
add(y,x,-1);
} Clear(dfn);
dep = 0;
bool ok = true;
rep1(x,n)
if(!dfn[x]){
if(!dfs(x,0)){
ok = false;
break;
}
} if(ok){
Clear(low);
out(1,0);
for(int i=2;i<=tol;i++)
if(edge[i].id>0)
printf("%d %d\n",edge[i].x,edge[i].y);
}
else
puts("0");
} return 0;
}

  

CF 118E Bertown roads 桥的更多相关文章

  1. Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)

    题目链接:http://codeforces.com/problemset/problem/118/E 思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn ...

  2. CF266D. BerDonalds [图的绝对中心]

    D. BerDonalds time limit per test 5 seconds memory limit per test 256 megabytes input standard input ...

  3. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

  4. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. CF 160D Edges in MST 最小生成树的性质,寻桥,缩点,批量处理 难度:3

    http://codeforces.com/problemset/problem/160/D 这道题要求哪条边存在于某个最小生成树中,哪条边不存在于最小生成树中,哪条边绝对存在于最小生成树中 明显桥边 ...

  6. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  7. codeforces 567 E. President and Roads 【 最短路 桥 】

    给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) 正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最 ...

  8. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥

    题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...

  9. [CF 191C]Fools and Roads[LCA Tarjan算法][LCA 与 RMQ问题的转化][LCA ST算法]

    参考: 1. 郭华阳 - 算法合集之<RMQ与LCA问题>. 讲得很清楚! 2. http://www.cnblogs.com/lazycal/archive/2012/08/11/263 ...

随机推荐

  1. Flex应用程序如何启动

    Flex应用程序启动 编写一个简单的Flex应用程序并不复杂,就算你从来没接触过Flex程序设计,照着帮助的实例步骤,不需花多长时间也能做出一个漂亮简捷的小程序出来.不过,随着对Flex程序编写的深入 ...

  2. CXF 与Spring整合配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...

  3. Ehcache(07)——Ehcache对并发的支持

    http://haohaoxuexi.iteye.com/blog/2119733 Ehcache对并发的支持 在高并发的情况下,使用Ehcache缓存时,由于并发的读与写,我们读的数据有可能是错误的 ...

  4. eclipse提示servlet不存在 的解决办法

    在以前的版本中,Tomcat的common/lib目录下有一个名为servlet-api.jar的包,把它拷贝至你的java安装目录下jre/lib/ext下就可以了. 如果是:tomcat6就在To ...

  5. Flex Builder 开发语言切换问题

    1.Flex Builder 4.6切换语言 打开\Adobe\Adobe Flash Builder 4.6\FlashBuilder.ini -nlzh_CN-startupeclipse/plu ...

  6. SoapUI命令行方式运行

    http://stackoverflow.com/questions/9220132/soapui-groovy-script-calls-to-command-line SoapUI支持用命令行方式 ...

  7. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  8. nslookup 查询IPv6

    > nslookup>  set type=AAAA > ipv6 domain name  (ipv6.google.com, time.buptnet.edu.cn)

  9. 一个获取文件绝对路径的sh

    脚本里有个获取文件绝对路径的需求,linux里有个很方便的realpath命令,但是mac下没有,甚至readlink -f也跟linux下的表现不同,所以……直接用pwd算了 #!/bin/bash ...

  10. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...