-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDependent_Events.cpp
More file actions
154 lines (126 loc) · 4.13 KB
/
Dependent_Events.cpp
File metadata and controls
154 lines (126 loc) · 4.13 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Round H 2021 - Kick Start 2021: https://codingcompetitions.withgoogle.com/kickstart/round/0000000000435914/00000000008d9970
/* CONCEPTS USED:
- LCA for general or n-ary trees (Sparse Matrix DP approach)
- Fast Modular Exponentiation + Modular Inverse
- Reducing a decimal to fraction in lowest irreducible form
- Conditional probability over graph
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LARGE 1000000.0
#define boost ios_base ::sync_with_stdio(0); cin.tie(0);
#define MAXN 1005
#define level 18
vector < ll > tree[MAXN];
ll depth[MAXN];
ll parent[MAXN][level];
vector < double > prob, occ, not_occ;
ll gcd(ll a, ll b) {
if (a == 0) return b;
else if (b == 0) return a;
if (a < b) return gcd(a, b % a);
else return gcd(b, a % b);
}
pair < ll, ll > reduce(double input){
double integral = floor(input);
double frac = input - integral;
ll precision = 1000000000; // This is the accuracy.
ll gcd_ = gcd((ll)round(frac * precision), precision);
ll denominator = precision / gcd_;
ll numerator = round(frac * precision) / gcd_;
pair < ll, ll > retVal = make_pair(integral*denominator + numerator, denominator);
return retVal;
}
ll fastModExp(ll a, ll b, ll m){
ll res = 1;
while(b > 0){
if(b & 1) res = (res*a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
// pre-compute the depth for each node and their first parent(2^0th parent)
void dfs(ll cur, ll prev) {
depth[cur] = depth[prev] + 1;
parent[cur][0] = prev;
for (int i = 0; i < tree[cur].size(); i++)
if (tree[cur][i] != prev)
dfs(tree[cur][i], cur);
}
// Dynamic Programming Sparse Matrix Approach populating 2^i parent for each node
void precomputeSparseMatrix(ll n) {
for (int i = 1; i < level; i++)
for (int node = 1; node <= n; node++)
if (parent[node][i-1] != -1)
parent[node][i] = parent[parent[node][i-1]][i-1];
}
// Returning the LCA of u and v
ll lca(ll u, ll v) {
if (depth[v] < depth[u]) swap(u, v);
ll diff = depth[v] - depth[u];
for (int i = 0; i < level; i++)
if ((diff >> i) & 1)
v = parent[v][i];
// now depth[u] == depth[v]
if (u == v) return u;
for (int i = level-1; i >= 0; i--)
if (parent[u][i] != parent[v][i]){
u = parent[u][i];
v = parent[v][i];
}
return parent[u][0];
}
void addEdge(ll u,ll v) {
tree[u].push_back(v);
tree[v].push_back(u);
}
double findCondProb(ll u, ll LCA, bool lca_occurs){
if(u == LCA) return lca_occurs ? 1 : 0;
double parentCond = findCondProb(parent[u][0], LCA, lca_occurs);
return parentCond * occ[u] + (1 - parentCond) * not_occ[u];
}
void solve(){
ll n, q, k; cin >> n >> q >> k;
prob.assign(n+1, 0);
occ.assign(n+1, 0);
not_occ.assign(n+1, 0);
prob[1] = k / LARGE;
for(ll i = 2; i <= n; i++){
ll par; cin >> par >> occ[i] >> not_occ[i];
addEdge(i, par);
occ[i] /= LARGE;
not_occ[i] /= LARGE;
prob[i] = prob[par] * occ[i] + (1 - prob[par]) * not_occ[i];
}
memset(parent, -1, sizeof(parent));
depth[0] = 0;
dfs(1, 0); // running dfs and precalculating depth of each node
precomputeSparseMatrix(n + 1); // Precomputing the 2^i th ancestor for evey node
while(q--){
ll u, v; cin >> u >> v;
ll LCA = lca(u, v);
double prob_combined = prob[LCA] * findCondProb(u, LCA, true) * findCondProb(v, LCA, true)
+ (1 - prob[LCA]) * findCondProb(u, LCA, false) * findCondProb(v, LCA, false);
// cout << prob_combined << " ";
pair < ll, ll > frac = reduce(prob_combined);
ll num = frac.first, den = frac.second;
ll den_inv = fastModExp(den, MOD-2, MOD);
ll ans = ((num % MOD) * (den_inv % MOD)) % MOD;
cout << ans << " ";
}
cout << endl;
}
int main() {
boost;
// cout << fixed << setprecision(20);
ll t;
cin >> t;
for (int testcase = 1; testcase <= t; testcase++){
cout << "Case #" << testcase << ": ";
solve();
}
}