1377. Frog Position After T Seconds

https://leetcode.com/problems/frog-position-after-t-seconds/submissions/

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.

给一棵树,从root开始随机选择一条边往下(不回头)遍历,问蹦T步后在target结点概率是多少?遍历=> DFS/BFS。BFS时记录下已访问过的结点,和到达每个点的概率。遍历时概率用乘法定律更新,并因为有子节点时不会回蹦,父节点概率置0。

class Solution {
public:
    double frogPosition(int n, vector<vector<int>>& edges, int T, int target) {
        queue<int> q;
        q.push(1);
        vector<bool> v(n + 1, false);
        vector<double> p(n + 1, 0);
        p[1] = 1.0;
        v[1] = true;
        vector<vector<int>> g(n + 1, vector<int>());
        for (const auto e : edges) {
            g[e[0]].push_back(e[1]);
            g[e[1]].push_back(e[0]);
        }
        for (int i = 0; i < T && !q.empty(); ++i) {
            for (int j = q.size(); j > 0; --j) {
                const auto t = q.front(); q.pop();
                int cnt = 0;
                for (const auto n : g[t]) {
                    if (v[n]) continue;
                    ++cnt;
                }
                if (cnt == 0) continue;
                const double prob = 1.0 / (double)(cnt);
                for (const auto n : g[t]) {
                    if (v[n]) continue;
                    v[n] = true;
                    q.push(n);
                    p[n] = p[t] * prob;
                }
                p[t] = 0.0;
            }
        }
        return p[target];
    }
};

Last updated