洛谷2860 [USACO06JAN]冗余路径Redundant Paths
原题链接
题意实际上就是让你添加尽量少的边,使得每个点都在至少一个环上。
显然对于在一个边双连通分量里的点已经满足要求,所以可以用\(tarjan\)找边双并缩点。
对于缩点后的树,先讲下我自己的弱鸡做法,每次找直径,因为将直径改为环显然使得新添的边贡献最大,这样贪心的连下去,直到所有点满足要求为止。
而实际上有一个简单结论,该题答案就是缩点后树的叶子节点个数除\(2\)(向上取整)。
代码是用的我自己的弱鸡做法。
#include<cstdio>
using namespace std;
const int N = 5010;
const int M = 2e4 + 10;
int fi[N], di[M], ne[M], cfi[N], cdi[M], cne[M], dfn[N], low[N], bl[N], pre[N], l = 1, lc, ti, DCC, ma, ma_id;
bool bg[M], v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline void add_c(int x, int y)
{
cdi[++lc] = y;
cne[lc] = cfi[x];
cfi[x] = lc;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
void tarjan(int x, int la)
{
int i, y;
dfn[x] = low[x] = ++ti;
for (i = fi[x]; i; i = ne[i])
if (!dfn[y = di[i]])
{
tarjan(y, i);
low[x] = minn(low[x], low[y]);
if (low[y] > dfn[x])
bg[i] = bg[i ^ 1] = 1;
}
else
if (i ^ la ^ 1)
low[x] = minn(low[x], dfn[y]);
}
void dfs(int x)
{
int i, y;
bl[x] = DCC;
for (i = fi[x]; i; i = ne[i])
if (!bl[y = di[i]] && !bg[i])
dfs(y);
}
void dfs_2(int x, int fa, int d)
{
int i, y;
if (ma < d)
{
ma = d;
ma_id = x;
}
pre[x] = fa;
for (i = cfi[x]; i; i = cne[i])
if ((y = cdi[i]) ^ fa)
dfs_2(y, x, v[x] && v[y] ? d : d + 1);
}
int main()
{
int i, n, m, x, y, s = 0;
n = re();
m = re();
for (i = 1; i <= m; i++)
{
x = re();
y = re();
add(x, y);
add(y, x);
}
for (i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i, 0);
for (i = 1; i <= n; i++)
if (!bl[i])
{
DCC++;
dfs(i);
}
for (i = 2; i <= l; i += 2)
{
x = bl[di[i ^ 1]];
y = bl[di[i]];
if (x ^ y)
{
add_c(x, y);
add_c(y, x);
}
}
while (DCC > 1)
{
ma = ma_id = 0;
dfs_2(1, 0, 0);
ma = 0;
dfs_2(ma_id, 0, 0);
for (i = ma_id; i; i = pre[i])
if (!v[i])
{
v[i] = 1;
DCC--;
}
s++;
}
printf("%d", DCC ^ 1 ? s : s ? s + 1 : s);
return 0;
}
洛谷2860 [USACO06JAN]冗余路径Redundant Paths的更多相关文章
- 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告
P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...
- 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths
P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...
- Luogu2860 [USACO06JAN]冗余路径Redundant Paths
Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...
- luogu P2860 [USACO06JAN]冗余路径Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...
- [USACO06JAN] 冗余路径 Redundant Paths
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- luogu P2860 [USACO06JAN]冗余路径Redundant Paths |Tarjan
题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...
- 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解
题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...
随机推荐
- ADO.Net 数据库 删除
删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...
- HTML 设置字体
HTML,CSS,font-family:中文字体的英文名称 (宋体 微软雅黑) 宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsof ...
- Python教程_简介2
人生苦短,我用Python--Life is short,you need Python. https://www.bilibili.com/video/av14184325/?p=101 Pytho ...
- git查看某个文件修改历史
[git查看某个文件修改历史] 1.使用git命令 git whatchanged charge.lua 显示某个文件的每个版本提交信息:提交日期,提交人员,版本号,提交备注(没有修改细节) git ...
- Spring之jdbcTemplate实现orm
public List<AppUser> getAppUser(AppUser appUser) { String sql = "select * from appuser a ...
- 四:python 对象类型详解一:数字(下)
一:位操作 除了一般的数学运算,python也支持c语言中的大多数数学表达式.这包括那些把整数当作二进制位串对待的操作.例如,还可以实现位移及布尔操作: >>> x = 1 > ...
- CentOS 下搭建Tomcat
1.下载tomcat软件包 wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.ta ...
- Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等
Selenium 如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...
- NoHtml
private string NoHtml(string Htmlstring) { if (string.IsNullOrWhiteSpace(Htmlstring)) return string. ...
- String int 相互转换
String->int: int i = Integer.parseInt(s) int->String: String s = Integer.toString(i)