poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)
/**
problem: http://poj.org/problem?id=3177
tarjan blog: https://blog.csdn.net/reverie_mjp/article/details/51704523 v 为下一结点, u为当前结点
如果low[v] > dfn[u] 则 边(u,v)为桥
缩点后剩下的所有边都为桥(缩点后即为树结构)
将叶子结点相连使其成为双联通分量为最优解
所以:
添加(leaf + 1) / 2 条边即可使图成为双联通图
**/
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std; class Graphics{
const static int MAXN = ;
const static int MAXM = * ;
private:
struct Edge{
int to, next;
bool bridge;
}edge[MAXM];
struct Point{
int dfn, low, color;
}point[MAXN];
int first[MAXN], sign, sumOfPoint, dfnNum, colorNum;
bool vis[MAXN];
stack<int> stk;
void tarjan(int u, int preEdge = -){
point[u].low = dfnNum;
point[u].dfn = dfnNum ++;
vis[u] = true;
stk.push(u);
for(int i = first[u]; i != -; i = edge[i].next){
int to = edge[i].to;
if((i^) == preEdge) continue; /// 由于是双向边,防止由该边跑回原来的点
if(!point[to].dfn){
tarjan(to, i);
point[u].low = min(point[u].low, point[to].low);
if(point[to].low > point[u].dfn){
edge[i].bridge = true;
edge[i^].bridge = true;
}
}else if(vis[to]){
point[u].low = min(point[to].dfn, point[u].low);
}
}
if(point[u].dfn == point[u].low){
vis[u] = false;
point[u].color = ++ colorNum;
while(stk.top() != u){
point[stk.top()].color = colorNum;
vis[stk.top()] = false;
stk.pop();
}
stk.pop();
}
}
public:
void init(int n){
sumOfPoint = n;
for(int i = ; i <= n; i ++){
first[i] = -;
vis[i] = ;
}
sign = colorNum = ;
dfnNum = ;
}
void addEdgeOneWay(int u, int v){
edge[sign].to = v;
edge[sign].next = first[u];
edge[sign].bridge = false;
first[u] = sign ++;
}
void addEdgeTwoWay(int u, int v){
addEdgeOneWay(u, v);
addEdgeOneWay(v, u);
}
void tarjanAllPoint(){
for(int i = ; i <= sumOfPoint; i ++){
if(!point[i].dfn)
tarjan(i);
}
}
int getAns(){
int *degree = new int[sumOfPoint+];
int ans = ;
for(int i = ; i <= sumOfPoint; i ++){
degree[i] = ;
}
tarjanAllPoint();
for(int i = ; i <= sumOfPoint; i ++){
for(int j = first[i]; j != -; j = edge[j].next){
int to = edge[j].to;
if(edge[j].bridge){
degree[point[to].color] ++;
}
}
}
for(int i = ; i <= sumOfPoint; i ++){
if(degree[i] == ){
ans ++;
}
}
delete []degree; return (ans + ) / ;
}
}graph; int main(){
int f, r;
scanf("%d%d", &f, &r);
graph.init(f);
while(r --){
int a, b;
scanf("%d%d", &a, &b);
graph.addEdgeTwoWay(a, b);
}
printf("%d\n", graph.getAns());
return ;
}
ps:
防止由该边跑回原来的点不能判断(点)而要判断(边)即

这么写是有bug的
例如:重边

poj 3177 Redundant Paths 求最少添加几条边成为双联通图: tarjan O(E)的更多相关文章
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
- poj 3177 Redundant Paths(tarjan边双连通)
题目链接:http://poj.org/problem?id=3177 题意:求最少加几条边使得没对点都有至少两条路互通. 题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3177 Redundant Paths (tarjan边双连通分量)
题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...
- POJ 3177 Redundant Paths(Tarjan)
题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf ...
- POJ - 3177 Redundant Paths (边双连通缩点)
题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...
随机推荐
- ubuntu 14.04 64bit 安装 oracle 11g r2
参考文章:http://tutorialforlinux.com/2016/03/09/how-to-install-oracle-11g-r2-database-on-ubuntu-14-04-tr ...
- SetupFactory7使用经验
1. exe默认产生快捷方式,可以点击去掉. 2. 默认英语,可与选汉语 3. 编码逻辑 安装程序复制完程序文件后,从编辑框中得到数据,并写ini文件 屏幕- ...
- 【网络编程】Socket套接字网络编程模型
一.Linux网络模型 -- Socket套接字编程 图片:Socket 抽象层 Socket编程--不同协议,统一接口 Socket的实质就是一个接口, 利用该接口,用户在使用不同的网络协议时,操作 ...
- jquery简介(一)
摘要:简要介绍jquery的起源,以及为什么需要使用jquery. jquery的优点 jquery体量小,加载速度快,其本身具有的功能使JavaScript应用程序开发人员的工作变得分外轻松.其中最 ...
- keras 自定义 custom 函数
转自: https://kexue.fm/archives/4493/,感谢分享! Keras是一个搭积木式的深度学习框架,用它可以很方便且直观地搭建一些常见的深度学习模型.在tensorflow出来 ...
- 修改mysql编码方式
第一种: 通过mysql命令行修改: 1)首先查看数据库字符编码,命令为: show variables like’collation_%’; show variables like’char ...
- python加解密
from Crypto.Cipher import AES import base64 secret_key = '1234567890123456' # 密匙 msg_text = 'test so ...
- C# 中关于radiobutton控件的使用
在一个Form窗口中定义了3个radiobutton,radioButton1.radioButton2和radioButton3,以及button1和button2(这里可以是其他控件) 为了实现单 ...
- QQ空间那年今日 & 人人过往的今天
都说天下文章一大抄!就看你会抄不会抄! 过往的今天这个功能很新颖,不过最后还是被企鹅抄走了~该出手时就出手! 自从过往的今天功能低调上线后,断断续续总是有人提到这个功能,有褒有贬: 顶的认为人人让自己 ...
- python入门11 元组tuple
tuple元组是一种不可变数据类型,也是一种序列,因此可用序列的各类方法,比如切片和索引 #coding:utf-8 #/usr/bin/python """ 2018- ...