Given an undirected tree consisting of n vertices numbered from 1 to n.
A frog starts jumping from 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] = [ai, bi] means that exists an edge connecting the vertices ai
and bi.
_Return the probability that aftert seconds the frog is on the vertex
target. _Answers within 10-5 of the actual answer will be accepted.

Input: n =7, edges =[[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t =2, target =4Output: 0.16666666666666666Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with1/3 probability to the vertex 2 after **second 1** and then jumping with1/2 probability to vertex 4 after **second 2**. Thus the probability for the frog is on the vertex 4 after 2 seconds is1/3*1/2=1/6=0.16666666666666666.
****
Input: n =7, edges =[[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t =1, target =7Output: 0.3333333333333333Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with1/3=0.3333333333333333 probability to the vertex 7 after **second 1**.
The frog moves randomly to any unvisited neighbor at each step. We can use DFS to simulate all possible paths, propagating the probability at each step. If the frog reaches the target at time t or gets stuck (no unvisited neighbors), we record the probability.
classSolution {
public:double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
vector<vector<int>> g(n +1);
for (auto& e : edges) {
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
vector<bool> vis(n +1, false);
returndfs(1, 0, 1.0, t, target, g, vis);
}
doubledfs(int u, int time, double prob, int t, int target, vector<vector<int>>& g, vector<bool>& vis) {
vis[u] = true;
int cnt =0;
for (int v : g[u]) if (!vis[v]) cnt++;
if (time == t || cnt ==0) return u == target ? prob : 0.0;
double ans =0.0;
for (int v : g[u]) {
if (!vis[v]) {
ans += dfs(v, time +1, prob / cnt, t, target, g, vis);
}
}
return ans;
}
};
classSolution {
publicdoublefrogPosition(int n, int[][] edges, int t, int target) {
List<Integer>[] g =new List[n + 1];
for (int i = 1; i <= n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(e[1]);
g[e[1]].add(e[0]);
}
boolean[] vis =newboolean[n + 1];
return dfs(1, 0, 1.0, t, target, g, vis);
}
doubledfs(int u, int time, double prob, int t, int target, List<Integer>[] g, boolean[] vis) {
vis[u]=true;
int cnt = 0;
for (int v : g[u]) if (!vis[v]) cnt++;
if (time == t || cnt == 0) return u == target ? prob : 0.0;
double ans = 0.0;
for (int v : g[u]) {
if (!vis[v]) {
ans += dfs(v, time + 1, prob / cnt, t, target, g, vis);
}
}
return ans;
}
}
classSolution {
funfrogPosition(n: Int, edges: Array<IntArray>, t: Int, target: Int): Double {
val g = Array(n + 1) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
val vis = BooleanArray(n + 1)
fundfs(u: Int, time: Int, prob: Double): Double {
vis[u] = trueval cnt = g[u].count { !vis[it] }
if (time == t || cnt ==0) returnif (u == target) prob else0.0var ans = 0.0for (v in g[u]) {
if (!vis[v]) {
ans += dfs(v, time + 1, prob / cnt)
}
}
return ans
}
return dfs(1, 0, 1.0)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
deffrogPosition(self, n: int, edges: list[list[int]], t: int, target: int) -> float:
from collections import defaultdict
g = defaultdict(list)
for a, b in edges:
g[a].append(b)
g[b].append(a)
vis = [False] * (n +1)
defdfs(u: int, time: int, prob: float) -> float:
vis[u] =True cnt = sum(not vis[v] for v in g[u])
if time == t or cnt ==0:
return prob if u == target else0.0 ans =0.0for v in g[u]:
ifnot vis[v]:
ans += dfs(v, time +1, prob / cnt)
return ans
return dfs(1, 0, 1.0)