题目:

原本没有记录桥是谁,而是染色时即时判断的。后来发现不行,因为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)(边双连通分量)的更多相关文章

  1. POJ 3352 (边双连通分量)

    题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...

  2. poj3177 && poj3352 边双连通分量缩点

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12676   Accepted: 5368 ...

  3. poj3177(边双连通分量+缩点)

    传送门:Redundant Paths 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任何两个牧场之间至少有两条独立 ...

  4. POJ3177 Redundant Paths 双连通分量

    Redundant Paths Description In order to get from one of the F (1 <= F <= 5,000) grazing fields ...

  5. POJ 3352 Road Construction(边—双连通分量)

    http://poj.org/problem?id=3352 题意: 给出一个图,求最少要加多少条边,能把该图变成边—双连通. 思路:双连通分量是没有桥的,dfs一遍,计算出每个结点的low值,如果相 ...

  6. 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 ...

  7. Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

     http://blog.csdn.net/geniusluzh/article/details/6619575 在说Tarjan算法解决桥和边双连通分量问题之前我们先来回顾一下Tarjan算法是如何 ...

  8. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  9. POJ 3352 Road Construction(边双连通分量,桥,tarjan)

    题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...

随机推荐

  1. 最应该注意的Oracle版本之一

    最近以来,两个用户的库接连出现问题,经过查阅资料和分析,确定为数据库bug所致,其实,早在很久前,也遭遇过这个版本的类似bug,当时似乎还惊动了原厂的技术人员,后来确定为这个版本的bug,这个版本就是 ...

  2. zabbix LLD 自定义脚本

    一 前言 二 懒人必备zabbix监控之 LLD (low level discovery) 本次的教程是我想监控kafka的消费情况,举个栗子 [root@VM_0_98_centos bin]# ...

  3. 管道的创建与读写pipe

    1.管道的创建 #include <unistd.h> int pipe(int pipefd[2]); linux下创建管道可以通过函数pipe来完成.该函数如果调用成功,数组中将包含两 ...

  4. .net下4款不错的微信SDK

    .net下4款不错的微信SDK: 一款值得推荐的.Net微信开发SDK http://www.17ky.net/soft/474.html.Net微信公开帐号接口 WeiXinSDK http://w ...

  5. Jboss7 部署EJB3 简明教程

    什么是EJB? EJB 是 Java 企业Bean, 是JavaEE服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序.话不多说,直接看如何在本机部署EJB3.   部署环境: 操作系统 ...

  6. SSRS配置

    1. Reporting Services Configuration Manager-->Execution Account. 2. C:\Program Files\Microsoft SQ ...

  7. [翻译]HTTP: Let’s GET It On!

    原文地址:HTTP: Let’s GET It On! 简介 几周之前,我们了解到HTTP是一个应用级的协议.现在是时候探索我们应该如何在客户端和服务端的通信之间使用这个协议了. 从网络上获取资源 记 ...

  8. anu - reactShime

    import { Component } from "./Component"; import { options } from "./util"; impor ...

  9. 动态更新 HTML 内容 —— AJAX

    通过 JavaScript 加载数据,在不刷新网页的情况下,更新网页内容的技术,称为 AJAX(Asynchronous JavaScript and XML,异步 JavaScript 和 XML) ...

  10. 《DSP using MATLAB》Problem 3.4

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...