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

Hint

Explanation of the sample:

One visualization of the paths is:

   1   2   3
+---+---+
| |
| |
6 +---+---+ 4
/ 5
/
/
7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

   1   2   3
+---+---+
: | |
: | |
6 +---+---+ 4
/ 5 :
/ :
/ :
7 + - - - -

Check some of the routes:
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7

Every pair of fields is, in fact, connected by two routes.

It's possible that adding some other path will also solve the
problem (like one from 6 to 7). Adding two paths, however, is the
minimum.

 

题目所求即为把无向图中缩点为一棵树后,再加边使之成为一个边双连通块。所加边数即为(叶子节点数+1)/2,加边方法为每次取两个LCA最远的叶节点,在他们两个中间连一条边,重复取直到加完。

注意:这道题两点之间会有多条边,不能简单的判断 To != father ,必须判断是否为同一条边。因为边为无向边,所以不知道 i+1 和 i-1 那一条和 i 是同一条边。这里介绍一种妙不可言的方法,把每条无向边的编号赋值为这条边的权,所以只需判断两条边权是否相同即可。

 #include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define file(a) freopen(a".in","r",stdin); freopen(a".out","w",stdout); inline int gi()
{
bool b=; int r=; char c=getchar();
while(c<'' || c>'') { if(c=='-') b=!b; c=getchar(); }
while(c>='' && c<='') { r=r*+c-''; c=getchar(); }
if(b) return -r; return r;
} const int inf = 1e9+, N = , M = ;
int n,m,num,Deep,f[N],dfn[N],low[N],cd[N];
bool b[N];
stack <int> s;
struct data
{
int nx,to,ds;
}da[M]; inline void add (int fr,int to,int ds)
{
da[++num].to=to, da[num].nx=f[fr], da[num].ds=ds, f[fr]=num;
} inline void tarjan (int o,int fa)
{
int i,to;
dfn[o]=low[o]=++Deep; b[o]=;
for (i=f[o]; i; i=da[i].nx)
{
to=da[i].to;
if (da[i].ds == da[fa].ds) continue;
if (!dfn[to]) tarjan (to,i), low[o]=min(low[o],low[to]);
else if (b[to]) low[o]=min(low[o],dfn[to]);
}
b[o]=;
} int main()
{
// file("POJ-3177");
n=gi(), m=gi();
int i,j,x,y;
for (i=; i<=m; i++)
{
x=gi(), y=gi();
add (x,y,i), add (y,x,i);
}
for (i=; i<=n; i++) if (!dfn[i]) tarjan (i,);
for (i=; i<=n; i++)
for (j=f[i]; j; j=da[j].nx)
{
x=low[da[j].to], y=low[i];
if (x != y) cd[y]++;
}
x=;
for (i=; i<=n; i++) if (cd[i] == ) x++;
printf ("%d\n",(x+)/);
return ;
}

欢迎在评论区提问质疑!

POJ-3352 Redundant Paths的更多相关文章

  1. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  2. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  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 (桥,边双连通分量,有重边)

    题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...

  7. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  8. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  9. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  10. POJ 3352 Road Construction ; POJ 3177 Redundant Paths (双联通)

    这两题好像是一样的,就是3177要去掉重边. 但是为什么要去重边呢??????我认为如果有重边的话,应该也要考虑在内才是. 这两题我用了求割边,在去掉割边,用DFS缩点. 有大神说用Tarjan,不过 ...

随机推荐

  1. jQuery事件委托之Safari失效的解决办法--摘抄

    什么是事件委托 事件委托是Jquery中一种事件绑定的方式,不同于常见的事件绑定方式将事件绑定在目标元素上,而是将事件绑定在父级元素上通过事件冒泡来执行绑定函数. //常见的事件绑定(Jquery) ...

  2. [Hihocoder 1289] 403 Forbidden (微软2016校园招聘4月在线笔试)

    传送门 #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Someti ...

  3. python之基本数据类型及深浅拷贝

    一.数据基本类型之set集合 set和dict类似,也是一组key的集合,但不存储value.由于key不能重复,所以,在set中,没有重复的key set集合,是一个无序且不重复的元素集合 1.创建 ...

  4. Linux中有硬件时钟与系统时钟

    在Linux中有硬件时钟与系统时钟等两种时钟.硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟.系统时钟则是指kernel中的时钟.当Linux启动时,系统时钟会去读取硬件时钟的设 ...

  5. zTree 用法小例

    插件地址:链接:http://pan.baidu.com/s/1jHVtyZ0 密码:7kee <select id="getTree" resultType="j ...

  6. 关于整合spring+mybatis 第三种方式-使用注解

    使用注解 1.与前两种方法一致.不过稍许不同的是beans.xml中配置的差异. <!-- 配置sqlSessionFactory --> <bean id="sqlSes ...

  7. BZOJ 4810 [Ynoi2017]由乃的玉米田 (莫队 + bitset)

    题目链接  BZOJ 4810 首先对询问离线, 莫队算法处理. 首先我们可以用bitset维护处当前区间中是否存在某个数. 对于询问1, 我们可以用 ((f >> q[i].x) &am ...

  8. luogu P3420 [POI2005]SKA-Piggy Banks

    题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can either be opened with its correspon ...

  9. bzoj 5216: [Lydsy2017省队十连测]公路建设

    5216: [Lydsy2017省队十连测]公路建设 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 66  Solved: 37[Submit][St ...

  10. PHP实现INT型,SHORT型,STRING转换成BYTE数组

    实现PHP实现INT型,SHORT型,STRING转换成BYTE数组的转化: class Bytes { public static function integerToBytes($val) { $ ...