题目链接: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. C++函数传递指针面试题

    [本文链接] http://www.cnblogs.com/hellogiser/p/function-passing-pointer-interview-questions.html [代码1]   ...

  2. 使用logrotate管理nginx日志文件

    本文转载自:http://linux008.blog.51cto.com/2837805/555829 描述:linux日志文件如果不定期清理,会填满整个磁盘.这样会很危险,因此日志管理是系统管理员日 ...

  3. 在VS中MFC、ATL与WIN32有什么联系或区别?

    有时候遇到一些初学者问我这个问题:在VS中使用MFC和ATL与使用WIN32有什么联系或区别?通俗来说,win32是通过调用windows api去实现需要的功能.而MFC和ATL是封装好的类库,包含 ...

  4. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  5. centos6.5Xen4.2安装

    官方安装文档:http://xen.crc.id.au/support/guides/install/ 一.环境说明 1. 本文采用CentOS6.5 x64,安装开发包及开发工具. 2. 关闭sel ...

  6. css实现图片闪光效果

    1.这个效果是看到京东商城上的一个下效果,原本的思路是 用js控制一个图片在某张需要闪光的图片上重复出现,但是 网上找了一些资料,竟然是用css写的,真是太帅了!!! 2.原理:在需要闪光的图片前添加 ...

  7. 获取指定文件下的所有file文件

    /** * 描述:获取所有的文件列表 * @param file * @param list * @return */ private List<File> getAllFiles(Fil ...

  8. 为Kindeditor控件添加图片自动上传功能

    Kindeditor是一款功能强大的开源在线HTML编辑器,支持所见即所得的编辑效果.它使用JavaScript编写,可以无缝地与多个不同的语言环境进行集成,如.NET.PHP.ASP.Java等.官 ...

  9. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  10. 【vs2010调试】当前不会命中断点 源代码与原始版本不同

    解决方案:全选CPP文件内容,选择 “编辑”-“高级”-“设置选定内容的格式”,保存,重新编译.