poj——3177Redundant Paths
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 15272 | Accepted: 6436 |
Description
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
Hint
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
描述
为了从一个F(1=f=5000)放牧场(编号为1…F)到另一个场,Bessie和其他牧群被迫越过腐烂的苹果树附近。奶牛现在已经厌倦了常常被迫走一条特定的道路,并且想建立一些新的道路,这样他们就可以在任何一对田地之间选择至少两条独立的路线。他们目前至少有一对路线之间的每一对领域,并希望有至少两个。当然,当他们从一个领域迁移到另一个领域时,他们只能在正式的道路上旅行。
描述了R的电流设定(F-1 <= R <= 10000),每个连接两个不同领域的路径,确定新的路径的最小数目(每个连接两个领域),必须建立,至少有两个独立的路线,任何对田野之间。如果路径没有相同的路径,即使它们在同一个中间区域访问相同的路径,它们也被认为是独立的。
在同一对字段之间可能有不止一条路径,您还可以构建一个新路径,将同一字段与其他路径连接起来。
输入
第1行:两个空间分隔的整数:f和r
第2行…r + 1:每行包含两个空间分隔的整数,这是某个路径端点的字段。
输出
第1行:一个整数,这是必须建立的新路径的数量。
思路:
我们来考虑一下这道题的做法。
由于题目说,给定一张无向强连通图:判断至少需要加多少条边,使得任意两点之间至少有两条相互‘边独立’的道路,也就是说,至少加多少条边,使得这个图成为一个边双连通图。
什么是双连通图?!
双连通图分为两种:双边连通图与双点连通图,双边连通图就是不存在割边的连通图,点双联通图就是不存在割点的连通图。。(差不多是这样吧。。)
首先我们已经有了一个连通图,我们先判断这个强连通图中是否存在环,这个就与tarjan缩点(有向图时有一点差别了)这个图保证是一个强连通图,如果我们还按以前那样缩点的话,我们只会把这个图个缩成一个点,这样肯定不对啊。。。
我们在缩完点以后把它变成了一个树,这样我们只需要把它的子节点连通起来就好了
也就是说:加入的边的条数一定等于:(叶子节点数+1)/2
我们在这个地方所说的叶子结点为入读或出度为0的点。
但在这里我又想到令一种算法:我们可以先求割边,割边的条数就是我们要求的叶子结点的个数
这样说有人可能会不明白了,那好,我们来看一个图
对于这个图来说,我们先将这个图中存在的环缩成一个点,那它就变成了右图的样子
在这里,我们以环缩点后的点5为根节点 
看这个图的子节点是不是3?!
我们下面再来看看这个图的割边
各大佬应该都能看出来我用红线割掉的边是这个图的割边吧。
他割边的个数是不是也是三?! 也就是说我们上面的结论成立。
那么这个题就变成一个很裸的求割边的板子了。。
好我们来看看代码吧。。
代码:(然而这个思路只能得63分,为什么?!)我们来考虑一下上面的思路的错误所在。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5005
using namespace std;
bool vis[N][N];
,tim;
long long dfn[N],low[N];
],cut_edge[];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-';ch=getchar();}
return x*f;
}
struct Edge
{
int from,next,to;
}edge[];
void add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int tarjan(int now,int pre)
{
;
dfn[now]=low[now]=++tim;
for(int i=head[now];i;i=edge[i].next)
{
^pre)) continue;
int t=edge[i].to;
if(!dfn[t])
{
tarjan(t,i);
low[now]=min(low[now],low[t]);
]=;
}
else low[now]=min(low[now],dfn[t]);
}
}
int main()
{
n=read(),m=read();
;i<=m;i++)
{
x=read(),y=read();
if(!vis[x][y]&&!vis[y][x]) add(x,y),add(y,x);
vis[x][y]=vis[y][x]=true;
}
tarjan(,);
;i<=m;i++)
)
ans++;
///printf("%d\n",ans);
printf()>>);
;
}
样例的图有些不是很明显,我们来看另一个图
对于这样一个图的话:
我们缩点之后把它变成了这样一个图:
这个图割边的条数为:5条,如果按照我们刚刚的结论来看的话,我们要添3条边,然而我们只需要添2条边就可以了
为什么?!
看这个图:
我们只需要在7~9,1~9之间添一条边就可以了。
那么我们还是乖乖地统计入读为2的点的个数吧。。。。。。(其实这个地方应该是度数为一的点,但是这个地方由于我们建的是双向边,每一个点都会统计两次,所以这个地方最小的将会是入读为2)
这个地方肯定有人就会问这样一个问题:为什么这个地方建的是双向边我们还能用tarjan缩点啊?! 缩点的时候所得不是一个强连通分量吗??这样的一个绝对联通的无向图不就是直接把他缩成了一个点了吗?!这样怎么还能这样做?!
想一下这个地方我们在缩点的时候为什么可以用tarjan??
这样缩点主要是归功于这一句话:if(i==(1^pre)) continue; 对,他是用两条边,但是我们这个地方只让他走一条边,这样不就和有向图缩点一样了吗?!
代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 5005
using namespace std;
bool vis[N];
,tim,sum,top;
long long du[N],dfn[N],low[N],stack[N],belong[N];
];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-';ch=getchar();}
return x*f;
}
struct Edge
{
int from,next,to;
}edge[];
void add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int tarjan(int now,int pre)
{
dfn[now]=low[now]=++tim;
stack[++top]=now;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
^pre)) continue;
if(!dfn[t]) tarjan(t,i),low[now]=min(low[now],low[t]);
else low[now]=min(low[now],dfn[t]);
}
if(low[now]==dfn[now])
{
sum++; belong[now]=sum;
for(;stack[top]!=now;top--)
belong[stack[top]]=sum;
top--;
}
}
int main()
{
n=read(),m=read();
;i<=m;i++)
x=read(),y=read(),add(x,y),add(y,x);
tarjan(,);
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to]) du[belong[i]]++,du[belong[edge[j].to]]++;
;i<=n;i++)
) ans++;
printf()>>);
;
}
poj——3177Redundant Paths的更多相关文章
- POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10798 Accepted: 4626 ...
- POJ 1942 Paths on a Grid(组合数)
http://poj.org/problem?id=1942 题意 :在一个n*m的矩形上有n*m个网格,从左下角的网格划到右上角的网格,沿着边画,只能向上或向右走,问有多少条不重复的路 . 思路 : ...
- poj 1924 Paths on a Grid(组合数学)
题目:http://poj.org/problem?id=1942 题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有 ...
- [ACM] POJ 1942 Paths on a Grid (组合)
Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21297 Accepted: 5212 ...
- POJ 1942 Paths on a Grid
// n*m 的格子 从左下角走到右上角的种数// 相当于从 n+m 的步数中选 m 步往上走// C(n+m,m) #include <iostream> #include <st ...
- POJ 3177 Redundant Paths(边双连通的构造)
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13717 Accepted: 5824 ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...
- (poj 3177) Redundant Paths
题目链接 :http://poj.org/problem?id=3177 Description In order to <= F <= ,) grazing fields (which ...
- POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)
POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...
随机推荐
- 洛谷 P1434 滑雪
题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...
- Redhat5 安装序列号及版本说明
为了保证安装的组件和订阅相匹配,红帽企业 Linux 5 需要输入一个安装号.它被用来配置安装程序来提供正确的软件包.安装号码包含在你的订阅里. 如果您没有输入安装号码,只有核心服务器或 Deskto ...
- Android系统固件定制方式
target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...
- Recyclerview设置间距
首先自定义一个RecyclerViewDivider 继承 RecyclerView.ItemDecoration,实现自定义. public class RecyclerViewDivider ex ...
- 使用python编写的简单远程管理软件
因为用户可以选择是否同意被控制,所以并不算是木马. 使用python3.7,spyder,在windows 10 开发. client为控制端,server为被控端. 参考 mygithub http ...
- C++ isalpha、isalnum、islower、isupper用法
1. isalpha isalpha()用来判断一个字符是否为字母,如果是字符则返回非零,否则返回零. cout<<isalpha('a'); //返回非零 cout<<isa ...
- error: version in "./docker-compose.yml" is unsupported
#sudo rm /usr/bin/docker-compose #curl -L https://github.com/docker/compose/releases/download/1.20.0 ...
- Java中的线程安全和非线程安全以及锁的几个知识点
1. 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不会出现数据不一致或者数据污染. 线程不安全就是不提供 ...
- [LOJ] #2363「NOIP2016」愤怒的小鸟
精度卡了一个点,别人自带大常数,我自带大浮点误差qwq. 听了好几遍,一直没动手写一写. f[S]表示S集合中的猪被打死的最少抛物线数,转移时考虑枚举两个点,最低位的0为第一个点,枚举第二个点,构造一 ...
- linux系统日志中出现大量systemd Starting Session ### of user root 解决
这种情况是正常的,不算是一个问题 https://access.redhat.com/solutions/1564823 Environment Red Hat Enterprise Linux 7 ...