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. JAVA 开发实例 一 移动的小球

    package com.java.move; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; i ...

  2. DbHelperSQL和Dapper数据访问的性能对比

    http://www.cnblogs.com/finesite/archive/2012/08/23/2652491.html

  3. erlang pool模块。

    出自: http://blog.sina.com.cn/s/blog_96b8a154010168ti.html

  4. 数据库 SQL :有关 NULL 值引发 TRUE、FALSE、UNKNOW 三值逻辑

    在 Java.C# 中,相信如果是 boolean 类型值,只有两种选择 true.false.然而,在 SQL 查询中,NULL 值的引入,使得新增了 UNKNOW ,因此,就产生了 TRUE.FA ...

  5. Hard problem

    1022: Hard problem Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 43   Solved: 12 Description The ...

  6. github 多帐户使用

    同一台电脑有2个github账号?咋办 比如一个公司账号一个个人账号. 私人账号如下: 邮箱example@126.com 账号:example 公司工作账号如下: work@xx.com 账号:my ...

  7. 内网能PING通TELNET通不能访问解决

    遇到一个离奇故障,内网,两个主机在同一IP段内,能互相PING通,TELNET对方的WEB服务器端口,通. 但用IE访问时不能,显示HTTP400.这明显是客户端系统的问题啊!但如何解决呢?我强烈怀疑 ...

  8. URL是什么?

    URL是统一资源定位符,有时也被俗称为网页地址. 如同在网络上的门牌,是因特网上标准的资源的地址(Address). 在因特网的历史上,统一资源定位符的发明是一个非常基础的步骤.统一资源定位符的语法是 ...

  9. C++ 内存泄露处理方法 (转)

    1.可以显示内存泄露情况,但无法找到具体的文件位置写法 包含头文件#include <crtdbg.h> 然后在需要检测内存泄露的位置加上语句 _CrtDumpMemoryLeaks(); ...

  10. android设置动态壁纸 (Wallpaper) 介绍

    当进入改壁纸的设置页面 但是还没有设置时 09-21 07:55:05.575: INFO/System.out(1337): service onCreate09-21 07:55:05.614: ...