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. 关于document.referrer的使用需要注意

    项目使用到一个场景,ajax请求返回无权限,跳回登录页面,登录后自动返回之前的浏览页,跳转由前端处理,于是想到document.referrer,但是对可靠性不确定,特意搜索了一下相关资料,大致整理如 ...

  2. MFC中关于子对话框中编辑框不能编辑的问题

    最近在用MFC写程序.发现子对话框中的编辑框不能编辑.具体问题是这样的: 我有一个对话框YhglDlg,创建了这个对话框的子对话框ZjyhxxDlg,子对话框的Style属性为Child,Border ...

  3. USB Device Finder

    http://www.velleman.eu/images/tmp/usbfind.c #ifdef __cplusplus extern "C" { #endif #includ ...

  4. UIImage imageNamed 与 imageWithContentsOfFile的差别

    [UIImage imageNamed:]仅仅适合与UI界面中的贴图的读取,较大的资源文件应该尽量避免使用 用UIImage载入本地图像最经常使用的是以下三种: 1.用imageNamed方法 [UI ...

  5. ajax跳转页面问题

    $.ajax({ type:"POST", url: //你的请求程序页面随便啦 async:false,//同步:意思是当有返回值以后才会进行后面的js程序. data://请求 ...

  6. PS-添加前景色

    alt+Delete是填充前景色,即ps左边两个颜色块,前面的那个 ctrl+delete填充背景色

  7. hdu 4740 The Donkey of Gui Zhou bfs

    The Donkey of Gui Zhou Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproble ...

  8. hdu4336 Card Collector 概率dp(或容斥原理?)

    题意: 买东西集齐全套卡片赢大奖.每个包装袋里面有一张卡片或者没有. 已知每种卡片出现的概率 p[i],以及所有的卡片种类的数量 n(1<=n<=20). 问集齐卡片需要买东西的数量的期望 ...

  9. Android集成支付宝接口 实现在线支付

    手机的在线支付,被认为是2012年最看好的功能,我个人认为这也是移动互联网较传统互联网将会大放光彩的一个功能. 人人有手机,人人携带手机,花钱买东西,不再需要取钱付现,不再需要回家上网银,想买什么,扫 ...

  10. 0c-39-ARC下单对象内存管理

    1.ARC工作原理详述 ARC是Objective-C编译器的特性,而不是运行时特性或者垃圾回收机制,ARC所做的只不过是在代码编译时为你自动在合适的位置插入release或autorelease A ...