|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../time-needed-to-inform-all-employees "Time Needed to Inform All Employees") |
| 9 | + |
| 10 | +Next > |
| 11 | + |
| 12 | +## [1377. Frog Position After T Seconds (Hard)](https://leetcode.com/problems/frog-position-after-t-seconds "T 秒后青蛙的位置") |
| 13 | + |
| 14 | +<p>Given an undirected tree consisting of <code>n</code> vertices numbered from 1 to <code>n</code>. A frog starts jumping from the <strong>vertex 1</strong>. In one second, the frog jumps from its current vertex to another <strong>unvisited</strong> vertex if they are directly connected. The frog can not jump back to a visited vertex. In case the frog can jump to several vertices it jumps randomly to one of them with the same probability, otherwise, when the frog can not jump to any unvisited vertex it jumps forever on the same vertex. </p> |
| 15 | + |
| 16 | +<p>The edges of the undirected tree are given in the array <code>edges</code>, where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> means that exists an edge connecting directly the vertices <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>.</p> |
| 17 | + |
| 18 | +<p><em>Return the probability that after <code>t</code> seconds the frog is on the vertex <code><font face="monospace">target</font></code>.</em></p> |
| 19 | + |
| 20 | +<p> </p> |
| 21 | +<p><strong>Example 1:</strong></p> |
| 22 | + |
| 23 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/20/frog_2.png" style="width: 350px; height: 236px;" /></p> |
| 24 | + |
| 25 | +<pre> |
| 26 | +<strong>Input:</strong> n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4 |
| 27 | +<strong>Output:</strong> 0.16666666666666666 |
| 28 | +<strong>Explanation: </strong>The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after <strong>second 1</strong> and then jumping with 1/2 probability to vertex 4 after <strong>second 2</strong>. Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p><strong>Example 2:</strong></p> |
| 32 | + |
| 33 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/02/20/frog_3.png" style="width: 350px; height: 236px;" /></strong></p> |
| 34 | + |
| 35 | +<pre> |
| 36 | +<strong>Input:</strong> n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7 |
| 37 | +<strong>Output:</strong> 0.3333333333333333 |
| 38 | +<strong>Explanation: </strong>The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 = 0.3333333333333333 probability to the vertex 7 after <strong>second 1</strong>. |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong>Example 3:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6 |
| 45 | +<strong>Output:</strong> 0.16666666666666666 |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>1 <= n <= 100</code></li> |
| 53 | + <li><code>edges.length == n-1</code></li> |
| 54 | + <li><code>edges[i].length == 2</code></li> |
| 55 | + <li><code>1 <= edges[i][0], edges[i][1] <= n</code></li> |
| 56 | + <li><code>1 <= t <= 50</code></li> |
| 57 | + <li><code>1 <= target <= n</code></li> |
| 58 | + <li>Answers within <code>10^-5</code> of the actual value will be accepted as correct.</li> |
| 59 | +</ul> |
| 60 | + |
| 61 | +### Related Topics |
| 62 | + [[Depth-first Search](../../tag/depth-first-search/README.md)] |
| 63 | + |
| 64 | +### Hints |
| 65 | +<details> |
| 66 | +<summary>Hint 1</summary> |
| 67 | +Use a variation of DFS with parameters 'curent_vertex' and 'current_time'. |
| 68 | +</details> |
| 69 | + |
| 70 | +<details> |
| 71 | +<summary>Hint 2</summary> |
| 72 | +Update the probability considering to jump to one of the children vertices. |
| 73 | +</details> |
0 commit comments