classSolution: deffrogPosition(self, n: int, edges: List[List[int]], t: int, target: int) -> float: G = [[] for i inrange(n + 1)] for i, j in edges: G[i].append(j) G[j].append(i) seen = [0] * (n + 1)
defdfs(i, t): nxt = len(G[i]) if i > 1: nxt -= 1 if nxt == 0or t == 0: return1.0if i == target else0.0 seen[i] = 1 for j in G[i]: ifnot seen[j]: p = dfs(j, t - 1) if p > 0: return p / nxt return0.0
var frogPosition = function(n, edges, t, target) { let G = []; for (let i = 0; i <= n; i++) { G.push([]); } for (let e of edges) { G[e[0]].push(e[1]); G[e[1]].push(e[0]); } let seen = newArray(n).fill(false); returndfs(G, seen, 1, t, target); }
functiondfs(G, seen, i, t, target) { let nxt = i == 1 ? G[i].length : G[i].length - 1; if (t == 0 || nxt == 0) { return i == target ? 1.0 : 0.0; } seen[i] = true; let ans = 0.0; for (let j of G[i]) { if (!seen[j]) { ans += dfs(G, seen, j, t - 1, target); } } return ans / nxt; }
publicclassSolution { publicdoubleFrogPosition(int n, int[][] edges, int t, int target) { List<List<int>> g = new List<List<int>>(); for (int i = 0; i <= n; i++) { g.Add(new List<int>()); } for (int i = 0; i < edges.Length; i++) { g[edges[i][0]].Add(edges[i][1]); g[edges[i][1]].Add(edges[i][0]); } bool[] seen = newbool[n + 1]; return Dfs(g, seen, 1, t, target); }
privatedoubleDfs(List<List<int>> g, bool[] seen, int i, int t, int target) { int nxt = i == 1 ? g[i].Count : g[i].Count - 1; if (t == 0 || nxt == 0) { return i == target ? 1.0 : 0.0; } seen[i] = true; double ans = 0.0; foreach (int j in g[i]) { if (!seen[j]) { ans += Dfs(g, seen, j, t - 1, target); } } return ans / nxt; } }
funcfrogPosition(n int, edges [][]int, t int, target int)float64 { G := make([][]int, n+1) for i := 0; i <= n; i++ { G[i] = make([]int, 0) } for _, e := range edges { G[e[0]] = append(G[e[0]], e[1]) G[e[1]] = append(G[e[1]], e[0]) } seen := make([]bool, n+1) return dfs(G, seen, 1, t, target) }
funcdfs(G [][]int, seen []bool, i int, t int, target int)float64 { nxt := len(G[i]) if i > 1 { nxt -= 1 } if t == 0 || nxt == 0 { if i == target { return1.0 } else { return0.0 } } seen[i] = true ans := 0.0 for _, j := range G[i] { if !seen[j] { ans += dfs(G, seen, j, t - 1, target) } } return ans / float64(nxt) }
doubledfs(constint **G, constint *GColSize, bool *visited, int i, int t, int target) { int nxt = i == 1 ? GColSize[i] : GColSize[i] - 1; if (t == 0 || nxt == 0) { return i == target ? 1.0 : 0.0; } visited[i] = true; double ans = 0.0; for (int k = 0; k < GColSize[i]; k++) { int j = G[i][k]; if (!visited[j]) { ans += dfs(G, GColSize, visited, j, t - 1, target); } } return ans / nxt; }
doublefrogPosition(int n, int** edges, int edgesSize, int* edgesColSize, int t, int target) { int *G[n + 1]; int GColSize[n + 1]; memset(GColSize, 0, sizeof(GColSize)); for (int i = 1; i <= n; i++) { G[i] = (int *)calloc(n + 1, sizeof(int)); } for (int i = 0; i < edgesSize; ++i) { int from = edges[i][0], to = edges[i][1]; G[from][GColSize[from]++] = to; G[to][GColSize[to]++] = from; } bool visited[n + 1]; memset(visited, 0, sizeof(visited)); double ret = dfs(G, GColSize, visited, 1, t, target); for (int i = 1; i <= n; i++) { free(G[i]); } return ret; }