-
Notifications
You must be signed in to change notification settings - Fork 481
/
1377.java
29 lines (25 loc) · 826 Bytes
/
1377.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public double frogPosition(int n, int[][] edges, int t, int target) {
if (n == 1) return 1.0;
g = new List[n + 1];
this.target = target;
for (int i = 0; i <= n; i++) g[i] = new ArrayList();
for (int[] it : edges) {
g[it[0]].add(it[1]);
g[it[1]].add(it[0]);
}
return dfs(-1, 1, t);
}
private List<Integer>[] g;
private int target;
private double dfs(int fa, int cur, int t) {
if (cur != 1 && g[cur].size() == 1 || t == 0) {
return cur == target ? 1 : 0;
}
double res = 0.0;
for (int i : g[cur]) {
if (i != fa) res = Math.max(res, dfs(cur, i, t - 1));
}
return res / (g[cur].size() - (cur != 1 ? 1 : 0));
}
}