[Usaco2006 Jan] Redundant Paths 分离的路径
1718: [Usaco2006 Jan] Redundant Paths 分离的路径
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 1132 Solved: 590
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
HINT
.jpg)
Source
[Submit][Status][Discuss]
HOME
Back
题解
题意要求的是加最少的边让连通图变成边双。
翻了好久才有一篇讲解了构造的题解。
首先tarjan把边双都缩成一个点。如果个数只有1或2,答案显然。
然后是一棵树的情况,选择一个度数不为1的点(易证存在)做根节点。 如何把这棵树加最少的边变成边双呢?
统计出树中度为1的节点的个数,即为叶节点的个数,记为leaf。则至少在树上添加(leaf+1)/2条边,就能使树达到边二连通,所以至少添加的边数就是(leaf+1)/2。具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样就可以再缩一次点。然后再找两个最近公共祖先最远的两个叶节点,这样一对一对找完,恰好是(leaf+1)/2次,把所有点收缩到了一起。
为什么是正确的呢?考虑我们的目的,是让这棵树变成一个点。而如果这些叶节点都被上述那种找最长链的方式缩成一个点,那么整棵树也应该被缩成一个点了。还是感性理解啊……
#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=5e3+1,M=2e4+2;
int n,m,dfn[N],low[N],num;
int head[N],edge[M],next[M],tot=1;
int st[N],top;
int dcc[N],deg[N],cnt,ans;
bool v[N];
il void add(int x,int y){
edge[++tot]=y,next[tot]=head[x],head[x]=tot;
}
void tarjan(int x){
dfn[x]=low[x]=++num;
for(int i=head[x];i;i=next[i]){
if(v[i]) continue;
int y=edge[i];
if(!dfn[y]){
st[++top]=y;
v[i]=v[i^1]=1;
tarjan(y);
v[i]=v[i^1]=0;
low[x]=min(low[x],low[y]);
}
else low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
++cnt;
while(top){
dcc[st[top]]=cnt;
if(st[top--]==x) break;
}
}
}
int main(){
read(n),read(m);
for(int x,y;m--;){
read(x),read(y);
add(x,y),add(y,x);
}
st[top=1]=1,tarjan(1);
for(int i=2;i<=tot;i+=2){
int x=edge[i],y=edge[i^1];
if(dcc[x]==dcc[y]) continue;
++deg[dcc[x]],++deg[dcc[y]];
}
for(int i=1;i<=n;++i) ans+=deg[i]==1;
printf("%d\n",(ans+1)/2);
return 0;
}
[Usaco2006 Jan] Redundant Paths 分离的路径的更多相关文章
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...
- [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
题目传送门 题目描述 为了从F个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分 ...
- BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径
Description 给出一个无向图,求将他构造成双连通图所需加的最少边数. Sol Tarjan求割边+缩点. 求出割边,然后缩点. 将双连通分量缩成一个点,然后重建图,建出来的就是一棵树,因为每 ...
- BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径
给你一个无向图,问至少加几条边可以使整个图变成一个双联通分量 简单图论练习= = 先缩点,ans = (度数为1的点的个数) / 2 这不是很好想的么QAQ 然后注意位运算的优先级啊魂淡!!!你个sb ...
- BZOJ1718: [Usaco2006 Jan] Redundant Paths 分离的路径【边双模板】【傻逼题】
LINK 经典傻逼套路 就是把所有边双缩点之后叶子节点的个数 //Author: dream_maker #include<bits/stdc++.h> using namespace s ...
- 【BZOJ】1718: [Usaco2006 Jan] Redundant Paths 分离的路径
[题意]给定无向连通图,要求添加最少的边使全图变成边双连通分量. [算法]Tarjan缩点 [题解]首先边双缩点,得到一棵树(无向无环图). 入度为1的点就是叶子,两个LCA为根的叶子间合并最高效,直 ...
- bzoj 1718: [Usaco2006 Jan] Redundant Paths 分离的路径【tarjan】
首先来分析一下,这是一张无向图,要求没有两条路联通的点对个数 有两条路连通,无向图,也就是说,问题转化为不在一个点双连通分量里的点对个数 tarjan即可,和求scc还不太一样-- #include& ...
- 【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 ...
随机推荐
- Oracle spatial与arcsde 的关系
有一些同事问过我下面这些问题: 我们用了oracle spatial sdo_geometry,是不是没用arcsde? 我们到底是使用oracle spatial还是arcsde,有点懵! 执行了c ...
- QT QML与C++混搭
"那些杀不死我的必使我更加强大"----尼采 QML与C++混合编程就是使用QML高效便捷地构建UI,而C++则用来实现业务逻辑和复杂算法. ML访问C++Qt集成了QML引擎和Q ...
- LeetCode 238. 除自身以外数组的乘积(Product of Array Except Self)
238. 除自身以外数组的乘积 238. Product of Array Except Self 题目描述 LeetCode LeetCode238. Product of Array Except ...
- Oracle 11g 总结篇2
第一部分: 字段名的别名用""括起来,如:last_name as "姓名". 去除重复:在投影的字段名前加上 distinct 就可以了. 比如:select ...
- TZOJ5255: C++实验:三角形面积
#include<iostream> #include<iomanip> #include<math.h> #include<cmath> using ...
- 【LEETCODE】58、数组分类,适中级别,题目:238、78、287
package y2019.Algorithm.array.medium; import java.util.Arrays; /** * @ProjectName: cutter-point * @P ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- (转)三大WEB服务器对比分析(apache ,lighttpd,nginx)
ref : https://www.iteye.com/blog/hai0378-1860220 一.软件介绍(apache lighttpd nginx) 1. lighttpd Light ...
- mysql_重置密码
# 修改编码 ```pythonshow variables like '%char%'; #查看当前使用的编码 1.打开配置文件: vim /etc/mysql/my.cnf 2.在[client] ...
- SpringBoot打成jar包后无法读取resources资源文件
在项目中做了一个支付功能, 需要引入第三方渠道的配置文件config.xml用来初始化文件证书, 将配置文件 config.xml 放到 resources 资源目录下. 本地开发环境下能正常读取该文 ...