BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)
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
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
HINT
.jpg)
Solution
首先可以发现,对于一个双连通分量,我们是不用处理它的
那么如果将所有边双缩成一个点的话,很显然我们可以得到一颗树
那么我们只需要处理叶子节点,在叶子节点间两两连边就好了
答案是(叶子节点数+1)/2
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#define N (5000+100)
using namespace std; struct Edge{int to,next;} edge[N<<];
int n,m,u,v,head[N],num_edge;
int Dfn[N],Low[N],dfs_num;
int bridge_num,ans;
bool Bridge[N],vis[N],dis[N][N]; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Tarjan(int x,int fa)
{
Dfn[x]=Low[x]=++dfs_num;
for (int i=head[x]; i; i=edge[i].next)
if (!Dfn[edge[i].to])
{
Tarjan(edge[i].to,x);
Low[x]=min(Low[x],Low[edge[i].to]);
if (Low[edge[i].to]>Dfn[x])
Bridge[i]=Bridge[(i-^)+]=true;
}
else if (Dfn[edge[i].to]<Dfn[x] && edge[i].to!=fa)
Low[x]=min(Low[x],Dfn[edge[i].to]);
} void Dfs(int x)
{
vis[x]=true;
for (int i=head[x]; i; i=edge[i].next)
{
if(Bridge[i]){bridge_num++; continue;}
if (!vis[edge[i].to]) Dfs(edge[i].to);
}
} int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<=m; ++i)
scanf("%d%d",&u,&v),dis[u][v]=dis[v][u]=true;
for (int i=; i<=n; ++i)
for (int j=i+; j<=n; ++j)
if (dis[i][j])
add(i,j),add(j,i);
for (int i=; i<=n; ++i)
if (!Dfn[i])
Tarjan(i,);
for (int i=; i<=n; ++i)
if (!vis[i])
{
bridge_num=;
Dfs(i);
if (bridge_num==)
ans++;
}
printf("%d",(ans+)/);
}
BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)的更多相关文章
- 【bzoj1718】Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 964 Solve ...
- Redundant Paths 分离的路径【边双连通分量】
Redundant Paths 分离的路径 题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- [Usaco2006 Jan] Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1132 Solv ...
- Redundant Paths 分离的路径
Redundant Paths 分离的路径 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她 ...
- [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
题目传送门 题目描述 为了从F个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分 ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径
Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...
- 【BZOJ】1718: [Usaco2006 Jan] Redundant Paths 分离的路径
[题意]给定无向连通图,要求添加最少的边使全图变成边双连通分量. [算法]Tarjan缩点 [题解]首先边双缩点,得到一棵树(无向无环图). 入度为1的点就是叶子,两个LCA为根的叶子间合并最高效,直 ...
- bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径【tarjan】
首先来分析一下,这是一张无向图,要求没有两条路联通的点对个数 有两条路连通,无向图,也就是说,问题转化为不在一个点双连通分量里的点对个数 tarjan即可,和求scc还不太一样-- #include& ...
随机推荐
- 带OUTPUT的增删改
sql server2005以后引入: 执行的sql语句中加入output可以事实输出处理的内容 go --插入并返回每行的插入值 DECLARE @NewRows TABLE(Id INT ,NAM ...
- 修改TrustedInstaller权限文件
我们在删除文件的时候有可能出现无法删除,并且需要TrustedInstaller提供的权限,所以我们主要修改该文件的拥有者(修改为本机用户)即可.下文举例哦,方法如下: 总结起来:修改文件的所有着为本 ...
- html5使用local storage存储的数据在本地是以何种形式保存的
html5使用local storage存储的数据是如何保存在本地的?(我使用的是chrome浏览器,chrom浏览器是用sqlite来保存本地数据的) Html5 的local storage 是通 ...
- (转)linux shell单引号、双引号及无引号区别
原文:http://blog.csdn.net/woshizhangliang999/article/details/50132265 3.描述linux shell中单引号.双引号及不加引号的简单区 ...
- js判断文件是否存在的方法
在做电力监控项目的时候,有一个需求就是左右布局的框架,点击左边的图形文件地址,然后去文件夹中找到文件,再在右边出现对应的图形文件,但是有些文件可能是配置的时候有问题,找不到文件,所以js需要判断,以下 ...
- js判断触摸方向
$("body").on("touchstart", function(e) { e.preventDefault(); startX = e.original ...
- Prometheus TSDB分析
Prometheus TSDB分析 概述 Prometheus是著名开源监控项目,其监控任务调度给具体的服务器,该服务器到目标上抓取监控数据,然后保存在本地的TSDB中.自定义强大的PromQL语言查 ...
- 【转载】Web 研发模式演变
一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 3-5 人搞定所有开发.页面由 JSP.PHP 等工程师在服务端生成,浏览器负责展现.基本上是服务端给什 ...
- Unity手册-Unity概述
Unity概述 Unity是一个强大引擎,他自带的多种工具可以满足你多种需求. 这种编辑器是直观的可定制的,让你的工作更大的自由. 原文 Unity is a powerful engine with ...
- C#请求http post和get
首先先要感谢博主小伟地方提供的博客,让我解决了问题. 同样是先提问题,我们要请求http干什么? 通过请求http,传入我的参数,我希望能够获取到项目里面的某些数据,比如这里,我们需要得到SceneL ...