Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10798   Accepted: 4626

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.

下面解析来自:女神的博客

斌神博客上有个不错的总结:斌神的博客

大致题意:

为了保护放牧环境,避免牲畜过度啃咬同一个地方的草皮,牧场主决定利用不断迁移牲畜进行喂养的方法去保护牧草。然而牲畜在迁移过程中也会啃食路上的牧草,所以假设每次迁移都用同一条道路,那么该条道路相同会被啃咬过度而遭受破坏。

如今牧场主拥有F个农场。已知这些农场至少有一条路径连接起来(不一定是直接相连)。但从某些农场去另外一些农场。至少有一条路可通行。为了保护道路上的牧草,农场主希望再建造若干条道路,使得每次迁移牲畜时,至少有2种迁移途径,避免反复走上次迁移的道路。

已知当前有的R条道路。问农场主至少要新建造几条道路,才干满足要求?

解题思路:

“使得每次迁移牲畜时,至少有2种迁移途径,避免反复走上次迁移的道路。”就是说当吧F个农场看作点、路看作边构造一个无向图G时,图G不存在桥。

那么能够建立模型:

给定一个连通的无向图G,至少要加入几条边。才干使其变为双连通图。

当图G存在桥(割边)的时候,它必然不是双连通的。桥的两个端点必然分别属于图G的两个【边双连通分量】。一旦删除了桥,这两个【边双连通分量】必然断开,图G就不连通了。可是假设在两个【边双连通分量】之间再加入一条边。桥就不再是桥了。这两个【边双连通分量】之间也就是双连通了。



那么假设图G有多个【边双连通分量】呢?至少应该加入多少条边,才干使得随意两个【边双连通分量】之间都是双连通(也就是图G是双连通的)

1、 首先要找出图G的全部【边双连通分量】。

2、 把每个【边双连通分量】都看做一个点(即【缩点】)

3、 问题再次被转化为“至少在缩点树上添加多少条树边。使得这棵树变为一个双连通图”。

首先知道一条等式:

若要使得随意一棵树。在添加若干条边后。变成一个双连通图,那么

至少添加的边数 =( 这棵树总度数为1的结点数 + 1 )/ 2。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#define maxn 5010
#define maxm 20010
using namespace std; int n, m;
struct node {
int u, v, next;
};
node edge[maxm];
//缩点后形成树,每一个点的度数
int du[maxn];
int head[maxn], cnt;
int low[maxn], dfn[maxn];
//Belong数组的值是 1 ~ ebc_block
int Stack[maxn], Belong[maxn];
int ebc_block;//边双连通块数
int dfs_clock;
int top;//模拟栈的指针
bool Instack[maxn]; void init(){
cnt = 0;
memset(head, -1, sizeof(head));
} void addedge(int u, int v){
edge[cnt] = {u, v, head[u]};
head[u] = cnt++;
} void getmap(){
while(m--){
int a, b;
scanf("%d%d", &a, &b);
addedge(a, b);
addedge(b, a);
}
} void tarjan(int u, int pre){
int v;
low[u] = dfn[u] = ++dfs_clock;
Stack[top++] = u;
Instack[u] = true;
int have = 1;
for(int i = head[u]; i != -1; i = edge[i].next){
v = edge[i].v;
if(have && v == pre){//去重边
have = 0;
continue;
}
if(!dfn[v]){
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(Instack[v])
low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]){
ebc_block++;
do{
v = Stack[--top];
Instack[v] = false;
Belong[v] = ebc_block;
}
while(v != u);
}
} void suodian(){
memset(du, 0, sizeof(du));
for(int i = 0; i < cnt; i += 2 ){
int u = Belong[edge[i].u];
int v = Belong[edge[i].v];
if(u != v)
du[u]++, du[v]++;
}
} void find(){
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(Instack, false, sizeof(Instack));
memset(Belong, 0, sizeof(Belong));
dfs_clock = 0;
ebc_block = 0;
top = 0;
tarjan(1, -1);//连通图
} void solve(){
int ans = 0;
if(ebc_block == 1){
printf("0\n");
return ;
}
for(int i = 1; i <= ebc_block; ++i)
if(du[i] == 1) ans++;
printf("%d\n", (ans + 1) / 2);
} int main (){
while(scanf("%d%d", &n, &m) != EOF){
init();
getmap();
find();
suodian();
solve();
}
return 0;
}

POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 &amp;&amp; tarjan求ebc &amp;&amp; 缩点构造缩点树】的更多相关文章

  1. POJ 3177 Redundant Paths 无向图边双联通基础题

    题意: 给一个无向图,保证任意两个点之间有两条完全不相同的路径 求至少加多少边才能实现 题解: 得先学会一波tarjan无向图 桥的定义是:删除这条边之后该图不联通 一条无向边(u,v)是桥,当且仅当 ...

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

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

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

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

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

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

  5. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  6. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. POJ 3177 Redundant Paths (tarjan边双连通分量)

    题目连接:http://poj.org/problem?id=3177 题目大意是给定一些牧场,牧场和牧场之间可能存在道路相连,要求从一个牧场到另一个牧场要有至少两条以上不同的路径,且路径的每条pat ...

  8. POJ 3177 Redundant Paths POJ 3352 Road Construction

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

  9. POJ 3177 Redundant Paths(强连通分量)

    题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条 ...

随机推荐

  1. [ACM] HDU 5086 Revenge of Segment Tree(全部连续区间的和)

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  2. ES Segment Memory——本质上就是segment中加到内存的FST数据,因此segment越多,该内存越大

    ElasticSearch优化系列四:ES的heap是如何被瓜分掉的 转自:https://www.jianshu.com/p/f41b706db6c7 以下分别解读几个我知道的内存消耗大户: Seg ...

  3. 【HDU 1847】 Good Luck in CET-4 Everybody!

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1847 [算法] 我们知道,每一种状态,要么必胜,要么必败 记忆化搜索即可 [代码] #includ ...

  4. Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树

    题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...

  5. 2018亚洲区预选赛北京赛站网络赛 D.80 Days 尺取

    题面 题意:你带着K元要去n个城市,这n个城市是环形的,你可以选择任意一个起点,然后顺时针走,对于每个城市,到达时可以获得a元,但是从这里离开又需要花费b元,问你能否找到一个起点(输出花钱最少的那个) ...

  6. Hyper和Vmware冲突,Device/Credential Guard 不兼容

    切换到VM的时候,采用关闭策略 1.PS管理员关闭命令 bcdedit /set hypervisorlaunchtype off 2.系统设置,启用或关闭Windows功能那里,关闭Hyper-V ...

  7. iOS数组越界

    数组越界就是假如你的下标总数现在为32个,然后你在下一秒又执行了一个方法要从50个数据里进行赋值啊筛选之类的,而你此时数组里的值为32个,50的数据还没有请求到,往往会出现数组越界的崩溃信息,大概是这 ...

  8. Hadoop MapReduce编程 API入门系列之网页排序(二十八)

    不多说,直接上代码. Map output bytes=247 Map output materialized bytes=275 Input split bytes=139 Combine inpu ...

  9. Hadoop MapReduce编程 API入门系列之MapReduce多种输出格式分析(十九)

    不多说,直接上代码. 假如这里有一份邮箱数据文件,我们期望统计邮箱出现次数并按照邮箱的类别,将这些邮箱分别输出到不同文件路径下. 代码版本1 package zhouls.bigdata.myMapR ...

  10. Aspose.Words将Word模板打印出来

    利用Aspose.Words将制作好的模板,输出出来自己所需的文件 /// <summary> /// 打印信息和列表 /// </summary> /// <param ...