题目链接:http://poj.org/problem?id=3177

Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15852   Accepted: 6649

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.

Source

题解:

1. 已知题目给出的图是联通图,那么我们可以用Tarjan算法求出每个边双联通分量。为什么是边双联通分量而不是点双联通分量或者其他?答:因为题目要求每个点之间至少有两条路径(并且路径中不能有重叠的边),位于同一个边双联通分量的点之间都至少有两条路径,但是位于不同边双联通分量的点只有一条路径。所以需要根据边双联通对原图进行划分。

2.对每个边双连通分量进行缩点,得到的是一棵无根树。那么无根树怎样才能变成边双联通图呢?至少需要添加几条边呢?答:只需要把所有叶子结点都“消灭”掉就可以了。那么最少需要添加:(叶子结点数+1)/2 条边。

代码如下:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; struct Edge
{
int to, next;
bool cut;
}edge[MAXN<<];
int head[MAXN], tot; int index, dfn[MAXN], low[MAXN];
int block, belong[MAXN];
int top, Stack[MAXN], instack[MAXN];
int degree[MAXN]; void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].cut = false;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u, int pre)
{
low[u] = dfn[u] = ++index;
Stack[top++] = u;
instack[u] = true;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(v==pre) continue;
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v]>dfn[u])
{
edge[i].cut = true;
edge[i^].cut = true;
}
}
else if(instack[v])
low[u] = min(low[u], dfn[v]);
} if(low[u]==dfn[u])
{
block++;
int v;
do
{
v = Stack[--top];
instack[v] = false;
belong[v] = block;
}while(v!=u);
}
} void init()
{
tot = ;
memset(head,-,sizeof(head)); index = ;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low)); block = top = ;
memset(instack,,sizeof(instack)); memset(degree,,sizeof(degree));
} int main()
{
int n, m;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d",&u,&v);
addedge(u, v);
addedge(v,u);
} Tarjan(, );
for(int u = ; u<=n; u++)
for(int i = head[u]; i!=-; i = edge[i].next)
if(edge[i].cut) //不需要两端都加,因为一条割边被标记了两次。一次正好对应一个端点。
degree[belong[u]]++; int leaf = ;
for(int i = ; i<=block; i++)
if(degree[i]==) leaf++; printf("%d\n", (leaf+)/);
}
}
												

POJ3177 Redundant Paths —— 边双联通分量 + 缩点的更多相关文章

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

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

  2. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  3. HDU5409---CRB and Graph 2015多校 双联通分量缩点

    题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...

  4. POJ3694 Network —— 边双联通分量 + 缩点 + LCA + 并查集

    题目链接:https://vjudge.net/problem/POJ-3694 A network administrator manages a large network. The networ ...

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

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

  6. poj3177 Redundant Paths 边双连通分量

    给一个无向图,问至少加入多少条边能够使图变成双连通图(随意两点之间至少有两条不同的路(边不同)). 图中的双连通分量不用管,所以缩点之后建新的无向无环图. 这样,题目问题等效于,把新图中度数为1的点相 ...

  7. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

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

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

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

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

随机推荐

  1. Jedis 工具类

    package com.pig4cloud.pigx.admin.utils; import redis.clients.jedis.*; import java.util.ArrayList; im ...

  2. Dream City(线性DP)

    描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...

  3. 使用SqlParameter.SqlDbType和SqlParameter.Size时需要注意的地方

    1.DbParameter类是SqlParameter和OracleParameter类的父类.DbParameter.Size用来获取或设置列中数据的最大尺寸(只对文本数据有用). 2.数据类型Ch ...

  4. Hybris Virtualjdbc Extension

    作者:Eason 编写日期:2018/07/31 联系方式:13920409462 1. Extension 说明 virtualjdbc extension 提供了虚拟JDBC驱动程序的实现. 通过 ...

  5. [APIO2012] 派遣 dispatching

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4580  Solved: 2348 Description 在一个忍者的帮派里,一些忍者们被选中派遣给 ...

  6. BZOJ3926 (后缀自动机)

    BZOJ3926 诸神眷顾的幻想乡 Problem : 给一个n个节点的树(n<=10^5), 每个点有一种颜色(c<=10), 询问所有点对之间路径组成字符串的种类.保证叶子节点小于等于 ...

  7. AndroidUI的基本结构

    AndroidUI的基本结构 创建时间: 2013-9-13 11:05 更新时间: 2013-9-13 11:05

  8. jxls使用模版导出Excel

    /**     * 使用模版导出Excel     */    @SuppressWarnings({ "unchecked", "deprecation" } ...

  9. MongoDB学习day09--Mongoose数据校验

    一.Mongoose检验参数 required : 表示这个数据必须传入max: 用于 Number 类型数据, 最大值 min: 用于 Number 类型数据, 最小值 enum:枚举类型, 要求数 ...

  10. 转: ORACLE存储过程笔记2----运算符和表达式

    运算符和表达式     关系运算 =等于<>,!=不等于<小于>大于<=小于等于>=大于等于       一般运算   +加-减*乘/除:=赋值号=>关系号. ...