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. 【译】ASP.NET MVC 5 教程 - 3:添加视图

    原文:[译]ASP.NET MVC 5 教程 - 3:添加视图 在本节内容中,我们将修改HelloWorldController类,使用视图模板来干净利索的封装生成HTML响应客户端的过程. 您将创建 ...

  2. ural1018(树形dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题意:给一棵边有权值的二叉树,节点编号为1-n,1是根节点 ...

  3. HDU 4790 Just Random 数学

    链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...

  4. STM32串口乱码

    库函数默认8MHz晶振,应根据实际硬件选择 # CMSIS/stm32f10x.h #define HSE_VALUE ((uint32_t)12000000) #if !defined HSE_VA ...

  5. 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)

    在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...

  6. java文字转成拼音

    package com.jframe.kit; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4 ...

  7. Redis 的性能幻想与残酷现实(转)

    2011 年,当初选择 Redis 作为主要的内存数据存储,主要吸引我的是它提供多样的基础数据结构可以很方便的实现业务需求.另一方面又比较担心它的性能是否足以支撑,毕竟当时 Redis 还属于比较新的 ...

  8. 不用Root权限获取已经安装的Apk安装包

    在安卓设备上安装的apk都会被保留一份在/data/app目录下,但是该目录对于普通用户来说只有可执行权限,是无法访问的. 但是其子文件具有可读权限. 意思也就说我们直接去查看/data/app这个目 ...

  9. poj 3311 状压DP

    经典TSP变形 学到:1.floyd  O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ...

  10. Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式

    简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...