博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3177——Redundant Paths——————【加边形成边双连通图】
阅读量:5283 次
发布时间:2019-06-14

本文共 4961 字,大约阅读时间需要 16 分钟。

 

Redundant Paths
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit     

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

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

 

题目大意:有n个草场,m条无向边。问让图形成边双连通图需要最少新建多少条无向边。图中给的有重边。

 

解题思路:我们可以先画出缩点后的图,这时已经成了一棵树。那么我们可以看出,要想形成边双连通图,需要没有桥,所以,只要将叶子结点连一条边即可。记叶子个数为leaf。所以需要新建的边即为(leaf+1)/2。

 

#include
#include
#include
#include
#include
using namespace std;const int maxn = 5100;struct Edge{ int from,to,dist,next; Edge(){} Edge(int _to,int _next):to(_to),next(_next){}}edges[maxn*4]; //无向图的边int head[maxn], tot; //邻接表int dfs_clock, dfn[maxn]; //时间戳//Stack:存放边双连通节点、instack:在栈中、ebccno:节点所在分量编号、ebcc_cnt分量数目(从1开始编号)int Stack[maxn], instack[maxn], top, ebccno[maxn], ebcc_cnt;int deg[maxn];//记录缩点的度void init(){ tot = 0; dfs_clock = 0; top = 0; ebcc_cnt = 0; memset(deg,0,sizeof(deg)); memset(head,-1,sizeof(head));}void AddEdge(int _u,int _v){ edges[tot] = Edge(_v,head[_u]); head[_u] = tot++;}int dfs(int u,int fa){ //这里的fa是记录的边的编号,用来处理重边 int lowu = dfn[u] = ++dfs_clock; Stack[++top] = u; //将每个访问的结点放入栈中 // instack[u] = 1; for(int i = head[u]; i != -1; i = edges[i].next){ int v = edges[i].to; if(!dfn[v]){ //如果v没有访问过 int lowv = dfs(v,i); //v及v的后代能访问到的最远祖先 lowu = min(lowu,lowv); //用后代来更新lowu //如果v已经在栈中了,并且这条边不是回边(一条无向边,拆成了有向边,回指了父亲的这条有向边) }else if(dfn[v] < dfn[u] && (fa^1) != i){ //有人在这里用instack[v]替代了判断已经在栈中 lowu = min(lowu,dfn[v]); //用反向边更新lowu } } if(dfn[u] == lowu){ //找到一个边双连通分量 ebcc_cnt++; for(;;){ int v = Stack[top--]; // instack[v] = 0; ebccno[v] = ebcc_cnt; //把节点划分到分量中 if(u == v){ break; } } } // low[u] = lowu; return lowu;}void find_ebcc(int n){ memset(dfn,0,sizeof(dfn)); memset(instack,0,sizeof(instack)); for(int i = 1; i <= n; i++){ if(!dfn[i]){ dfs(i,-1); } }}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ init(); int a,b; for(int i = 0; i < m; i++){ scanf("%d%d",&a,&b); AddEdge(a,b); AddEdge(b,a); } find_ebcc(n); for(int i = 1; i <= n; i++){ for(int j = head[i]; j != -1; j = edges[j].next){ int v = edges[j].to; if(ebccno[i] != ebccno[v]){ deg[ebccno[v]]++; } } } int leaf = 0; for(int i = 1; i <= ebcc_cnt; i++){ if(deg[i] == 1){ leaf++; } } printf("%d\n",(leaf+1)/2); } return 0;}

  

转载于:https://www.cnblogs.com/chengsheng/p/4940944.html

你可能感兴趣的文章
TYVJ.1864.[Poetize I]守卫者的挑战(概率DP)
查看>>
0925 韩顺平java视频
查看>>
iOS-程序启动原理和UIApplication
查看>>
mysql 8.0 zip包安装
查看>>
awk 统计
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
java选择文件时提供图像缩略图[转]
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
string与stringbuilder的区别
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
iOS8统一的系统提示控件——UIAlertController
查看>>
PAT甲级——1101 Quick Sort (快速排序)
查看>>
python创建进程的两种方式
查看>>