1377. Frog Position After T Seconds

Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from the vertex 1. In one second, the frog jumps from its current vertex to another unvisited 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. 

The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting directly the vertices fromi and toi.

Return the probability that after t seconds the frog is on the vertex target.

 

Example 1:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4
Output: 0.16666666666666666 
Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and then jumping with 1/2 probability to vertex 4 after second 2. Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666. 

Example 2:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 1, target = 7
Output: 0.3333333333333333
Explanation: 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 second 1. 

Example 3:

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 20, target = 6
Output: 0.16666666666666666

 

Constraints:

  • 1 <= n <= 100
  • edges.length == n-1
  • edges[i].length == 2
  • 1 <= edges[i][0], edges[i][1] <= n
  • 1 <= t <= 50
  • 1 <= target <= n
  • Answers within 10^-5 of the actual value will be accepted as correct.

Rust Solution

struct Solution;

impl Solution {
    fn frog_position(n: i32, edges: Vec<Vec<i32>>, t: i32, target: i32) -> f64 {
        let n = n as usize;
        let mut adj: Vec<Vec<usize>> = vec![vec![]; n];
        let mut prob: Vec<f64> = vec![0.0; n];
        prob[0] = 1.0;
        for e in edges {
            let u = e[0] as usize - 1;
            let v = e[1] as usize - 1;
            adj[u].push(v);
            adj[v].push(u);
        }
        Self::dfs(0, 0, t, &mut prob, &adj);
        prob[target as usize - 1]
    }
    fn dfs(u: usize, prev: usize, t: i32, prob: &mut Vec<f64>, adj: &[Vec<usize>]) {
        let size = adj[u].len() - if u == prev { 0 } else { 1 };
        if size == 0 || t == 0 {
            return;
        }
        let p = prob[u];
        for &v in &adj[u] {
            if v != prev {
                prob[v] += p / size as f64;
                Self::dfs(v, u, t - 1, prob, adj);
            }
        }
        prob[u] = 0.0;
    }
}

#[test]
fn test() {
    use assert_approx_eq::assert_approx_eq;

    let n = 7;
    let edges = vec_vec_i32![[1, 2], [1, 3], [1, 7], [2, 4], [2, 6], [3, 5]];
    let t = 2;
    let target = 4;
    let res = 0.16666666666666666;
    assert_approx_eq!(Solution::frog_position(n, edges, t, target), res);
    let n = 7;
    let edges = vec_vec_i32![[1, 2], [1, 3], [1, 7], [2, 4], [2, 6], [3, 5]];
    let t = 1;
    let target = 7;
    let res = 0.3333333333333333;
    assert_approx_eq!(Solution::frog_position(n, edges, t, target), res);
    let n = 7;
    let edges = vec_vec_i32![[1, 2], [1, 3], [1, 7], [2, 4], [2, 6], [3, 5]];
    let t = 20;
    let target = 6;
    let res = 0.16666666666666666;
    assert_approx_eq!(Solution::frog_position(n, edges, t, target), res);
}

Having problems with this solution? Click here to submit an issue on github.