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

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2
    题目大意:直接抽象一下问题:给你一个无向连通图,计算最少需要添加多少边,才能使得任意两点之间至少有两条相互“边独立”的道路(即两条路径中没有相同的边)。
    解题思路:这里要用到边双连通分量的知识,先解释一下:在边双连通分量中,不存在割边,其中任何一对顶点之间至少存在两条无公共边的路径(允许有公共内部顶点)。很容易看出,边连通分量中的所有点可以缩为一个点。这样原图就大大简化了,缩点之后的图中的边就只剩下桥了,然后统计出新生成的图(准确说应该是树)中的度为 1 的顶点个数sum ,运用结论(sum + 1)/ 2 就得到答案了。
    Ps:缩点时要用到并查集。。
    请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std ;
const int MAXN = 5005 ;
struct Node
{
int adj ;
int e ; // 边的序号
Node *next ;
} ;
Node mem[MAXN * 2] ; // 边节点的数组
int memp ; // 统计边节点
Node *vert[MAXN] ; // 顶点指针数组
int set[MAXN] ; // 用于并查集
bool vis[MAXN] ; // 标记数组,记录顶点是否被访问过
bool vise[MAXN * 2] ; // 标记数组,记录边是否被访问过
int low[MAXN] ; // 记录顶点在深度优先搜索生成树中通过自己的子孙(如果有的话)以及一条回边
// 可以到达的最小深度
int dfn[MAXN] ; // 记录顶点在深度优先搜索生成树中所在的深度
int bridges[MAXN * 2][2] ; // 记录桥的两端顶点
int belong[MAXN] ; // 记录每个顶点所属的边连通分量
int d[MAXN] ; // 统计桥的两端顶点(缩点之后)的度
int cbridges ; // 记录原图中桥的个数
int tmpdfn ;
int counte ; // 给图中的边编号
int sumfz ; // 统计原图中边连通分量个数
int root ; // 记录根节点
int n , m ;
void clr() // 初始化
{
memp = 0 ;
counte = 0 ;
memset(vis , 0 ,sizeof(vis)) ;
memset(vert , 0 , sizeof(vert)) ;
memset(vise , 0 , sizeof(vise)) ;
memset(belong , -1 , sizeof(belong)) ;
memset(bridges , -1 , sizeof(bridges)) ;
memset(low , 0 , sizeof(low)) ;
memset(dfn , 0 , sizeof(dfn)) ;
}
int find(int x) // 并查集(查找部分)
{
int r = x ;
int t ;
while (r != set[r])
{
r = set[r] ;
}
/* while (x != set[x]) // 并查集的优化 , 可以加在程序中
{
t = set[x] ;
set[x] = r ;
x = t ;
}*/
return r ;
}
void unitset(int i , int j) // 并查集(合并部分)
{
int tx = find(i) ;
int ty = find(j) ;
if(tx < ty)
{
set[ty] = tx ;
}
else
{
set[tx] = ty ;
}
}
void init() // 输入
{
int i , j ;
for(i = 0 ; i < m ; i ++)
{
int a , b ;
scanf("%d%d" , &a , &b) ; //建图
mem[memp].adj = b ;
mem[memp].e = counte ;
mem[memp].next = vert[a] ;
vert[a] = &mem[memp] ;
memp ++ ; mem[memp].adj = a ;
mem[memp].e = counte ++ ;
mem[memp].next = vert[b] ;
vert[b] = &mem[memp] ;
memp ++ ; root = b ;
}
}
void dfs(int u) // 找桥
{
Node *p = vert[u] ;
while (p != NULL)
{
int v = p -> adj ;
int te = p -> e ;
if(!vise[te]) // 图中可能有重边,所以应先判断此边是否被访问过
{
vise[te] = 1 ;
if(!vis[v])
{
vis[v] = 1 ;
dfn[v] = low[v] = ++ tmpdfn ;
dfs(v) ;
low[u] = min(low[u] , low[v]) ;
if(low[v] > dfn[u]) // (u , v) 是桥
{
bridges[cbridges][0] = u ;
bridges[cbridges ++][1] = v ;
}
else // 如果(u,v)不是桥,那么u、v必在一个边连通分量中
{
unitset(u , v) ;
}
}
else
{
low[u] = min(low[u] , dfn[v]) ;
}
}
p = p -> next ;
}
}
int countfz() //统计边连通分支数(缩点)
{
int i ;
int k ;
int fz = 0 ;
for(i = 1 ; i <= n ; i ++)
{
k = find(i) ;
if(belong[k] == -1)
{
belong[k] = fz ++ ;
}
belong[i] = belong[k] ; // 缩点
}
return fz ;
}
void solve()
{
int i ;
for(i = 1 ; i <= n ; i ++) // 初始化并查集
{
set[i] = i ;
}
tmpdfn = 1 ;
cbridges = 0 ;
vis[root] = 1 ;
dfn[root] = low[root] = tmpdfn ;
dfs(root) ;
sumfz = countfz() ;
memset(d , 0 , sizeof(d)) ;
for(i = 0 ; i < cbridges ; i ++) // 统计各个边连通分量的度
{
int ta = bridges[i][0] ;
int tb = bridges[i][1] ;
d[belong[ta]] ++ ;
d[belong[tb]] ++ ;
}
int sumd1 = 0 ;
for(i = 0 ; i < sumfz ; i ++)
{
if(d[i] == 1)
sumd1 ++ ;
}
printf("%d\n" , (sumd1 + 1) / 2) ; // (度数为1的顶点个数 + 1)/ 2 即得答案
}
int main()
{
while (scanf("%d%d" , &n , &m) != EOF)
{
clr() ;
init() ;
solve() ;
}
return 0 ;
}

 

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. .Net 文本框实现内容提示(仿Google、Baidu)

    原文:.Net 文本框实现内容提示(仿Google.Baidu) 1.Demo下载: 文本框实现内容提示(仿Google.Baidu).rar 2.创建数据库.表(我用的sqlserver2008数据 ...

  2. ServiceProvider实现

    ServiceProvider实现揭秘 [总体设计 ] 本系列前面的文章我们主要以编程的角度对ASP.NET Core的依赖注入系统进行了详细的介绍,如果读者朋友们对这些内容具有深刻的理解,我相信你们 ...

  3. IIS设置允许下载.exe文件解决方法

    最近很多客户使用IIS服务器,然后提示返现宝下载无法找到等无法下载的问题. 返现宝是.exe安装文件,部分服务器或主机可能无法下载. 第一.如果是自己服务器或VPS请按如下设置: 1.设置MIME,让 ...

  4. 用css3实现各种图标效果

    原文:用css3实现各种图标效果 公共样式 应该说现在绝大多数公司的项目前端都是一团乱,不仅仅是js写的没有任何框架而言,css同样也是如此,导致项目如果要升级或者说有新的变更维护起来就特别困难. 最 ...

  5. ios7开发者必知

    如果你想为iOS 设备开发app,你需要知道如何与软件交互,如何设计,你还要知道苹果独特的开发理念和开发工具.真正的能力还需要成功地从其他行业领域借鉴核心概念.最后把所有这些东西糅合进你的信息库中, ...

  6. cocos2D-x 3.5 引擎解析之--引用计数(Ref),自己主动释放池(PoolManager),自己主动释放池管理器( AutoreleasePool)

    #include <CCRef.h> Ref is used for reference count manangement. If a classinherits from Ref. C ...

  7. 修ecshop品牌筛选以LOGO图片形式显示

    如何实现商品列表页属性筛选区品牌筛选以LOGO形式展示,最模板总结ecshop/'>ecshop教程入下: 1.修改 category.php 文件,将(大概215行) $sql = " ...

  8. pygame系列

    在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,pygame系列偷自http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...

  9. ICMP协议Ping命令的应用

    ICMP的全称是 Internet Control Message Protocol ,它是TCP/IP协议族的一个子协议,属于网络层协议,用于在IP主机.路由器之间传递控制消息.从技术角度来讲,就是 ...

  10. Windows 8 应用开发 - 应用栏

    原文:Windows 8 应用开发 - 应用栏      通过应用栏(AppBar)可以在需要时向用户显示各种应用命令.应用栏提供与用户当前页面或当前选定的内容相关的各种命令.默认情况下,应用栏处于隐 ...