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 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
随机推荐
- quartz---(1)
Quartz Quartz是什么? 简单的一些例子 Quartz是什么 简单的认为Quartz是是一个定时器. 1.1. 下载 http://www.quartz-scheduler.org/down ...
- Excel中输入身份证后3位变成0,怎么办?
1.问题介绍: 1)问题: 在使用excel输入身份证号时,会发现直接输入数字后最后面的三位数字都变成0了,这可怎么办呢?
- [置顶]使用scrapy_redis,自动实时增量更新东方头条网全站新闻
存储使用mysql,增量更新东方头条全站新闻的标题 新闻简介 发布时间 新闻的每一页的内容 以及新闻内的所有图片.项目文件结构. 这是run.py的内容 1 #coding=utf-8 2 from ...
- CDMA学习
1.关于RC:http://www.mscbsc.com/askpro/question74915 2.CDMA知识要点:http://wenku.baidu.com/view/d4511442a89 ...
- Installing MIB in Ubuntu and Solving the Error “SNMP Cannot Find Module …”
Has noticed an error after executing the command snmpwalk with the indication of MIB instead of OID: ...
- python3.6 ubuntu
apt-get install python3.6-dev 安装 pip install mysqlclient 必备
- Swift中WebView的应用
WebView控件是做网络应用开发中使用最多的控件,直接在WebView内部指定一个网页地址就可以访问网页了,同时也可以实现UIWebViewDelegate协议实现相应的方法去控制内容的加载和处理. ...
- 新手小白Linux(Centos6.5)部署java web项目(mysql5.7安装及相关操作)
一.安装 参考:https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html # 检测系统中是否安装了mysql y ...
- HDU 4585
http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用 ...
- 一个两年Java的面试总结
前言 16年毕业到现在也近两年了,最近面试了阿里集团(菜鸟网络,蚂蚁金服),网易,滴滴,点我达,最终收到点我达,网易offer,蚂蚁金服二面挂掉,菜鸟网络一个月了还在流程中...最终有幸去了网易.但是 ...