poj 3177 Redundant Paths 边-双连通分量
来源:互联网 发布:手机三维绘图软件 编辑:程序博客网 时间:2024/06/06 15:46
题目链接
http://poj.org/problem?id=3177
题意
某人要修路,保证任意两个点之间可以有至少两条不同的路线到达,求至少需要修的路的数量(加的边数)。给的图是连通的
思路
其实就是边-双连通图的定义:这种图任意两个顶点之间至少存在两条无公共边的路径。
所以就是需要加最少的边,使图变成边双连通图。具体做法是先双连通分量缩点成树,找出叶子节点的个数leaf。答案为(leaf+1)/2.做法与pojpoj 3352 Road Construction 一样。唯一的区别是这题图是有重边的,只需在tarjan缩点时处理一下即可。
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<string>#include<queue>#include<stack>#include<set>#include<map>#define ll long longusing namespace std;const int INF = ( 2e9 ) + 2;const ll maxn = 5e3+10;const int maxm = 2*1e4+10;vector<int> g[maxn];int dfn[maxn],low[maxn],Belong[maxn],Stack[maxn],In[maxn];int index,bcc,top;void tarjan(int u,int fa){ dfn[u]=low[u]=++index; Stack[top++]=u; int f=1; for(int i=0,L=g[u].size();i<L;i++) { int v=g[u][i]; if(f&&v==fa) { f=0; continue; } if(!dfn[v]) { tarjan(v,u); low[u]=min(low[u],low[v]); } else low[u]=min(low[u],dfn[v]); } if(low[u]==dfn[u]) { int x; bcc++; do { x=Stack[--top]; Belong[x]=bcc; }while(x!=u); }}int solve(int n){ memset(dfn,0,sizeof(dfn)); memset(In,0,sizeof(In)); index=bcc=top=0; tarjan(1,-1); for(int u=1;u<=n;u++) { for(int j=0,L=g[u].size();j<L;j++) { int v=g[u][j]; if(Belong[u]!=Belong[v]) { In[Belong[v]]++; } } } int leaf=0; for(int i=1;i<=bcc;i++) if(In[i]==1)leaf++; return (leaf+1)/2;}int main(){ int n,m,u,v; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++)g[i].clear(); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } printf("%d\n",solve(n)); }}
阅读全文
0 0
- poj 3177 Redundant Paths(双连通分量)
- POJ 3177 Redundant Paths 边的双连通分量
- [边双连通分量] poj 3177 Redundant Paths
- POJ 3177 Redundant Paths(边双连通分量,3级)
- POJ 3177 Redundant Paths / 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- poj 3177 Redundant Paths 边双连通分量+缩点
- poj 3177 Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths 边双连通分量+缩点
- POJ 3177 Redundant Paths(边双连通分量)
- poj 3177 Redundant Paths 边双连通分量
- POJ-3177 Redundant Paths (边双连通分量[Tarjan])
- POJ 3177Redundant Paths 边双连通分量
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- POJ 3177 Redundant Paths <边双连通分量>
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- POJ 3177 Redundant Paths(边双连通分量+缩点)
- poj 3177 Redundant Paths(Tarjan,边双连通分量)
- 基于TensorFlow实现Skip-Gram模型
- [反演] 2017 计蒜之道 复赛 A. 阿里云秘钥池
- URAL 1009|URAL 1012|URAL 1013|K-based Numbers|高精度|动态规划
- 用construct 2来做一个射击小游戏吧ヾ(o´∀`o)ノ
- 51Nod 1085背包问题(dp)
- poj 3177 Redundant Paths 边-双连通分量
- pata1039(输出格式很重要,多一个空格就完蛋!)
- Bootstrap学习
- [最短路] 2017 计蒜之道 复赛 D. 百度地图导航
- 杂项 数据竞赛
- 10-1 DAIRY
- 对象、属性、方法、 封装与继承
- 20171003WindowsPrj08_01消息钩子
- JNI之C代码访问java中的成员和java调C