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. Ipsec transport mode and turnnel mode

     隧道(tunnel)模式:用户的整个IP数据包被用来计算AH或ESP头,AH或ESP头以及ESP加密的用户数据被封装在一个新的IP数据包中.通常,隧道模式应用在两个安全网关之间的通讯. 传输(t ...

  2. Windows下安装MySQLdb, Python操作MySQL数据库的增删改查

    这里的前提是windows上已经安装了MySQL数据库,且配置完成,能正常建表能操作. 在此基础上仅仅需安装MySQL-python-1.2.4b4.win32-py2.7.exe就ok了.仅仅有1M ...

  3. Hibernate实体对象继承策略

    Hibernate继承策略总共同拥有三种,一种是共用一张表:一种是每一个类一张表,表里面储存子类的信息和父类的信息:另一种是通过表连接的方式.每一个类都有一张表,可是子类相应的表仅仅保存自己的信息,父 ...

  4. SE 2014年5月6日

    如图配置: 三台交换机两两相连接,构成一二层环路,同时为了保证链路的较为可靠,使用双线链接 请用自己的语言描述以上拓扑搭建的优劣势:并使用哪些技术较为合理,请描述并实施 SW3为接入层交换机,下链接三 ...

  5. 灰度图像阈值化分割常见方法总结及VC实现

    转载地址:http://blog.csdn.net/likezhaobin/article/details/6915755 在图像处理领域,二值图像运算量小,并且能够体现图像的关键特征,因此被广泛使用 ...

  6. cocos2d-x 3.2 它 三消游戏——万圣节大作战

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  7. Android实现隐藏状态栏和标题栏

    隐藏标题栏需要使用预定义样式:android:theme=”@android:style/Theme.NoTitleBar”. 隐藏状态栏:android:theme=”@android:style/ ...

  8. Java EE (9) -- JDBC & JTA

    Connection接口中定义了5中隔离级别常量 Connection.TRANSACTION_NONE  --  不支持事务 Connection.TRANSACTION_READ_UNCOMMIT ...

  9. C语言cgi(1)

    1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #2, 60ish pointsJune 9, 2014Follow ...

  10. Eclipse with C++: "Launch failed. Binary not found."

    Eclipse with C++:  "Launch failed. Binary not found." (windows 7) 用Eclipse创建一个Hello world ...