【Redundant Paths】【无向图】【双连通分量】【缩点】

2024-08-31 16:38

本文主要是介绍【Redundant Paths】【无向图】【双连通分量】【缩点】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Redundant Paths

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

In order to get from one of the  F(1F5,000)  grazing fields (which are numbered  1F ) 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(F1R10,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  2R+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 and output

Sample Input Sample Output
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
2

Hint

Explanation of the sample:

One visualization of the paths is:

title

Building new paths from  1  to  6  and from  4  to  7  satisfies the conditions.

title

Check some of the routes:

  1. 12:1>2  and  1>6>5>2
  2. 14:1>2>3>4  and  1>6>5>4
  3. 37: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




#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_backconst int MAXN = 50010;
const int MAXM = 100010;
int n,m;
struct Edge
{int to,next,id;
}edge[MAXM];
int vis[MAXN];
int low[MAXN];
int myst[MAXN];
int inst[MAXN];
int myblock[MAXN];
int in_de[MAXN];
int out_de[MAXN];
int H[MAXN];
int top;
int len;
int times;
int sccnum;void addedge(int from,int to,int id)
{edge[len].id = id;edge[len].to = to;edge[len].next = H[from];H[from] = len ++;
}void tarjan(int u,int x)
{myst[top++] = u;low[u] = vis[u] = ++times;inst[u] = true;for(int i=H[u];~i;i=edge[i].next){int v = edge[i].to;if(x == edge[i].id) continue;if(vis[v] == -1){tarjan(v,edge[i].id);low[u] = min(low[u],low[v]);}else {low[u] = min(low[u],vis[v]);}}int t;if(low[u] == vis[u]){sccnum ++;do{t = myst[--top];inst[t] = false;myblock[t] = sccnum;} while(t != u);}
}void solve(int n)
{for (int i = 1; i <= n; ++i){if(vis[i] == -1)tarjan(i,-1);}for (int u = 1; u <= n; ++u){for (int j = H[u]; ~j; j = edge[j].next){int v = edge[j].to;if(myblock[u] != myblock[v]){out_de[myblock[v]] ++;out_de[myblock[u]] ++;}}}int cnt = 0;for(int i=1;i<=sccnum;i++){if(out_de[i]/2 == 1){++cnt;}}printf("%d\n", (cnt + 1) >> 1);}
int main()
{freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m) != EOF){len = 0;times = 0;top = 0;sccnum = 0;memset(vis,-1,sizeof(vis));memset(H,-1,sizeof(H));memset(inst,0,sizeof(inst));memset(myst,0,sizeof(myst));memset(myblock,0,sizeof(myblock));memset(in_de,0,sizeof(in_de));memset(out_de,0,sizeof(out_de));for(int i=1;i<=m;i++){int u,v;scanf("%d%d",&u,&v);addedge(u,v,i);addedge(v,u,i);}solve(n);}}


这篇关于【Redundant Paths】【无向图】【双连通分量】【缩点】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124433

相关文章

poj 2914 无向图的最小割

题意: 求无向图的最小割。 解析: 点击打开链接 代码: #pragma comment(linker, "/STACK:1677721600")#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <cstd

线性因子模型 - 独立分量分析(ICA)篇

序言 线性因子模型是数据分析与机器学习中的一类重要模型,它们通过引入潜变量( latent variables \text{latent variables} latent variables)来更好地表征数据。其中,独立分量分析( ICA \text{ICA} ICA)作为线性因子模型的一种,以其独特的视角和广泛的应用领域而备受关注。 ICA \text{ICA} ICA旨在将观察到的复杂信号

像素间的关系(邻接、连通、区域、边界、距离定义)

文章目录 像素的相邻像素4邻域D邻域8邻域 邻接、连通、区域和边界邻接类型连通区域边界 距离测度欧氏距离城市街区距离(city-block distance)棋盘距离(chessboard distance) 参考 像素的相邻像素 4邻域 坐标 ( x , y ) (x,y) (x,y)处的像素 p p p有2个水平的相邻像素和2个垂直的相邻像素,它们的坐标是: ( x

大数据Java基础-JAVA IO 9】java IO流 (九) Path、Paths、Files的使用

1.NIO的使用说明: >Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO AP。 >NIO与原来的IO同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。 >NIO将以更加高效的方式进行文件的读写操作。 >随着 JDK 7 的发布,Java对N

Avoided redundant navigation to current location: 路由相同报错

vue-router有一个内置保护机制,它会阻止不必要的重复导航,以提高性能并避免不必要的计算。 具体来说,错误信息中的就是试图访问的路径时,应用程序已经在当前这个路径上。因此,vue-router检测到了这个重复的导航请求,就发出了警告。 通常情况下,这种警告并不需要特别处理,因为这只是一个优化措施,防止不必要的导航。但是如果你频繁遇到这种情况,可能需要检查触发导航的部分代码逻辑是否有必要进

OpenCV结构分析与形状描述符(6)带统计的连通组件计算函数connectedComponentsWithStats()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 connectedComponentsWithStats 函数计算布尔图像的连通组件标记图像,并为每个标记产生统计信息。 该函数接受一个具有4或8连通性的二值图像,并返回 N,即标签总数(标签范围为 [0, N-1],其中 0 代表背景标签)

最小连通网络

使用网络中的n-1条边来连接网络中的n个顶点 不产生回路 各边上的权值总和达到最小 prim算法:针对顶点展开,适合边的数量较多的情况 kruskal算法:针对边展开的,适合边的数量较少的情况

【HDU】2242 考研路茫茫——空调教室 双连通分量+树型DP

考研路茫茫——空调教室 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1978    Accepted Submission(s): 576 Problem Description 众所周知,HDU的考研教室是没

【HDU】3861 The King’s Problem 强连通缩点+有向图最小路径覆盖

传送门:【HDU】3861 The King’s Problem 题目分析:首先强连通缩点,因为形成一个环的王国肯定在一条路径中,这样才能保证拆的少。 然后缩点后就是DAG图了,由于题目要求的是最小路径覆盖,那么二分匹配即可。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>#includ

【Live Archive】6393 Self-Assembly【强连通】

传送门:【Live Archive】6393 Self-Assembly 题目分析: 假设我们只用到向上或者向右的块,这样我们只要找到一个回路使得某个块可以和第一个块一样,那么我们就相当于找到了一个循环,这样就可以无限循环了。 但是我们要怎样去找这么一个环?考虑到必须是对应字母 X+,X− X^+,X^-才能建边,然后一个环中一定是多个一对一对的这样的对应字母组成的。 可以发现块的数量那么