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)/重新规划路线 [reorder-routes-to-make-all-paths-lead-to-the-city-zero].html
2022-03-29 12:43:11 +08:00

46 lines
2.2 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> 座城市,从 <code>0</code><code>n-1</code> 编号,其间共有 <code>n-1</code> 条路线。因此,要想在两座不同城市之间旅行只有唯一一条路线可供选择(路线网形成一颗树)。去年,交通运输部决定重新规划路线,以改变交通拥堵的状况。</p>
<p>路线用 <code>connections</code> 表示,其中 <code>connections[i] = [a, b]</code> 表示从城市 <code>a</code><code>b</code> 的一条有向路线。</p>
<p>今年,城市 0 将会举办一场大型比赛,很多游客都想前往城市 0 。</p>
<p>请你帮助重新规划路线方向,使每个城市都可以访问城市 0 。返回需要变更方向的最小路线数。</p>
<p>题目数据 <strong>保证</strong> 每个城市在重新规划路线方向后都能到达城市 0 。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/30/sample_1_1819.png" style="height: 150px; width: 240px;"></strong></p>
<pre><strong>输入:</strong>n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
<strong>输出:</strong>3
<strong>解释:</strong>更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。</pre>
<p><strong>示例 2</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/05/30/sample_2_1819.png" style="height: 60px; width: 380px;"></strong></p>
<pre><strong>输入:</strong>n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
<strong>输出:</strong>2
<strong>解释:</strong>更改以红色显示的路线的方向,使每个城市都可以到达城市 0 。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>n = 3, connections = [[1,0],[2,0]]
<strong>输出:</strong>0
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 5 * 10^4</code></li>
<li><code>connections.length == n-1</code></li>
<li><code>connections[i].length == 2</code></li>
<li><code>0 &lt;= connections[i][0], connections[i][1] &lt;= n-1</code></li>
<li><code>connections[i][0] != connections[i][1]</code></li>
</ul>