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

时间:2022-12-21 20:29:05
Redundant Paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13717   Accepted: 5824

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.

题目链接:POJ 3177

给你可能有多个的无向图,求最少加几条边使得图成为一个双连通分量。

做法:tarjan求出桥并把桥边标记删除,再DFS出各个连通块,并标注上所属块id,然后统计缩点之后的每个点的度,显然叶子节点的度为1即只连着一条边,但由于是无向图,会重复算一次,因此实际上所有点的度会多一倍,记叶子节点的个数为$leaf$,则答案为$(leaf+1)/2$,为什么是这样呢?因为当叶子为偶数的时候,可以找到具有最远LCA的叶子,两两之间连一条边形成环,这样便融入了双连通分量中而且这样是最优的方案,奇数的话剩下的一个随意找一个叶子融合一下就行了,这题据说有重边,用id处理一下即可。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=5010;
const int M=10010;
struct edge
{
int to,nxt;
int id,flag;
};
edge E[M<<1];
int head[N],tot;
int dfn[N],low[N],st[N],ts,scc,top,belong[N],deg[N];
bitset<N> ins; void init()
{
CLR(head,-1);
tot=0;
CLR(dfn,0);
CLR(low,0);
ts=scc=top=0;
CLR(belong,0);
CLR(deg,0);
ins.reset();
}
inline void add(int s,int t,int id)
{
E[tot].to=t;
E[tot].id=id;
E[tot].flag=false;
E[tot].nxt=head[s];
head[s]=tot++;
}
void Tarjan(int u,int id)
{
dfn[u]=low[u]=++ts;
ins[u]=1;
st[top++]=u;
int i,v;
for (i=head[u]; ~i; i=E[i].nxt)
{
if(E[i].id==id)
continue;
v=E[i].to;
if(!dfn[v])
{
Tarjan(v,E[i].id);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
E[i].flag=true;
E[i^1].flag=true;
}
}
else if(ins[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++scc;
do
{
v=st[--top];
ins[v]=0;
}while (u!=v);
}
}
void dfs(int u,int x)
{
belong[u]=x;
ins[u]=1;
for (int i=head[u]; ~i; i=E[i].nxt)
{
int v=E[i].to;
if(!ins[v]&&!E[i].flag)
dfs(v,x);
}
}
int main(void)
{
int n,m,a,b,i,j;
while (~scanf("%d%d",&n,&m))
{
init();
for (i=0; i<m; ++i)
{
scanf("%d%d",&a,&b);
add(a,b,i);
add(b,a,i);
}
for (i=1; i<=n; ++i)
if(!dfn[i])
Tarjan(i,-1);
ins.reset();
int block=0;
for (i=1; i<=n; ++i)
if(!ins[i])
dfs(i,++block);
for (i=1; i<=n; ++i)
{
for (j=head[i]; ~j; j=E[j].nxt)
{
int v=E[j].to;
if(belong[i]!=belong[v])
{
++deg[belong[i]];
++deg[belong[v]];
}
}
}
int ans=0;
for (i=1; i<=block; ++i)
if(deg[i]==2)
++ans;
printf("%d\n",(ans+1)>>1);
}
return 0;
}

POJ 3177 Redundant Paths(边双连通的构造)的更多相关文章

  1. POJ 3177 Redundant Paths &lpar;边双连通&plus;缩点&rpar;

    <题目链接> <转载于 >>>  > 题目大意: 有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新 ...

  2. poj 3177 Redundant Paths&lpar;边双连通分量&plus;缩点&rpar;

    链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使得任 ...

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

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

  4. POJ 3177 Redundant Paths 边双(重边)缩点

    分析:边双缩点后,消环变树,然后答案就是所有叶子结点(即度为1的点)相连,为(sum+1)/2; 注:此题有坑,踩踩更健康,普通边双缩短默认没有无向图没有重边,但是这道题是有的 我们看,low数组是我 ...

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

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

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

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

  7. poj 3177 Redundant Paths(tarjan边双连通)

    题目链接:http://poj.org/problem?id=3177 题意:求最少加几条边使得没对点都有至少两条路互通. 题解:边双连通顾名思义,可以先求一下连通块显然连通块里的点都是双连通的,然后 ...

  8. POJ - 3177 Redundant Paths (边双连通缩点)

    题意:在一张图中最少可以添加几条边,使其中任意两点间都有两条不重复的路径(路径中任意一条边都不同). 分析:问题就是最少添加几条边,使其成为边双连通图.可以先将图中所有边双连通分量缩点,之后得到的就是 ...

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

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

随机推荐

  1. Bootstrap栅格布局系统的特点

    栅格布局系统的特点: (1)所有的行必须放在容器中: .container或.container-fluid (2)分为多行(row),一行中平均分为12列(col) (3)网页内容只能放在列(col ...

  2. docker命令小记

    最近工作需要,对docker稍微了解了一下,大部分内容网上都有各种各样的教程.有两个稍微特殊的命令这里记录一下: 1. rename image的命令 从各种私有镜像下载后用docker images ...

  3. Dubbo集成Spring与Zookeeper实例

    >>Dubbo最佳实践 使用Dubbo结合Zookeeper和Spring,是使用比较广泛的一种组合,下面参考官方文档,做个简单的示例,一步步搭建一个使用dubbo结合Zookeeper和 ...

  4. BZOJ 2653 middle

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=2653 题目大意:多组询问,求左右端点在规定范围内移动所能得到的最大中位数. [分析] 求中 ...

  5. 201521123009 《Java程序设计》第13周学习总结

    1. 本周学习总结 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分析返回结果有何不同?为什么会有这样的不同? 从上图来看, ...

  6. Ubuntu文件中文乱码

    如图,该文件在gedit打开中文显示正常 在命令行中用vim打开,显示内容如下: 使用命令进行编码转换 iconv -f gbk -t utf8 ./SogouQ.mini > ./sougou ...

  7. Tomcat他山之石&period;可以攻玉&lpar;一&rpar;Server组件

    Server组件 Server组件作用: 采用观察者模式,又叫源-收听者的设计模式,提供了可以动态添加.删除的监听器,作用是在Server组件的不同生命周期中完成不同的功能.逻辑: Tomcat容器的 ...

  8. TOJ 2778 数据结构练习题&horbar;&horbar;分油问题&lpar;广搜和哈希&rpar;

    描述 设有大小不等的三个无刻度的油桶,分别能盛满x,y,z公升油.初始时,第一个油桶盛满油,第二.三个油桶为空,在某一个油桶上分出targ公升油. 输入 输入包含多组测试数据.每组数据包含一行.分别x ...

  9. FineUI开源版(ASP&period;Net)初学手册

    女朋友鄙视我原创少... 1.下载 进入官方论坛:http://www.fineui.com/bbs/ 要用到下载源代码和空项目下载 http://fineui.codeplex.com/ http: ...

  10. 【c&plus;&plus;】动态绑定

    C++的函数调用默认不使用动态绑定.要触发动态绑定,必须满足两个条件: 只有指定为虚函数的成员函数才能进行动态绑定 必须通过基类类型的引用或指针进行函数调用 因为每个派生类对象中都拥有基类部分,所以可 ...