POJ3177 Redundant Paths

POJ3177 Redundant Paths

文章目录

    • Description
    • 题意:
    • 题解:
    • 代码:

Time Limit: 1000MS		Memory Limit: 65536K
Total Submissions: 21945		Accepted: 9056

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

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample:

One visualization of the paths is: 1 2 3 ±–±--+
| |
| | 6 ±–±--+ 4
/ 5
/
/ 7 + Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 1 2 3 ±–±--+ : | | : | |
6 ±–±--+ 4
/ 5 :
/ :
/ : 7 + - - - - Check some of the routes: 1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 3 – 7: 3
–> 4 –> 7 and 3 –> 2 –> 5 –> 7 Every pair of fields is, in fact,
connected by two routes.

It’s possible that adding some other path will also solve the problem
(like one from 6 to 7). Adding two paths, however, is the minimum.

题意:

n个点,m个边,问再添加多少边可以使得任意两点有两条路径(且不可重复)

题解:

如果任意两点至少存在两条边不重复路径,则称该图为边双连通的。
我们可以用Tarjan来求出每个边双联通分量,对于同一个边双连通分量的点之间都至少有两条路径,但是不同之间只会有一条路径。
所以我们对每个边双连通分量进行缩点,就可以得到一个树,要使这个无根数变成边双联通图,我们要先看看树中谁需要连线,没错就是叶子节点,如果我们将所有叶子节点都消灭那不就行了,所以至少要添加(叶子节点数+1)/2条边
可以结合图分析一下(图中为题目给的样例)
在这里插入图片描述

代码:

