原题链接

题意实际上就是让你添加尽量少的边,使得每个点都在至少一个环上。

显然对于在一个边双连通分量里的点已经满足要求,所以可以用\(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的更多相关文章

  1. 洛谷 P2860 [USACO06JAN]冗余路径Redundant Paths 解题报告

    P2860 [USACO06JAN]冗余路径Redundant Paths 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们 ...

  2. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths(tarjan求边双联通分量)

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  3. 洛谷P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  4. 缩点【洛谷P2860】 [USACO06JAN]冗余路径Redundant Paths

    P2860 [USACO06JAN]冗余路径Redundant Paths 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了 ...

  5. Luogu2860 [USACO06JAN]冗余路径Redundant Paths

    Luogu2860 [USACO06JAN]冗余路径Redundant Paths 给定一个连通无向图,求至少加多少条边才能使得原图变为边双连通分量 \(1\leq n\leq5000,\ n-1\l ...

  6. luogu P2860 [USACO06JAN]冗余路径Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1- ...

  7. [USACO06JAN] 冗余路径 Redundant Paths

    题目描述 In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1. ...

  8. 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. ...

  9. 【luogu P2860 [USACO06JAN]冗余路径Redundant Paths】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2860 考虑在无向图上缩点. 运用到边双.桥的知识. 缩点后统计度为1的点. 度为1是有一条路径,度为2是有两 ...

随机推荐

  1. 学JS的心路历程-for of和for in

    我们在刚入门JS时候,说到要跑出数组的每个值肯定都是这样子: var arr = [1,2,3,4,5,6]: for(let i = 0:i < arr.length:i++){ consol ...

  2. Applese走方格-dfs

    链接:https://ac.nowcoder.com/acm/contest/330/B来源:牛客网 题目描述 精通程序设计的 Applese 又写了一个游戏. 在这个游戏中,它位于一个 n 行 m ...

  3. JAVA语言 第二周

    放假第二周了,时间真快! 上一周配置好了环境变量,这一周就可以做一些测试了.对不同的内容分类进行了测试,包括写入.输出.变量·······还有很多.对于开学的试卷,在第一部分做的还行,第二部分就没什么 ...

  4. Kotlin系列之序列(Sequences)源码完全解析

    Kotlin系列之序列(Sequences)源码完全解析 2018年06月05日 22:04:50 mikyou 阅读数:179 标签: Kotlin序列(sequence)源码解析Androidja ...

  5. spring .cloud ------------java.lang.RuntimeException: com.netflix.client.ClientException,Caused by: java.lang.IllegalArgumentException: MIME type may not contain reserved characters

    1.问题的发生 Feign在默认情况下使用的是JDK原生的URLConnection发送HTTP请求,没有连接池,但是对每个地址会保持一个长连接,即利用HTTP的persistence connect ...

  6. 微信小程序商品筛选,侧方弹出动画选择页面

    https://blog.csdn.net/qq_36538012/article/details/85110641

  7. spring init method destroy method

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  8. mysql5.7.10开启慢查询

    MySql提供慢SQL日志的功能,能够记录下响应时间超过一定阈值的SQL查询,以便于我们定位糟糕的查询语句. 首先,查询当前mysql数据库是否开启了慢查询日志功能: show VARIABLES l ...

  9. 读取excel表格以及生成自动化报告

    数据库读取 标签(空格分隔): 数据库读取 读excel数据xlrd 当登录的账号有多个的时候,我们一般用excel存放测试数据,本节课介绍,python读取excel方法,并保存为字典格式. 1.先 ...

  10. Failed to start LSB: Bring up/down networking 问题

    Failed to start LSB: Bring up/down networking 问题   1.执行 service network restart 出现以下错误 Restarting ne ...