poj3177 Redundant Paths
来源:互联网 发布:好八字 知乎 编辑:程序博客网 时间:2024/06/05 08:32
链接:http://poj.org/problem?id=3177
题意:大概就是,有一个公路网络,求至少加几条边使其成为一个(边)双连通图。
可以拿来用作模板。学了两种方法,
一种是常规的解题方法:求连通分量,删桥,缩点,加桥,求叶子节点数。
添加边数=(叶子节点数+1)/2
另一种是,原理我也有点说不清楚啊。就是最终操作对每条边edge(u,v)遍历,low[u]==low[v]表示u,v在同一个连通分量;
若不同,则degree[u]++、degree[v]++。然后求叶子节点数。。
poj3352和此题一模一样。
//常规解法//求连通分量,删桥,缩点,加桥,求叶子节点数。#include<cstdio>#include<cstring>#define MAXN 5005#define MAXE 10005using namespace std;struct Edge{ int to; int next;}edge[MAXE*2];struct Bridge{int u,v;}bridge[MAXE];int map[MAXN][MAXN],head[MAXN],far[MAXN],stack[MAXN];int dfn[MAXN],low[MAXN],vist[MAXN],degree[MAXN];int bcnt,ecnt,index,bres,top;int n,r;void add(int u,int v){ edge[ecnt].to=v; edge[ecnt].next=head[u]; head[u]=ecnt++; edge[ecnt].to=u; edge[ecnt].next=head[v]; head[v]=ecnt++; map[u][v]=map[v][u]=1;}void tarjan(int u,int fa){int i,v; dfn[u]=low[u]=++index;stack[top++]=u;for(i=head[u];i!=-1;i=edge[i].next){v=edge[i].to;if(v==fa)continue;if(!dfn[v]){ tarjan(v,u);if(low[u]>low[v])low[u]=low[v];if(dfn[u]<low[v]){bridge[bres].u=u; //把桥保存下来bridge[bres++].v=v;bcnt++;do{ far[stack[--top]]=bcnt; //弹出同一个连通分量的节点,并缩点}while(stack[top]!=v);}}else if(low[u]>dfn[v])low[u]=dfn[v];}}int solve(){memset(far,-1,sizeof(far));memset(degree,0,sizeof(degree));memset(dfn,0,sizeof(dfn));bcnt=bres=index=top=0; tarjan(1,-1); ++bcnt; //处理特殊的顶点for(int i=1;i<=n;i++)if(dfn[i]&&far[i]==-1)far[i]=bcnt;for(int i=0;i<bres;i++){ degree[far[bridge[i].u]]++; //对桥的两个端点求度 degree[far[bridge[i].v]]++;}int leaf=0;for(int i=1;i<=bcnt;i++)if(degree[i]==1) //度数为1的即为叶子节点leaf++; return (leaf+1)/2;}int main(){int i,a,b,res;while(scanf("%d%d",&n,&r)!=EOF){memset(head,-1,sizeof(head));ecnt=0;for(i=0;i<r;i++){ scanf("%d%d",&a,&b);if(map[a][b])continue;add(a,b);}res=solve();printf("%d\n",res);} return 0;}
//简便方法#include<cstdio>#include<cstring>#define MAXN 5005#define MAXE 10005using namespace std;struct Edge{ int to; int next;}edge[MAXE*2];int map[MAXN][MAXN],head[MAXN],dfn[MAXN],low[MAXN],vist[MAXN],degree[MAXN];int ecnt,index,n,r;void add(int u,int v){ edge[ecnt].to=v; edge[ecnt].next=head[u]; head[u]=ecnt++; edge[ecnt].to=u; edge[ecnt].next=head[v]; head[v]=ecnt++; map[u][v]=map[v][u]=1;}void dfs(int u,int fa){int i,v; vist[u]=1; low[u]=dfn[u]=++index; for(i=head[u];i!=-1;i=edge[i].next){ v=edge[i].to; if(v==fa)continue; if(!vist[v]) dfs(v,u); if(low[u]>low[v]) low[u]=low[v];}}int tarjan(){int i,j,v,ans=0; memset(dfn,0,sizeof(dfn)); memset(vist,0,sizeof(vist)); memset(degree,0,sizeof(degree)); index=0; dfs(1,1); for(i=1;i<=n;i++)for(j=head[i];j!=-1;j=edge[j].next){ v=edge[j].to; if(low[i]!=low[v]) degree[low[i]]++;}for(i=1;i<=n;i++)if(degree[i]==1)ans++;return (ans+1)/2;}int main(){int i,a,b,res;while(scanf("%d%d",&n,&r)!=EOF){memset(head,-1,sizeof(head));ecnt=0;for(i=0;i<r;i++){ scanf("%d%d",&a,&b);if(map[a][b])continue;add(a,b);}res=tarjan();printf("%d\n",res);} return 0;}
0 0
- poj3177 - Redundant Paths
- POJ3177--Redundant Paths
- 【POJ3177】Redundant Paths
- poj3177 Redundant Paths
- poj3177 Redundant Paths
- POJ3177 Redundant Paths
- POJ3177 Redundant Paths
- 【poj3177】 Redundant Paths
- poj3177 Redundant Paths
- Redundant Paths(POJ3177)
- 【poj3177】【双连通分量】Redundant Paths
- poj3177 Redundant Paths 边双连通分量
- POJ3177 Redundant Paths (双联通缩点)
- poj3177 Redundant Paths && poj3352 Road Construction
- poj3177 Redundant Paths 双连通分量
- poj3177 Redundant Paths 双连通分量
- poj3177 Redundant Paths(边双联通)
- [POJ3177]Redundant Paths(tarjan求边双)
- HUD1711
- JDK1.7.0_51使用javah工具生成.h声明文件的环境配置
- Python中dict的排序方法(按key或按value)
- 跑腿兔(TaskRabbit)是一个跑腿网站
- 使用 LINQ to XML
- poj3177 Redundant Paths
- 导致企业营销模式脱节的几宗问题
- 【Linux入门学习之】vi/vim编辑器必知必会
- PyQt4 精彩实例分析* 实例1 Hello Kitty!
- Smack PacketReader 启动过程分析
- 一个古怪而又有用的python语法
- hibernate映射(一) 之Hibernate单项多对一映射
- summernote网页编辑器嵌入视频
- Storyboard 简述 使用 创建 数据传送 跳转