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. Day 10 Linux nfs && crond(摘)

    (摘) 介绍: NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系 ...

  2. spark学习(五)总结及其demo

    RDD及其特点 1.RDD是Spark的核心数据模型,但是个抽象类,全称为Resillient Distributed Dataset,即弹性分布式数据集. 2.RDD在抽象上来说是一种元素集合,包含 ...

  3. centos 安装php缓存 apc或zend-opcode

    去官方下载apc:pecl.php.net 搜索apc,安装最新的. #wget http://pecl.php.net/get/APC# tar -xzvf APC-3.1.9.tgz#cd  AP ...

  4. 一个炫酷的Actionbar效果

    今天在网上看到一个炫酷的Actionbar效果,一个老外做的DEMO,目前很多流行的app已经加入了这个效果. 当用户初始进入该界面的时候,为一个透明的 ActiionBar ,这样利用充分的空间显示 ...

  5. openfalcon的安装和使用

    蛮复杂的样子 根据官方文档指导,一步一步走起:https://book.open-falcon.org/zh_0_2/quick_install/prepare.html 单机安装的过程:单击安装会把 ...

  6. iOS开发 下滑隐藏Tabbar

    项目中用到下滑隐藏tabbar,上滑显示.      虽然实现起来非常简单,还是记录一下. -(void)scrollViewDidScroll:(UIScrollView*)scrollView { ...

  7. C标准提前定义宏,调试时加打印非常实用

    #include<stdio.h> int main(int argc, char *argv[]) { printf("File:[%s]\r\n", __FILE_ ...

  8. mmall 项目实战(一)项目初始化

    1.创建 数据库 及 表 数据脚本: /* Navicat Premium Data Transfer Source Server : 182.92.82.103 Source Server Type ...

  9. android-problem——remount of /system failed: Read-only file system

    adb remount后仍旧不能对system进行读写.需要进行adb disable-verity 在Android6.0 (Android M)userdebug版本上(eng版本不存在该问题), ...

  10. C#语法复习1

    一.C#与.net框架 .net是语言无关的. 程序的执行流程: .net兼容语言的源代码文件 .net兼容编译器 程序集(公共中间语言(CIL)common intermediate languag ...