1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/图中的最短环 [shortest-cycle-in-a-graph].html
2023-04-04 00:26:09 +08:00

35 lines
1.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>现有一个含 <code>n</code> 个顶点的 <strong>双向</strong> 图,每个顶点按从 <code>0</code><code>n - 1</code> 标记。图中的边由二维整数数组 <code>edges</code> 表示,其中 <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> 表示顶点 <code>u<sub>i</sub></code><code>v<sub>i</sub></code> 之间存在一条边。每对顶点最多通过一条边连接,并且不存在与自身相连的顶点。</p>
<p>返回图中 <strong>最短</strong> 环的长度。如果不存在环,则返回 <code>-1</code></p>
<p><strong></strong> 是指以同一节点开始和结束,并且路径中的每条边仅使用一次。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;">
<pre><strong>输入:</strong>n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
<strong>输出:</strong>3
<strong>解释:</strong>长度最小的循环是0 -&gt; 1 -&gt; 2 -&gt; 0
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;">
<pre><strong>输入:</strong>n = 4, edges = [[0,1],[0,2]]
<strong>输出:</strong>-1
<strong>解释:</strong>图中不存在循环
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 1000</code></li>
<li><code>1 &lt;= edges.length &lt;= 1000</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li>
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
<li>不存在重复的边</li>
</ul>