【POJ】3177 Redundant Paths
【算法】边双连通分量
【题意&题解】http://blog.csdn.net/geniusluzh/article/details/6619575 (注意第一份代码是错误的)
一些细节:
1.判断桥只能在树边判断,不能在反向边判断,体现在程序中注释的wrong位置。
2.标记桥要双向标记。
3.第二次dfs的时候记得不走已染色的点,否则会陷入环中。
核心判断:
1.未访问过:取low
2.未访问过:判桥
3.访问过:取dfn
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=,maxm=;
struct edge{int u,v,from;}e[maxm*];
int first[maxn],dfn[maxn],dfsnum,low[maxn],iscut[maxn],cc[maxn],ccnum,n,m,tot,degree[maxn];
void insert(int u,int v)
{tot++;e[tot].u=u;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
int tarjan(int x,int fa)
{
dfn[x]=low[x]=++dfsnum;
for(int i=first[x];i;i=e[i].from)
if(e[i].v!=fa)
{
int y=e[i].v;
if(!dfn[y])
{
tarjan(y,x);
low[x]=min(low[x],low[y]);
if(low[y]>dfn[x])iscut[i]=,iscut[i%?i+:i-]=;
}
else low[x]=min(low[x],dfn[y]);
// if(low[y]<dfn[x])iscut[i]=1,iscut[i%2?i+1:i-1]=1; wrong
}
}
void dfs(int x,int fa)
{
cc[x]=ccnum;
for(int i=first[x];i;i=e[i].from)
if(e[i].v!=fa&&!iscut[i]&&!cc[e[i].v])dfs(e[i].v,x);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
insert(u,v);
insert(v,u);
}
for(int i=;i<=n;i++)if(!dfn[i])tarjan(i,-);
// for(int i=1;i<=tot;i++)printf("iscut[%d]%d u=%d v=%d\n",i,iscut[i],e[i].u,e[i].v);
for(int i=;i<=n;i++)
if(!cc[i])
{
ccnum++;
dfs(i,-);
}
int ans=;
for(int i=;i<=tot;i++)
if(cc[e[i].u]!=cc[e[i].v])
{
degree[cc[e[i].u]]++;
degree[cc[e[i].v]]++;
// printf("%d %d\n",cc[e[i].u],cc[e[i].v]);
}
// printf("ccnum=%d\n",ccnum);
for(int i=;i<=ccnum;i++)
if(degree[i]==)ans++;
printf("%d",(ans+)/);
return ;
}
【POJ】3177 Redundant Paths的更多相关文章
- 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】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- POJ 3177——Redundant Paths——————【加边形成边双连通图】
Redundant Paths Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- 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(边双连通分量)
[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
随机推荐
- CentOS7安装Consul集群
1.准备4台服务器 linux1 192.168.56.101 linux2 192.168.56.102 linux3 192.168.56.103 linux4 192.168.56.104 2. ...
- [Prism框架实用分享]如何在Prism应用程序中使用日志
前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...
- hdu-题目:1058 Humble Numbes
http://acm.hdu.edu.cn/showproblem.php?pid=1058 Problem Description A number whose only prime factors ...
- Deepin系统又损坏了!
1.首先,去Deepin官方下载镜像(记得MD5检验一下).2.提取ISO里的安装程序到桌面,执行之&写入.(提醒:勾选下面的支持BIOS启动的选项,自测深度的UEFI很不稳定,建议不使用UE ...
- 【linux】linux的数据流重定向
首先说一下什么是数据流重定向,所谓数据流重定向简单来说就是一个过程,这个过程捕捉一个文件,或者命令,程序,脚本,甚至脚本中的代码块(code block)的输出,然后把捕捉到的输出,作为输入发送给另外 ...
- 【C++】new和delete表达式与内存管理
new和delete表达式可以用来动态创建和释放单个对象,也可以用来动态创建和释放动态数组. 定义变量时,必须指定其数据类型和名字.而动态创建对象时,只需指定其数据类型,而不必为该对象命名.new表达 ...
- python while 学习
while True: reply = input('please input:') if reply == 'stop': break else: print (reply.upper())
- ios 中不new Date 的格式 不支持年月日 以‘-’ 分割的格式
new Date("2018-1-5") 在 ios 中显示 invalid date - 换做 / 则可以顺利显示 new Date("2018/1/5")
- PL/SQL中复制粘贴表结构信息
1.打开下图中的Tables文件夹 2.查找要找的表 3.右键单击找到的表—>Describe 4.复制所需的数据到EXCEL表中
- C++解析(15):二阶构造模式
0.目录 1.构造函数与半成品对象 2.二阶构造 3.小结 1.构造函数与半成品对象 关于构造函数: 类的构造函数用于对象的初始化 构造函数与类同名并且没有返回值 构造函数在对象定义时自动被调用 问题 ...