题目链接:http://codeforces.com/problemset/problem/118/E

思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn[u],则说明边(u,v)是桥,从而这个图不是边双连通,然后发现在判断的时候已经访问了所有的顶点,顺便加入就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (300000 + 100);
int N, M, cnt, bcc_count;
int low[MAX_N], dfn[MAX_N], vis[MAX_N], mark[MAX_N];
stack<int > S;
vector<int > g[MAX_N];
map<pair<int, int>, int> mp;
vector<pair<int, int > > edge; bool Tarjan(int u, int fa)
{
int tag = 0;
low[u] = dfn[u] = ++cnt;
vis[u] = 1;
S.push(u);
REP(i, 0, (int)g[u].size()) {
int v = g[u][i];
if (v == fa && !tag) { tag = 1; continue; }
if (!dfn[v]) {
if (!Tarjan(v, u)) return false;
low[u] = min(low[u], low[v]);
if (low[v] > dfn[u]) return false;
else {
pair<int, int >PPI = make_pair(u, v);
edge.push_back(PPI);
mark[mp[PPI]] = 1;
}
}
else if (vis[v]) {
low[u] = min(low[u], dfn[v]);
pair<int, int> PPI = make_pair(u, v);
if (!mark[mp[PPI]]) {
mark[mp[PPI]] = 1;
edge.push_back(PPI);
}
}
}
return true;
} int main()
{
cin >> N >> M;
FOR(i, 1, M) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
mp[make_pair(u, v)] = i;
mp[make_pair(v, u)] = i;
}
cnt = bcc_count = 0;
memset(vis, 0, sizeof(vis));
memset(mark, 0, sizeof(mark));
memset(dfn, 0, sizeof(dfn));
if (!Tarjan(1, -1)) { puts("0"); return 0; }
REP(i, 0, (int)edge.size()) {
printf("%d %d\n", edge[i].first, edge[i].second);
}
return 0;
}

Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)的更多相关文章

  1. codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)

    题目链接:http://www.codeforces.com/problemset/problem/118/A题意:字符串转换……C++代码: #include <string> #inc ...

  2. Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland

    D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

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

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. 从其它系统登录到SharePoint 2010系统的单点登录

    以前做的只是使用SharePoint的单一登录,用SharePoint去登录其他的系统,现在要反过来,用Form认证的系统来登录SharePoint. 我们都知道,SharePoint使用的是域认证系 ...

  2. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  3. cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused

    cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused 环境: ...

  4. PHP生命周期

    2015-08-19 15:05:30 周三 一篇很好的文章 PHP内核探索 总结一下 1. 模块初始化 MINIT 各个PHP模块/扩展初始化内部变量, 告诉PHP调用自己的函数时, 函数体在哪里( ...

  5. 【动态规划】简单背包问题II

    问题 B: [动态规划]简单背包问题II 时间限制: 1 Sec  内存限制: 64 MB提交: 21  解决: 14[提交][状态][讨论版] 题目描述 张琪曼:“为什么背包一定要完全装满呢?尽可能 ...

  6. Divide and conquer:Median(POJ 3579)

        快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...

  7. Spring+SpringMVC+Mybatis 多数据源整合(转)

    转载自:http://blog.csdn.net/q908555281/article/details/50316137 目录(?)[-]拷贝所需jar拷贝jar文件需要的jar文件入下图所示因为我的 ...

  8. Android状态栏微技巧,带你真正意义上的沉浸式

    记得之前有朋友在留言里让我写一篇关于沉浸式状态栏的文章,正巧我确实有这个打算,那么本篇就给大家带来一次沉浸式状态栏的微技巧讲解. 其实说到沉浸式状态栏这个名字我也是感到很无奈,真不知道这种叫法是谁先发 ...

  9. Please see the 'svn upgrade' command

    svn: E155036: Please see the 'svn upgrade' command svn: E155036: Working copy '/home/easwy/dev' is t ...

  10. rsync使用

    1)拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式.     如:rsync -a  ./test.c  /backup 2)使用一个远程 ...