Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)
题目链接: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、边双连通分量)的更多相关文章
- codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
题目链接:http://www.codeforces.com/problemset/problem/118/A题意:字符串转换……C++代码: #include <string> #inc ...
- 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 ...
- 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 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
随机推荐
- 配置IIS,Apache,PHP过程中遇到的一些问题
下载了eclipse的最新版本,并且添加了PHP插件.为了支持多语言,决定采用UTF-8编码.但是在开发的过程中,发现代码的自动提示帮助信息显示的是乱码,PHP源文件及注释,均正常.在网上查了很多资料 ...
- ffmpeg-20160806-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- monitor disk
#!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...
- manage account
#!/bin/bash # #Delete_user - Automates the steps to remove an account # ############################ ...
- 【QT】C++ GUI Qt4 学习笔记1
Find对话框实现 平台 Qt5.3.2 MinGW4.8.2 注意创建时用QDialog finddialog.h #ifndef FINDDIALOG_H #define FINDDIALOG_H ...
- 【linux】sudo su切换到root权限
在用户有sudo权限但不知道root密码时可用 sudo su切换到root用户
- 【XLL API 函数】xlCoerce
将 XLOPER/XLOPER12 转换为另一种类型,或是查询表格中的单元格值. 函数原型 Excel12(xlCoerce, LPXLOPER12 pxRes, 2, LPXLOPER12 pxSo ...
- Sql存储过程
下面的存储过程从四个表的联接中返回所有作者(提供了姓名).出版的书籍以及出版社.该存储过程不使用任何参数. USE pubs IF EXISTS (SELECT name FROM sysobject ...
- GoF23种设计模式
创建型模式 1.ABSTRACT FACTORY-追MM少不了请吃饭了,麦当劳的套餐和肯德基的套餐都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说"两个B ...
- 用 MyEclipse 开发 Spring 入门操作
何为Spring Spring框架是一个轻量级的控制反转(IOC)技术和面向切面编程(AOP)技术的容器框架,利用Spring框架可以实现对象的生命周期管理和分离应用系统中的业务逻辑组件和通用的技术服 ...