poj3177&&poj3352 无向图的双连通问题(至少加几条边原图变为双连通图)
来源:互联网 发布:淘宝会员账号 编辑:程序博客网 时间:2024/05/19 10:35
http://poj.org/problem?id=3177
Description
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.
Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.
There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
Line 1: Two space-separated integers: F and R
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Line 1: A single integer that is the number of new paths that must be built.
Sample Input
7 71 22 33 42 54 55 65 7
Sample Output
2
Hint
Explanation of the sample:
One visualization of the paths is:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
One visualization of the paths is:
1 2 3 +---+---+ | | | | 6 +---+---+ 4 / 5 / / 7 +Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.
1 2 3 +---+---+ : | | : | | 6 +---+---+ 4 / 5 : / : / : 7 + - - - -Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
Every pair of fields is, in fact, connected by two routes.
It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
/***poj3177&&poj3352 无向图的双连通问题(至少加几条边原图变为双连通图)解题思路:tarjan缩点,将原图变成有向无环图,那么答案就是叶子节点数n再加一的一半*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=5005;int head[maxn],ip;int n,m,du[maxn];int low[maxn],dfn[maxn],st[maxn],inst[maxn],top,dex,cnt,belong[maxn];void init(){ memset(head,-1,sizeof(head)); ip=0;}struct note{ int v,next; bool cut;}edge[maxn*2];void addedge(int u,int v){ edge[ip].v=v,edge[ip].cut=false,edge[ip].next=head[u],head[u]=ip++;}void tarjan(int u,int pre){ dfn[u]=low[u]=++dex; st[top++]=u; inst[u]=1; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; if(!dfn[v]) { tarjan(v,u); if(low[u]>low[v])low[u]=low[v]; if(dfn[u]<low[v]) { // bridge++; edge[i].cut=true; edge[i^1].cut=true; } } else if(inst[v]&&low[u]>dfn[v]) low[u]=dfn[v]; } if(low[u]==dfn[u]) { int v; cnt++; do { v=st[--top]; inst[v]=0; belong[v]=cnt; } while(v!=u); }}void solve(){ memset(dfn,0,sizeof(dfn)); memset(inst,0,sizeof(inst)); top=dex=cnt=0; tarjan(1,-1); memset(du,0,sizeof(du)); for(int u=1;u<=n;u++) { for(int i=head[u];i!=-1;i=edge[i].next) { if(edge[i].cut) { du[belong[u]]++; } } } int ans=0; for(int i=1;i<=cnt;i++) { if(du[i]==1) ans++; } printf("%d\n",(ans+1)/2);}int main(){ while(~scanf("%d%d",&n,&m)) { init(); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } solve(); } return 0;}
0 0
- poj3177&&poj3352 无向图的双连通问题(至少加几条边原图变为双连通图)
- POJ3177.Redundant Paths——增加多少条边使原图变为边双连通图
- poj3177 求无向图加多少点双连通
- poj3352[无向图双连通分量]
- Tarjan算法求至少要添加几条边才能使无向连通图变为边双连通图。
- Tarjan算法求至少要添加几条边才能使无向连通图变为边双连通图。
- poj3352--旅游岛修路--双连通--无向图邻接表的容量是边数量的2倍
- 无向图的双连通块(点的双连通)&&边的双连通算法
- poj3177 Redundant Paths 无向图 双连通分量 桥
- 无向图的割点,割边,点双连通,边双连通模板
- 无向图的桥、割点、边双连通
- POJ-3352(无向图边双连通部分)
- POJ3177,/3352.求最少添加多少边使无向图边双连通
- (POJ 3352)无向图的边双连通分量模板题 + 在一个图中最少加几条边可以使得图边双连通
- poj3352 Road Construction (双连通)
- 无向图的双连通分量
- 【无向图的双连通分量】
- 无向图的双连通分量
- Android之Service分析
- Spring整合 RMI
- java
- phpredis
- Activity状态
- poj3177&&poj3352 无向图的双连通问题(至少加几条边原图变为双连通图)
- SUSE下与RHEL下yum相似的RPM包管理工具zypper
- GridView--学习
- Java&&Clone之浅拷贝和深拷贝
- Effective C++ 读书笔记 条款22:将成员变量声明为private
- Win8如何关闭Hyper-V,支持VMWare虚拟机
- 计算机网络知识储备总结
- 系统相同签名
- C++实现删除字符串中所有重复出现的字符