代码参考

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 5e3+10;struct Edge
{int to, next;bool cut;
}edge[MAXN<<2];
int head[MAXN], tot;int index, dfn[MAXN], low[MAXN];
int block, belong[MAXN];
int top, Stack[MAXN], instack[MAXN];
int degree[MAXN];void addedge(int u, int v)
{edge[tot].to = v;edge[tot].cut = false;edge[tot].next = head[u];head[u] = tot++;
}void Tarjan(int u, int pre)
{low[u] = dfn[u] = ++index;Stack[top++] = u;instack[u] = true;for(int i = head[u]; i!=-1; i = edge[i].next){int v = edge[i].to;if(v==pre) continue;if(!dfn[v]){Tarjan(v, u);low[u] = min(low[u], low[v]);if(low[v]>dfn[u])//当前边i所连接的点为叶子节点 {edge[i].cut = true;edge[i^1].cut = true;//标记两次 }}else if(instack[v])low[u] = min(low[u], dfn[v]);}if(low[u]==dfn[u]){block++;int v;do{v = Stack[--top];instack[v] = false;belong[v] = block;}while(v!=u);}
}void init()
{tot = 0;memset(head,-1,sizeof(head));index = 0;memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));block = top = 0;memset(instack,0,sizeof(instack));memset(degree,0,sizeof(degree));
}int main()
{int n, m;while(scanf("%d%d",&n,&m)!=EOF){init();for(int i = 1; i<=m; i++){int u, v;scanf("%d%d",&u,&v);addedge(u, v);addedge(v,u);}Tarjan(1, 1);for(int u = 1; u<=n; u++)for(int i = head[u]; i!=-1; i = edge[i].next)if(edge[i].cut) //不需要两端都加,因为一条割边被标记了两次。一次正好对应一个端点。degree[belong[u]]++;//求各点的度数 int leaf = 0;for(int i = 1; i<=block; i++)if(degree[i]==1) leaf++;printf("%d\n", (leaf+1)/2);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/319988.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【最小生成树】路线规划(nowcoder 217603)

路线规划 nowcoder 217603 题目大意 给一个无向连通图&#xff0c;问你在经过的边最少的前提下&#xff0c;从1走过所有点&#xff0c;再走回1的最短距离 样例#1 输入样例#1 5 5 5 4 3 4 3 5 2 3 7 1 2 4 2 4 1输出样例#1 26样例解释#1 最少时间的路径: 1 →2 →…

计算几何学习小记

文章目录前言正题平面运算加减乘积常见问题直线/线段规范交点求垂线/点问题判断点在多边形的内/外求两个圆的交点前言 因为懒得画图理解计算几何所以要来这里鼓励一下自己 以后新学的应该也会写在这里。就当我是水博客 应该都是二维的计算几何&#xff0c;三维的有生之年再学 …

[XSY3343] 程序锁(DP)

XSY3343 先考虑如何判定一个填好的序列会不会gg&#xff1a; 若∃p,q,使s′[p1]t′[q1]−1\exist p,q,使s[p1]t[q1]-1∃p,q,使s′[p1]t′[q1]−1且∑i1ps′[i]∑j1qt′[j]≤0\sum_{i1}^{p}s[i]\sum_{j1}^{q}t[j]\leq 0∑i1p​s′[i]∑j1q​t′[j]≤0&#xff0c;则这个序列必g…

Asp.Net Core SignalR 用泛型Hub优雅的调用前端方法及传参

继续学习最近一直在使用Asp.Net Core SignalR(下面成SignalR Core)为小程序提供websocket支持,前端时间也发了一个学习笔记&#xff0c;在使用过程中稍微看了下它的源码,不得不说微软现在真的强大,很多事情都帮你考虑到了,比如使用Redis,使用Redis后,你的websocket就支持横向扩…

Network POJ-3694

Network POJ-3694 文章目录Description题意&#xff1a;样例分析&#xff1a;题解&#xff1a;代码&#xff1a;Description A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of com…

【树状数组】递增子序列(金牌导航 数据结构优化DP-1)

递增子序列 金牌导航 数据结构优化DP-1 题目大意 给出一个序列&#xff0c;让你求长度为m的单调递增子序列的个数 输入样例 3 2 1 1 2 7 3 1 7 3 5 9 4 8输出样例 2 12数据范围 1⩽n⩽104,1⩽m⩽100,0⩽ai⩽9876543211\leqslant n \leqslant 10^4,1\leqslant m \leqslant…

使用.NET Core 2.1的Azure WebJobs

WebJobs不是Azure和.NET中的新事物。 Visual Studio 2017中甚至还有一个默认的Azure WebJob模板&#xff0c;用于完整的.NET Framework。 但是&#xff0c;Visual Studio中以某种方式遗漏了.NET Core中WebJobs的类似模板。 在这篇文章中&#xff0c;我使用的是.NET Core 2.1来创…

P3265-[JLOI2015]装备购买【线性基,拟阵贪心】

正题 题目链接:https://www.luogu.com.cn/problem/P3265 题目大意 给出nnn个有权值的mmm元组。求最大独立集&#xff0c;即一个最大的集合且内部元素线性无关。且在集合最大的情况下权值和最小 通俗的说就是没有任何一个元素内被其他元素的倍数和表示。 解题思路 我们考虑线…

【决策单调性】玩具装箱(金牌导航 决策单调性优化DP-1)

玩具装箱 金牌导航 决策单调性优化DP-1 题目大意 给出若干个物品&#xff0c;把iii到jjj个物品装在一起的长度lj−i∑kijaklj-i\sum_{ki}^{j}a_klj−i∑kij​ak​&#xff08;物品必须是连续的&#xff09;&#xff0c;其代价为(lL)2(l L)^2(lL)2&#xff08;L为给出的常数…

HDU4612 Warm up

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 11184 Accepted Submission(s): 2573HDU4612 Warm up 文章目录Problem Description题意&#xff1a;题解&#xff1a;代码&#xff1a;Problem Description N …

[NOIP2016 提高组] 天天爱跑步(树上差分)

如果没有时间的限制&#xff0c;这题就是对每个点iii&#xff0c;求经过iii的路径数&#xff0c;用树上差分解决即可&#xff1a; 枚举路径x→y{x\to y\{x→y{ a[x]1;a[y]1;a[x]1;a[y]1;a[x]1;a[y]1; a[lca(x,y)]−1;a[fa[lca(x,y)]]−2;a[lca(x,y)]-1;a[fa[lca(x,y)]]-2;a[lc…

.NET Core中的CSV解析库

感谢本篇首先特别感谢从此启程兄的《.NetCore外国一些高质量博客分享》, 发现很多国外的.NET Core技术博客资源, 我会不定期从中选择一些有意思的文章翻译总结一下。.NET Core中的CSV解析库本篇博客来源于.NET Core Totorials的《CSV Parsing In .NET Core》。背景介绍对于初级…

【manacher】双倍回文(金牌导航 manacher-2/luogu 4287)

双倍回文 金牌导航 manacher-2 luogu 4287 题目大意 设串为x&#xff0c;将其取反为x’&#xff0c;定义双倍回文为形如xx’xx’的串 现在给你一个字符串&#xff0c;让你求最大双倍回文子串 输入样例 16 ggabaabaabaaball输出样例 12数据范围 N⩽105N\leqslant 10^5N⩽…

P6178-[模板]Matrix-Tree 定理

正题 题目链接:https://www.luogu.com.cn/problem/P6178 题目大意 给出一个nnn个点mmm条边的无向/有向图。 求所有的生成树/以1为根的外向生成树的权值乘积和。 解题思路 矩阵AAA的行列式表示为det(A)det(A)det(A)&#xff0c;定义为 det(A)∑P(−1)μ(P)∏i1nAi,pidet(A)\…

可达性

来源&#xff1a;牛客网 文章目录题目描述题解&#xff1a;代码&#xff1a;时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld题目描述 给出一个 0 ≤ N ≤ 105 点数、0 ≤ M ≤ 105 边…

[集训队作业2018]小Z的礼物(min-max容斥,插头dp)

传送门 这种求 “取到所有物品的期望时间” 的题一般都用 min−maxmin-maxmin−max容斥 解决&#xff1a; 设t(i,j)t(i,j)t(i,j)为取到格子(i,j)(i,j)(i,j)的期望时间&#xff0c;集合S∪c(i,j)′∗′{t(i,j)}S\cup_{c(i,j)*}\{t(i,j)\}S∪c(i,j)′∗′​{t(i,j)} 那么根据min−…

为什么要使用Entity Framework

本文介绍从DDD(Domain-Driven Design[领域驱动设计])的角度来说说为什么要使用Entity Framework(以下都会简称为EF)&#xff0c;同时也看出类似Drapper之类的简陋ORM不足的地方。设想业务都是大家知晓的权限管理&#xff0c;实体类如下。读到这里&#xff0c;请先思考一下&…

【Splay】波动值之和(金牌导航 Splay-1)

波动值之和 金牌导航 Splay-1 题目大意 给出一个数列&#xff0c;求∑i1nminj1i−1∣ai−aj∣\sum_{i1}^{n}min_{j1}^{i-1}|a_i-a_j|∑i1n​minj1i−1​∣ai​−aj​∣ 输入样例 6 5 1 2 5 4 6输出样例 12样例解释 5∣1−5∣∣2−1∣∣5−5∣∣4−5∣∣6−5∣541011125|1…

P4336-[SHOI2016]黑暗前的幻想乡【矩阵树定理,容斥】

正题 题目链接:https://www.luogu.com.cn/problem/P4336 题目大意 nnn个点&#xff0c;n−1n-1n−1个边集&#xff0c;求有多少种方案使得每个边集中恰好选出一条边使得这nnn个点连成一棵树。 解题思路 我们需要利用好n−1n-1n−1个边集这个性质&#xff0c;因为nnn很小&…

Tarjan算法

Tarjan算法可以应用在求解 强连通分量&#xff0c;缩点&#xff0c;桥&#xff0c;割点&#xff0c;双连通分量&#xff0c;LCA等 关于文章目录强连通分量代码题目tarjan求割点割点概念流程代码&#xff1a;求无向图的割边&#xff0f;桥理解&#xff1a;代码&#xff1a;强连通…