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.

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

  1. 7 7
  2. 1 2
  3. 2 3
  4. 3 4
  5. 2 5
  6. 4 5
  7. 5 6
  8. 5 7

Sample Output

  1. 2
  1. 题目大意:直接抽象一下问题:给你一个无向连通图,计算最少需要添加多少边,才能使得任意两点之间至少有两条相互“边独立”的道路(即两条路径中没有相同的边)。
  1. 解题思路:这里要用到边双连通分量的知识,先解释一下:在边双连通分量中,不存在割边,其中任何一对顶点之间至少存在两条无公共边的路径(允许有公共内部顶点)。很容易看出,边连通分量中的所有点可以缩为一个点。这样原图就大大简化了,缩点之后的图中的边就只剩下桥了,然后统计出新生成的图(准确说应该是树)中的度为 1 的顶点个数sum ,运用结论(sum + 1)/ 2 就得到答案了。
  1. Ps:缩点时要用到并查集。。
  1. 请看代码:
  1. #include<iostream>
  2. #include<cstring>
  3. #include<string>
  4. #include<cmath>
  5. #include<cstdio>
  6. #include<vector>
  7. #include<algorithm>
  8. using namespace std ;
  9. const int MAXN = 5005 ;
  10. struct Node
  11. {
  12. int adj ;
  13. int e ; // 边的序号
  14. Node *next ;
  15. } ;
  16. Node mem[MAXN * 2] ; // 边节点的数组
  17. int memp ; // 统计边节点
  18. Node *vert[MAXN] ; // 顶点指针数组
  19. int set[MAXN] ; // 用于并查集
  20. bool vis[MAXN] ; // 标记数组,记录顶点是否被访问过
  21. bool vise[MAXN * 2] ; // 标记数组,记录边是否被访问过
  22. int low[MAXN] ; // 记录顶点在深度优先搜索生成树中通过自己的子孙(如果有的话)以及一条回边
  23. // 可以到达的最小深度
  24. int dfn[MAXN] ; // 记录顶点在深度优先搜索生成树中所在的深度
  25. int bridges[MAXN * 2][2] ; // 记录桥的两端顶点
  26. int belong[MAXN] ; // 记录每个顶点所属的边连通分量
  27. int d[MAXN] ; // 统计桥的两端顶点(缩点之后)的度
  28. int cbridges ; // 记录原图中桥的个数
  29. int tmpdfn ;
  30. int counte ; // 给图中的边编号
  31. int sumfz ; // 统计原图中边连通分量个数
  32. int root ; // 记录根节点
  33. int n , m ;
  34. void clr() // 初始化
  35. {
  36. memp = 0 ;
  37. counte = 0 ;
  38. memset(vis , 0 ,sizeof(vis)) ;
  39. memset(vert , 0 , sizeof(vert)) ;
  40. memset(vise , 0 , sizeof(vise)) ;
  41. memset(belong , -1 , sizeof(belong)) ;
  42. memset(bridges , -1 , sizeof(bridges)) ;
  43. memset(low , 0 , sizeof(low)) ;
  44. memset(dfn , 0 , sizeof(dfn)) ;
  45. }
  46. int find(int x) // 并查集(查找部分)
  47. {
  48. int r = x ;
  49. int t ;
  50. while (r != set[r])
  51. {
  52. r = set[r] ;
  53. }
  54. /* while (x != set[x]) // 并查集的优化 , 可以加在程序中
  55. {
  56. t = set[x] ;
  57. set[x] = r ;
  58. x = t ;
  59. }*/
  60. return r ;
  61. }
  62. void unitset(int i , int j) // 并查集(合并部分)
  63. {
  64. int tx = find(i) ;
  65. int ty = find(j) ;
  66. if(tx < ty)
  67. {
  68. set[ty] = tx ;
  69. }
  70. else
  71. {
  72. set[tx] = ty ;
  73. }
  74. }
  75. void init() // 输入
  76. {
  77. int i , j ;
  78. for(i = 0 ; i < m ; i ++)
  79. {
  80. int a , b ;
  81. scanf("%d%d" , &a , &b) ; //建图
  82. mem[memp].adj = b ;
  83. mem[memp].e = counte ;
  84. mem[memp].next = vert[a] ;
  85. vert[a] = &mem[memp] ;
  86. memp ++ ;
  87.  
  88. mem[memp].adj = a ;
  89. mem[memp].e = counte ++ ;
  90. mem[memp].next = vert[b] ;
  91. vert[b] = &mem[memp] ;
  92. memp ++ ;
  93.  
  94. root = b ;
  95. }
  96. }
  97. void dfs(int u) // 找桥
  98. {
  99. Node *p = vert[u] ;
  100. while (p != NULL)
  101. {
  102. int v = p -> adj ;
  103. int te = p -> e ;
  104. if(!vise[te]) // 图中可能有重边,所以应先判断此边是否被访问过
  105. {
  106. vise[te] = 1 ;
  107. if(!vis[v])
  108. {
  109. vis[v] = 1 ;
  110. dfn[v] = low[v] = ++ tmpdfn ;
  111. dfs(v) ;
  112. low[u] = min(low[u] , low[v]) ;
  113. if(low[v] > dfn[u]) // (u , v) 是桥
  114. {
  115. bridges[cbridges][0] = u ;
  116. bridges[cbridges ++][1] = v ;
  117. }
  118. else // 如果(u,v)不是桥,那么u、v必在一个边连通分量中
  119. {
  120. unitset(u , v) ;
  121. }
  122. }
  123. else
  124. {
  125. low[u] = min(low[u] , dfn[v]) ;
  126. }
  127. }
  128. p = p -> next ;
  129. }
  130. }
  131. int countfz() //统计边连通分支数(缩点)
  132. {
  133. int i ;
  134. int k ;
  135. int fz = 0 ;
  136. for(i = 1 ; i <= n ; i ++)
  137. {
  138. k = find(i) ;
  139. if(belong[k] == -1)
  140. {
  141. belong[k] = fz ++ ;
  142. }
  143. belong[i] = belong[k] ; // 缩点
  144. }
  145. return fz ;
  146. }
  147. void solve()
  148. {
  149. int i ;
  150. for(i = 1 ; i <= n ; i ++) // 初始化并查集
  151. {
  152. set[i] = i ;
  153. }
  154. tmpdfn = 1 ;
  155. cbridges = 0 ;
  156. vis[root] = 1 ;
  157. dfn[root] = low[root] = tmpdfn ;
  158. dfs(root) ;
  159. sumfz = countfz() ;
  160. memset(d , 0 , sizeof(d)) ;
  161. for(i = 0 ; i < cbridges ; i ++) // 统计各个边连通分量的度
  162. {
  163. int ta = bridges[i][0] ;
  164. int tb = bridges[i][1] ;
  165. d[belong[ta]] ++ ;
  166. d[belong[tb]] ++ ;
  167. }
  168. int sumd1 = 0 ;
  169. for(i = 0 ; i < sumfz ; i ++)
  170. {
  171. if(d[i] == 1)
  172. sumd1 ++ ;
  173. }
  174. printf("%d\n" , (sumd1 + 1) / 2) ; // (度数为1的顶点个数 + 1)/ 2 即得答案
  175. }
  176. int main()
  177. {
  178. while (scanf("%d%d" , &n , &m) != EOF)
  179. {
  180. clr() ;
  181. init() ;
  182. solve() ;
  183. }
  184. return 0 ;
  185. }
  1.  
 

