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& ...
随机推荐
- Spring 和整个体系 @Value注解 配合属性文件.property
未来学习方向 重要思路 学的时候看官方文档,系统地学,比如学Spring Boot ,但真正使用的时候,有比自动化(条件化)配置,约定即配置 更好的方法(这个可读性不强,对电脑来说它执行的代码一样 ...
- bind-named
main-book: http://www.zytrax.com/books/dns resolv.conf: http://dns-learning.twnic.net.tw/bind/intro4 ...
- 踩坑--http返回码之302状态码
项目介绍:springboot+shiro+maven 业务需求:拦截一切不登录的盗链URL,除了问卷调查,可以给任意用户填写和提交意外. 问题重现:表单提交过程中返回302状态码,我就觉得很奇怪.在 ...
- (转)跟着老男孩一步步学习Shell高级编程实战
原文:http://oldboy.blog.51cto.com/2561410/1264627/ 跟着老男孩一步步学习Shell高级编程实战 原创作品,允许转载,转载时请务必以超链接形式标明文章 原 ...
- Linux的应用层到底层驱动的调用过程
应用层如何内核.md 1.从应用层打通内核:驱动 首先来说是设备号的引入,我们通过 cat/proc/kallsyms |grep mydevice 可以查看设备号,当然我们也是可以自己创建设备号,这 ...
- React.js 小书 Lesson15 - 实战分析:评论功能(二)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson15 转载请注明出处,保留原文链接和作者信息. 上一节我们构建了基本的代码框架,现在开始完善其 ...
- 命令行编译java项目
命令行编译java项目 项目名: testproj 目录 src -> cn -> busix -> test bin lib 编译项目 cd testproj javac -d . ...
- hdu 5242——Game——————【树链剖分思想】
Game Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- wex5新增数据库
首先是要打开Wex5 (这是废话,下面进入正题..) 1.第一步,找到界面中的 ”窗口” 点击打开,你会看到一个 “ 首选项 ”按照流程也要打开 (囧),,,,,,,,看图为重 2.当你打开了 “ ...
- 08.StreamReader和StreamWrite的学习
StreamReader和StreamWrite是用来操作字符的 namespace _21.对StreamReader和StreamWriter的学习 { class Program { stati ...