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. 阿里巴巴2015研究project普通笔试题,与答案

    欢迎您对这篇文章的其他建议.我可以留言在以下平台. 个人博客网站:www.anycodex.com/blog/ Csdn博客网站:http://my.csdn.net/?ref=toolbar 微博: ...

  2. c++map按value排序--将map的pair对保存到vector中,然后写比较仿函数+sort完成排序过程。

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行 ...

  3. hdu2102(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 分析:bfs求最短时间到达'P'点,不过本题有好几个trick,我都踩到了,自己还是太嫩了... ...

  4. poj3295 Tautology , 计算表达式的值

    给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...

  5. SharePoint 2013的HTML5特性之响应式布局

    今天偶然看到一本书<Pro SharePoint 2013 Branding and Responsive Web Development>,看到SharePoint 2013基于HTML ...

  6. C库函数标准编程之fscanf()函数解读及其实验

    函数功能 fscanf()函数用于从参数stream的文件流中读取format格式的内容,然后存放到...所指定的变量中去.字符串以空格或换行符结束(实验1中会对它进一步说明) 函数格式 字符格式说明 ...

  7. SE 2014年5月27日

    R1模拟总部,R2 与R3模拟分部 如图配置 要求使用 GRE over IPSec VPN 主模式,启用动态路由协议rip使得总部与两分部内网可相互通讯,但要求分部用户数据流不允许互通! 步骤: 1 ...

  8. poj1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 题意:有N个点,每两个点至多只有一条边,如果在一个结点上放一个士兵,那他能看守与之相连的边,问最少放多少个兵,才能把所有的边能看 ...

  9. HDOJ 5276 YJC tricks time multimap

    multimap的使用 YJC tricks time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K ...

  10. linuxserver启动过程

    随着Linux的应用日益广泛.特别是在网络应用方面,有大量的网络server使用Linux操作系统.因为Linux的桌面应用和Windows相比另一 定的差距.所以在企业应用中往往是Linux和Win ...