题目链接: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. 抓取网页内容生成kindle电子书

    参考: http://calibre-ebook.com/download_linux http://blog.codinglabs.org/articles/convert-html-to-kind ...

  2. UIScrollView 的 delaysContentTouches

    UIScrollView 的一段说明: Because a scroll view has no scroll bars, it must know whether a touch signals a ...

  3. Maven 3.3.3 Win10环境下的使用实例(中)

    继上一篇文章介绍了Maven在Windows中的安装,本文将介绍 Maven 的核心概念. POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依 ...

  4. HDU 4942 Game on S♂play(线段树、模拟、扩栈)

    比赛的时候想到这题的大概做法,但由于卡别的水题...就赛后做了... 题意:给一个二叉树,每个结点有一个w[i],有3种操作,0 x表示左旋x,1 x表示右旋x,3 x表示询问x结点的价值,其中,价值 ...

  5. LayoutInflater的inflate函数用法详解

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...

  6. (2016弱校联盟十一专场10.2) A.Nearest Neighbor Search

    题目链接 水题,算一下就行. #include <bits/stdc++.h> using namespace std; typedef long long ll; ll x[],y[], ...

  7. JS图表插件(柱形图、饼状图、折线图)

    http://www.open-open.com/lib/view/open1406378625726.html

  8. Hibernate双向一对多对象关系模型映射

    双向one-to-many 描述部门和岗位:一个部门有多个岗位 将单向的one-to-many 和many-to-one合并. 4.1双向的one-to-many数据库模型 create table ...

  9. osg四元数设置roll pitch heading角度

    roll绕Y轴旋转 pitch绕X轴旋转 heading绕Z轴旋转 单位是弧度,可以使用osg::inDegrees(45)将45角度转换为弧度 定义一个四元数 osg::Quat q( roll,o ...

  10. MFC 使用MFC EditBrowse Control控件选择文件或者文件夹

    从工具箱中拖拽一个MFC EditBrowse Control到窗体中, 通过设置“Browse Mode”属性指定“文件浏览”还是“文件夹浏览” 可以通过添加对象的方式将其与一个CString se ...