POJ3177(3352)(边双连通分量)
题目:
原本没有记录桥是谁,而是染色时即时判断的。后来发现不行,因为a去b可能满足low[b]>dfn[a],但b去a就不满足了。
这是因为low和dfn的关系是相对的,仅限于tarjan时的那棵dfs搜索树中。染色时没有按那棵树走,就不能用dfn和low了!
累加ans时可以把边+=2,很方便。
别忘了写v=edge[i].to。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,r,head[],xnt=,dfn[],low[],col[];
int x,y,tim,cnt,d[],ans;
bool b[][],brg[];
struct Edge{
int next,to,from;
Edge(int n=,int t=,int f=):next(n),to(t),from(f) {}
}edge[];
void dfs(int cur,int fa)
{
// printf("cur=%d\n",cur);
dfn[cur]=low[cur]=++tim;
for(int i=head[cur],v;i;i=edge[i].next)
{
if(dfn[v=edge[i].to]&&v!=fa)
low[cur]=min(low[cur],dfn[v]);
if(!dfn[v])
{
dfs(v,cur);
low[cur]=min(low[cur],low[v]);
if(low[v]>dfn[cur])brg[i]=,brg[i^]=;
}
}
// printf("cur=%d dfn=%d low=%d\n",cur,dfn[cur],low[cur]);
}
void cl(int cur,int fa)
{
col[cur]=cnt;
// printf("cur=%d col=%d\n",cur,cnt);
for(int i=head[cur],v;i;i=edge[i].next)
{
if(edge[i].to==fa||brg[i])continue;
// printf("v=%d lowv=%d dfncur=%d\n",edge[i].to,low[edge[i].to],dfn[cur]);
// if(low[v=edge[i].to]<=dfn[cur]&&!col[v])cl(v,cur);//避免环的死循环 //a去b是桥,b去a就不一定了
if(!col[v=edge[i].to])cl(v,cur);//
}
}
int main()
{
// freopen("POJ3177.in","r",stdin);
// freopen("poj3177.out","w",stdout);
// while(1)
// {
// xnt=1;cnt=0;ans=0;tim=0;
// memset(b,0,sizeof b);
// memset(col,0,sizeof col);
// memset(dfn,0,sizeof dfn);
// memset(low,0,sizeof low);
// memset(head,0,sizeof head);
// memset(d,0,sizeof d);
// memset(brg,0,sizeof brg);
scanf("%d%d",&n,&r);
for(int i=;i<=r;i++)
{
scanf("%d%d",&x,&y);
if(!b[x][y])
{
b[x][y]=;b[y][x]=;
edge[++xnt]=Edge(head[x],y,x);head[x]=xnt;
edge[++xnt]=Edge(head[y],x,y);head[y]=xnt;
}
}
dfs(,);
for(int i=;i<=n;i++)
if(!col[i])
{
cnt++;cl(i,);
}
for(int i=;i<=xnt;i+=)
if(col[edge[i].from]!=col[edge[i].to])
// if(brg[i])
{
d[col[edge[i].from]]++;d[col[edge[i].to]]++;
// brg[i]=0;
// brg[i^1]=0;
}
for(int i=;i<=cnt;i++)
if(d[i]==)ans++;
printf("%d\n",(ans+)/);
// }
return ;
}
POJ3177(3352)(边双连通分量)的更多相关文章
- POJ 3352 (边双连通分量)
题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...
- poj3177 && poj3352 边双连通分量缩点
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12676 Accepted: 5368 ...
- poj3177(边双连通分量+缩点)
传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...
- POJ3177 Redundant Paths 双连通分量
Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- POJ 3352 Road Construction(边—双连通分量)
http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...
- POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)
Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...
- Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)
http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...
- POJ3177 Redundant Paths(边双连通分量+缩点)
题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
随机推荐
- OAF 清空指定控件或区域的值
CO if (pageContext.getParameter("ClearBtn") != null) { clearRegion(pageContext, webBean, & ...
- @XmlAccessorType @XmlType 详解
例子: @XmlAccessorType(XmlAccessType.FIELD) String name=”CY”; } public static void main(String[] args) ...
- Activiti工作流笔记(4)
Activiti工作流启动流程 /** * 启动流程 * */ public class ActivitiTest2 { RepositoryService repositoryService; Ru ...
- Windows系统配置Python环境,python2和python3共存
Windows系统配置python2和python3共存 1.下载python: https://www.python.org/downloads/ 注:选择需要的版本(python2 or ...
- 跟我一起学习ASP.NET 4.5 MVC4.0(五)
前面几篇文章介绍了一下ASP.NET MVC中的一些基础,今天我们一起来学习一下在ASP.NET MVC中控件的封装.在页面中我们会经常使用到Html对象,来程序控件,当然这里的控件不是说ASP.NE ...
- python笔记01:基础知识
1.4 数字和表达式 # -*- coding:utf-8 -*- #1.4 #除法 print 1 / 2 print 1.0 / 2 print 10 / 3 print 10.0 / 3.0 p ...
- Bitdefender Internet Security 2013 – 免费3个月
Bitdefender Internet Security 2013 – 免费3个月大约1分钟参加调查,申请3个月免费,缺陷是…要经过人工审核活动地址: 点此进入申请方法,最好用谷歌翻译(俄文)
- localforage 对不同浏览器 使用不同的缓存策略 , 大大提高了性能 ,IndexedDB,WebSQL 和 localStorage 三种存储模式
支持回调的异步 API: 支持 IndexedDB,WebSQL 和 localStorage 三种存储模式(自动为你加载最佳的驱动程序): 支持 BLOB 和任意类型的数据,让您可以存储图片,文件等 ...
- NBUT 1217 Dinner 2010辽宁省赛
Time limit 1000 ms Memory limit 32768 kB Little A is one member of ACM team. He had just won the g ...
- 玩转TypeScript(1) --定义简单的类
相对于JavaScript来说,TypeScript增强了强制类型,同时添加了一系列的面向对象的特性,包含:静态类型(Static typing).类(Classes).接口(Interfaces). ...