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. JavaScript 跨域:谈谈跨域之 JSONP

    在 Web 开发中,后台开发人员应该会通常遇到这个问题:跨域,而使用 JSONP 就是其中解决办法之一,当然,还有其它解决方法,比如:window.name.window.postMessage.CO ...

  2. jquery checkbox选中状态

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. CMSIS Example - Signal and Yield

    /*---------------------------------------------------------------------------- * RL-ARM - RTX *----- ...

  4. Android Studio初步使用教程

    今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...

  5. C#封装、多态、抽象、接口、匿名方法等学习

    1:封装 将对象进行封装,并不等于将整个对象完全包裹起来,而是根据实际需要,设置一定的访问权限,用户根据不同的权限调用对象提供的功能,在C#语言中,可以使用修饰符public.internal.pro ...

  6. 提供一个免费的CSDN下载账号

    账号:windforce05password:w12345678请下载了资源后评价一下资源,以便赚回分数.

  7. Android应用增量更新

    Original:https://github.com/cundong/SmartAppUpdates Backup:https://github.com/eltld/SmartAppUpdates

  8. MySql5.5忘记root密码的解决方法

    试了很多方法,下面这种方法是确保可以成功的,呵呵.转载自:http://hi.baidu.com/bjben/item/722bb50b27baf1dcdde5b097. 申明:本文章应该属于转载,但 ...

  9. 【安卓特效】怎样给ImageView加上遮罩,点击时泛黑、或泛白、?

    基本思路: 方法1.遮罩可直接叠加一层带alpha的纯白.或纯黑View,可直接在ImageView外套一层FrameLayout,其foreground(一般同学可能仅仅知道background,事 ...

  10. Foundation框架之NSString及其Mutable类型

    Foundation框架之NSString及其Mutable类型 目录 概述 对字符串的实用操作 拼接 拆分 字符串比较 是否包含某字符串 字数统计 大小写转换 具体的方法参见API 待研究 概述 对 ...