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. c++中volatile详解

    1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...

  2. hdu5119(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5119 分析:dp[i][j]表示由前i个数组成异或和为j的方法数,则dp[i][j]=d[i-1][j ...

  3. SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中

    SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中 SOLOWHEEL俱乐部聚会活动火热报名中

  4. H.O.T candy

    candy是什么意思_candy在线翻译_英语_读音_用法_例句_海词词典 candy

  5. Android网络编程http派/申请服务

    最近的研究Android网络编程知识,这里有一些想法,今晚学习.与您分享. 在实际的应用程序的开发非常需要时间appserver请求数据,那么app怎样发送请求呢?以下的代码就是当中的一种情况.使用H ...

  6. hibernate得知——Set设置配置

    Set设置配置 创建数据表:表关系的员工有多重身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR ...

  7. 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数

    Ready 事件不一定 ready 使用 easyloader 的时候,必须要注意到脚本的加载时机问题,easyloader 会异步加载模块,所以,你使用的模块不一定已经加载了.比如下面的代码. &l ...

  8. POJ 1696 Space Ant(点积的应用)

    Space Ant 大意:有一仅仅蚂蚁,每次都仅仅向当前方向的左边走,问蚂蚁走遍全部的点的顺序输出.開始的点是纵坐标最小的那个点,開始的方向是開始点的x轴正方向. 思路:从開始点開始,每次找剩下的点中 ...

  9. 期望dp专题

    一直不明白为什么概率是正推,期望是逆推. 现在题目做多了,慢慢好像有点明白了 poj2096 收集bug,  有n个种类的bug,和s个子系统.  每找到一个bug需要一天. 要我我们求找到n个种类的 ...

  10. Linux从零到高手的进阶心得(转)

    从2006年毕业至今,从事IT行业已经接近8个年头. 一路走来有很多心路历程和技术心得都写在了51CTO的博客中,不少文字现在看来已显稚嫩,但是这正是我真实的成长之路.这八年,从最基础的网络管理员开 ...