POJ 3177 Redundant Paths - from lanshui_Yang的更多相关文章

  1. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  2. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  3. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  4. POJ 3177——Redundant Paths——————【加边形成边双连通图】

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

  5. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  6. poj 3177 Redundant Paths

    题目链接:http://poj.org/problem?id=3177 边双连通问题,与点双连通还是有区别的!!! 题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的 ...

  7. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  8. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  9. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

随机推荐

  1. Xcode如何添加字体库--

    1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序viewdidload中加载一下一段代码 NSArray *familyNames = [UI ...

  2. Android中振动器(Vibrator)的使用

    系统获取Vibrator也是调用Context的getSystemService方法,接下来就可以调用Vibrator的方法控制手机振动了.Vibrator只有三个方法控制手机振动: 1.vibrat ...

  3. 向日葵sunlogin配置

    客户端配置: xxxx@TIM sunlogin_linux_1.0.0.25020]$ lsbin  html  install_sunlogin.sh  readme.txt  script  u ...

  4. ECSHOP分类页面筛选功能(按分类下子分类和品牌筛选)

    其实分类页面里面本来就有相关的品牌.属性.分类的筛选功能在category.php和模板加上相应的功能即可 1.读出当前分类的所有下级分类 $chlidren_category = $GLOBALS[ ...

  5. 【甘道夫】Apache Hadoop 2.5.0-cdh5.2.0 HDFS Quotas 配额控制

    前言 HDFS为管理员提供了针对文件夹的配额控制特性,能够控制名称配额(指定文件夹下的文件&文件夹总数),或者空间配额(占用磁盘空间的上限). 本文探究了HDFS的配额控制特性,记录了各类配额 ...

  6. Windows Phone开发(48):不可或缺的本地数据库

    原文:Windows Phone开发(48):不可或缺的本地数据库 也许WP7的时候,是想着让云服务露两手,故似乎并不支持本地数据库,所有数据都上传上"云"数据库中.不过呢,在SD ...

  7. Shiro学习笔记(5)——web集成

    Web集成 shiro配置文件shiroini 界面 webxml最关键 Servlet 測试 基于 Basic 的拦截器身份验证 Web集成 大多数情况.web项目都会集成spring.shiro在 ...

  8. 使用jni技术进行android应用签名信息核查及敏感信息保护

           近期业余时间写了一款应用<摇啊摇>,安智.安卓.360等几个应用商店已经陆续审核通过并上线.从有想法到终于将产品做出来并公布,断断续续花了近二个半月的业余时间,整体来讲还算顺 ...

  9. 1、Cocos2dx 3.0游戏开发三找一小块前言

    尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 前言 Cocos2d-x 是一个通用 ...

  10. 配置jndi服务,javax.naming.NamingException的四种情况

    1.当jndi服务没有启动,或者jndi服务的属性没有设置正确,抛出如下异常: javax.naming.CommunicationException: Can't find SerialContex ...