-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#1377 Frog Position After T Seconds.cpp
57 lines (51 loc) · 1.32 KB
/
#1377 Frog Position After T Seconds.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
vector<double> dp;
vector<vector<int>> adj;
vector<int> leaf,depth;
int n,T,Tar;
void helper(int node,int time,int par,int dpt){
if(time>T) return;
if(dp[node] != -1.0) return;
if(par==0){
dp[node]=1;
}
else{
if(adj[par].size()>1)
dp[node]= (dp[par])/(double)(adj[par].size()-1);
else
dp[node]= dp[par];
}
depth[node]=dpt;
for(auto ch : adj[node]){
if(ch == par) continue;
helper(ch,time +1,node,dpt+1);
}
if(adj[node].size()==1) leaf[node]=1;
}
double frogPosition(int N, vector<vector<int>>& edges, int t, int target) {
n = N;
T = t;
Tar = target;
adj.resize(n+1);
for(auto e:edges){
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
adj[1].push_back(0);
dp.resize(n+1,-1.0);
leaf.resize(n+1,0);
depth.resize(n+1,0);
helper(1,0,0,0);
if(leaf[target]){
if(t>=depth[target]){
return dp[target];
}
else return 0.0;
}
else{
if(t==depth[target]) return dp[target];
}
return 0.0;
}
};