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

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.

 
模拟赛第二题居然考边双联通分量……我图论很差的啊
题意是给一个连通图,最少添加多少条边,使得任意两点之间有两条无公共边的路(可以有公共点)
这题有个结论的……若tarjan缩完点后所有叶节点的个数是x,那么答案是(x+1)/2
这个要画图理解,有点麻烦。(这时候你就需要善良的学长)
总之就是每次选取lca最大的两个点连无向边,一直到叶节点搞完为止。+1是因为如果两两配对完还剩一个还要再连一条
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{int to,next;}e[1000010];
int n,m,cnt=1,cnt3,tt,sum;
int head[100010];
int dfn[100010],low[100010],belong[100010];
int zhan[100010],top;
bool inset[100010];
int I[100010],O[100010];
inline void ins(int u,int v)
{
e[++cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v)
{
ins(u,v);
ins(v,u);
}
inline void dfs(int x,int fa)
{
zhan[++top]=x;inset[x]=1;
dfn[x]=low[x]=++tt;
for(int i=head[x];i;i=e[i].next)
if (i!=(fa^1))
if (!dfn[e[i].to])
{
dfs(e[i].to,i);
low[x]=min(low[x],low[e[i].to]);
}else if (inset[e[i].to])low[x]=min(low[x],dfn[e[i].to]);
if (low[x]==dfn[x])
{
cnt3++;
int p=-1;
while (p!=x)
{
p=zhan[top--];
belong[p]=cnt3;
inset[p]=0;
}
}
}
inline void tarjan()
{
for (int i=1;i<=n;i++)if (!dfn[i])dfs(i,0);
}
int main()
{
n=read();m=read();
for (int i=1;i<=m;i++)
{
int x=read(),y=read();
insert(x,y);
}
tarjan();
for (int i=1;i<=n;i++)
for (int j=head[i];j;j=e[j].next)
if (belong[i]!=belong[e[j].to])
{
O[belong[i]]++;
I[belong[e[j].to]]++;
}
for (int i=1;i<=cnt3;i++)
if (I[i]==1)sum++;
printf("%d\n",(sum+1)/2);
}

  

poj3177 Redundant Paths的更多相关文章

  1. [POJ3177]Redundant Paths(双联通)

    在看了春晚小彩旗的E技能(旋转)后就一直在lol……额抽点时间撸一题吧…… Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  2. POJ3177 Redundant Paths 双连通分量

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

  3. POJ3177:Redundant Paths(并查集+桥)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19316   Accepted: 8003 ...

  4. POJ3177 Redundant Paths —— 边双联通分量 + 缩点

    题目链接:http://poj.org/problem?id=3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total ...

  5. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...

  6. [POJ3177]Redundant Paths(双连通图,割边,桥,重边)

    题目链接:http://poj.org/problem?id=3177 和上一题一样,只是有重边. 如何解决重边的问题? 1.  构造图G时把重边也考虑进来,然后在划分边双连通分量时先把桥删去,再划分 ...

  7. POJ3177 Redundant Paths【双连通分量】

    题意: 有F个牧场,1<=F<=5000,现在一个牧群经常需要从一个牧场迁移到另一个牧场.奶牛们已经厌烦老是走同一条路,所以有必要再新修几条路,这样它们从一个牧场迁移到另一个牧场时总是可以 ...

  8. poj3352 Road Construction & poj3177 Redundant Paths (边双连通分量)题解

    题意:有n个点,m条路,问你最少加几条边,让整个图变成边双连通分量. 思路:缩点后变成一颗树,最少加边 = (度为1的点 + 1)/ 2.3177有重边,如果出现重边,用并查集合并两个端点所在的缩点后 ...

  9. POJ3177 Redundant Paths【tarjan边双联通分量】

    LINK 题目大意 给你一个有重边的无向图图,问你最少连接多少条边可以使得整个图双联通 思路 就是个边双的模板 注意判重边的时候只对父亲节点需要考虑 你就dfs的时候记录一下出现了多少条连向父亲的边就 ...

随机推荐

  1. 【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(3)

    上篇文章分析到了定时器的定义.这篇的重点就是定时器是怎样执行起来的. 1.从main中寻找定时器的回调 讲定时器的执行,就不得不触及到cocos2dx的main函数了,由于定时器是主线程上执行的.并非 ...

  2. Opencv学习笔记(六)SURF学习笔记

    原创文章,转载请注明出处:http://blog.csdn.net/crzy_sparrow/article/details/7392345 本人挺菜的,肯定有非常多错误纰漏之处 ,希望大家不吝指正. ...

  3. Android(java)学习笔记257:JNI之helloword案例(利用NDK工具)

    1.逻辑思路过程图: 2.下面通过一个HelloWorld案例来说明一下JNI利用NDK开发过程(步骤) 分析:我们在Win7系统下编译的C语言代码,我们知道C语言依赖操作系统,不能跨平台,所以我们要 ...

  4. OpenStack对象存储——Swift

    OpenStack Object Storage(Swift)是OpenStack开源云计算项目的子项目之一,被称为对象存储,提供了强大的扩展性.冗余和持久性.本文将从架构.原理 和实践等几方面讲述S ...

  5. 异步tcp通信——APM.Core 服务端概述

    为什么使用异步 异步线程是由线程池负责管理,而多线程,我们可以自己控制,当然在多线程中我们也可以使用线程池.就拿网络扒虫而言,如果使用异步模式去实现,它使用线程池进行管理.异步操作执行时,会将操作丢给 ...

  6. Windows服务器之间rsync同步文件

    两台windows7机器 server:192.168.12.104 client:192.168.12.103 目的:将server上的E盘的目录FYFR里面的内容定时同步到client上的D盘下F ...

  7. Win8 IE10 只能以管理员打开的解决方法

    Win8 IE10 只能以管理员打开的解决方法 使用 Windows8 一段时间后,最近遇到 IE10 打不开,只能以管理员身份打开,很是郁闷.无论禁用IE加载项还是恢复默认设置都无效,也看到论坛不少 ...

  8. xib添加手势后报错:-[UITapGestureRecognizer setFrame:]: unrecognized selector sent to instance xxx

    主要原因如下: + (instancetype)mineHeaderView { return [[NSBundle mainBundle] loadNibNamed:@"DDMineHea ...

  9. js的相关验证

    1 var JavaScriptCommon = { /*身份证号码校验*/ VerifyID: function (socialNo) { if (socialNo == "") ...

  10. java学习——正则表达式

    本文内容来源于  历经5年锤练--史上最适合初学者入门的Java基础视频 例:要求QQ号长度为5~15位,不能以0开头 String qq="123456"; String reg ...