首页 > 其他分享 >Shortest Cycle in a Graph

Shortest Cycle in a Graph

时间:2023-04-14 18:01:28浏览次数:42  
标签:dist Graph vertex ret leq edges graph Shortest Cycle

Shortest Cycle in a Graph

There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1. The edges in the graph are represented by a given 2D integer array edges, where edges[i] = [ui, vi] denotes an edge between vertex ui and vertex vi . Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.

Return the length of the shortest cycle in the graph. If no cycle exists, return -1.

A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.

Example 1:

Input: n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
Output: 3
Explanation: The cycle with the smallest length is : 0 -> 1 -> 2 -> 0 

Example 2:

Input: n = 4, edges = [[0,1],[0,2]]
Output: -1
Explanation: There are no cycles in this graph.

Constraints:

  • $2 \leq n \leq 1000$
  • $1 \leq \text{edges.length} \leq 1000$
  • $\text{edges}[i]\text{.length} \ \mathrm{==} \ 2$
  • $0 \leq u_i, v_i < n$
  • $u_i \ne v_i$
  • There are no repeated edges.

 

解题思路

  求最小环的模板题,当时比赛的时候没了解过就没做出来。解决最小环问题可以参考该博客:无向图的最小环问题

  可以发现题目中的$n$最大可以取到$1000$,因此不可以用Floyd的做法。又发现每一条边的权重都为$1$,因此可以枚举每一条边,然后删除这条边,再用bfs来求这条边两个端点间的最短路。这样就能得到包含这条边的环的最小长度。

  AC代码如下,时间复杂度为$O(m(n+m))$:

 1 class Solution {
 2 public:
 3     int findShortestCycle(int n, vector<vector<int>>& edges) {
 4         vector<vector<int>> g(n);
 5         for (auto &p : edges) {
 6             g[p[0]].push_back(p[1]);
 7             g[p[1]].push_back(p[0]);
 8         }
 9         int ret = 0x3f3f3f3f;
10         for (auto &p : edges) {
11             vector<int> dist(n, 0x3f3f3f3f);
12             dist[p[0]] = 0;
13             queue<int> q({p[0]});
14             while (!q.empty()) {
15                 int t = q.front();
16                 q.pop();
17                 for (auto &i : g[t]) {
18                     if (t == p[0] && i == p[1] || i == p[0] && t == p[1]) continue;
19                     if (dist[i] > dist[t] + 1) {
20                         dist[i] = dist[t] + 1;
21                         q.push(i);
22                     }
23                 }
24             }
25             ret = min(ret, dist[p[1]] + 1);
26         }
27         if (ret == 0x3f3f3f3f) ret = -1;
28         return ret;
29     }
30 };

 

参考资料

  最小环 - OI Wiki:https://oi-wiki.org/graph/min-circle/

标签:dist,Graph,vertex,ret,leq,edges,graph,Shortest,Cycle
From: https://www.cnblogs.com/onlyblues/p/17319160.html

相关文章

  • Unigraphics NX(UG NX)1957 安装包下载及(UG NX)1957 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • Unigraphics NX(UG NX)1926 安装包下载及(UG NX)1926 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • Unigraphics NX(UG NX)1899 安装包下载及(UG NX)1899 安装教程
    UG(UnigraphicsNX)是SiemensPLMSoftware公司出品的一个产品工程解决方案,它为用户的产品设计及加工过程提供了数字化造型和验证手段。UnigraphicsNX针对用户的虚拟产品设计和工艺设计的需求,以及满足各种工业化需求,提供了经过实践验证的解决方案。UG同时也是用户指南(userguide)和普......
  • POJ 2001 Shortest Prefixes(字典树)
    题目地址:POJ2001考察的字典树,利用的是建树时将每一个点只要走过就累加。最后从根节点开始遍历,当遍历到只有1次走过的时候,就说明这个地方是最短的独立前缀。然后记录下长度,输出即可。代码如下:#include<iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#inc......
  • 【Azure Developer】使用 Microsoft Graph API 获取 AAD User 操作示例
    问题描述查看官方文档“ Getauser ”,产生了一个操作示例的想法,在中国区Azure环境中,演示如何获取AADUser信息。 问题解答使用MicrosoftGraphAPI,演示如何获取AADUser信息,因参考文档是针对GlobalAzure,所以文档种的URL为://GlobalAzureMicrosoftGraphAPIHostG......
  • 在Django+Vue3+GraphQL的Blog例子代码中引入Element-Plus UI Framework
    Vue3的UIFramework中有Element-Plus、BalmUI、Quasar、PrimeVue、AntDesignVue等UIFramework.Element-Plus是Element-UI的Vue3版,Element-UI的使用人数的基数较大,Github上的Star数也较多,就选择了Element-Plus作为这个Blog项目的UIFramework.UIFramework的好处就是提供了......
  • Graphs with Python: Overview and Best Libraries
    GraphswithPython:OverviewandBestLibrariesGraphanalysis,interactivevisualizations,andgraphmachinelearning Agraphisarelativelyoldmathematicaldataentitythatisasetofconnectedelements.Sincethegraphisaveryflexiblestructure......
  • IDEA Java项目中Maven Lifecycle功能
    功能点clean用于清除之前构建生成的所有文件,具体为清除Target目录中的所有文件,包括该目录删除了install生成的所有文件。validate用于验证项目是否正确,并且说必要的信息是否都可用。compile编译项目的源代码,主要是Java文件。test编译和运行测试代码。p......
  • 决策树可视化Graphviz中文乱码
    输出svg时中文显示正常!!!fromsiximportStringIO#可视化dot_data=StringIO()tree.export_graphviz(clf,out_file=dot_data,feature_names=feature_name,class_names=target_name,filled=True,rounded=True,special_characte......
  • UVa 113 / POJ 2109 Power of Cryptography (使用double处理大整数&泰勒公式与误差分
    113-PowerofCryptographyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49http://poj.org/problem?id=2109题意:给出n和p,求出 ,但是p可以很大()如何存储p?不用大数可不可以?先看看double......