-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1377.cpp
More file actions
37 lines (37 loc) · 944 Bytes
/
Copy path1377.cpp
File metadata and controls
37 lines (37 loc) · 944 Bytes
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
class Solution {
public:
double prob[105];
int depth[105];
bool is_leaf[105];
vector<int> G[105];
void dfs(int v, int p, int d, double pr){
int n_children = G[v].size();
if(v != 0) n_children--;
if(n_children == 0){
is_leaf[v] = 1;
}
depth[v] = d;
prob[v] = pr;
for(int c : G[v]){
if(c == p) continue;
dfs(c, v, d+1, pr / n_children);
}
}
double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
for(vector<int> &e : edges){
G[e[0]-1].push_back(e[1]-1);
G[e[1]-1].push_back(e[0]-1);
}
dfs(0, -1, 0, 1);
if(depth[target-1] > t){
return 0;
}
if(depth[target-1] == t){
return prob[target-1];
}
if(is_leaf[target-1]){
return prob[target-1];
}
return 0;
}
};