-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl11a.cpp
139 lines (108 loc) · 2.47 KB
/
l11a.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
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
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define dwn(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
const int MAX_N = 2100;
const int MOD = 1e9 + 7;
#define mod(x) ((x) % MOD)
int n, k, cur;
int ws = 0, invws;
bool isv[MAX_N] = {false};
struct client {
int w;
int ip;
int np;
vector<int> adj;
} c[MAX_N];
int p[MAX_N][MAX_N];
inline int mul_m(ll a, ll b) { return mod(mod(a) * mod(b)); }
inline int plus_m(ll a, ll b) { return mod(mod(a) + mod(b)); }
inline int inv(ll a) {
int ans = 1;
int b = MOD - 2;
a = mod(mod(a) + MOD);
for (; b; b >>= 1) {
if (b & 1) ans = mod(a * ans);
a = mod(a * a);
}
return ans;
}
int read();
void init();
int dfs(int root);
int main() {
int ans = 0;
init();
invws = inv(ws);
rep(i, 1, n) {
memset(isv, false, sizeof(isv));
memset(p, 0, sizeof(int) * MAX_N * MAX_N);
cur = i;
dfs(i);
ans = plus_m(ans, p[i][k]);
}
printf("%d", ans);
return 0;
}
void init() {
n = read();
k = read();
int u, v;
rep(i, 1, n - 1) {
u = read();
v = read();
c[u].adj.pb(v);
c[v].adj.pb(u);
}
int w, a, b;
rep(i, 1, n) {
w = read();
ws += w;
c[i].w = w;
a = read();
b = read();
int invb = inv(b);
c[i].ip = mul_m(a, invb);
c[i].np = mul_m(b - a, invb);
}
}
int dfs(int root) {
isv[root] = true;
int ch_cnt = 1;
if (root == cur) {
p[root][1] = mul_m(c[root].w, invws);
p[root][0] = mul_m(ws - c[root].w, invws);
} else {
p[root][0] = c[root].np;
p[root][1] = c[root].ip;
}
for (int a: c[root].adj) {
if (!isv[a]) {
int child = dfs(a);
dwn(i, ch_cnt + child, 1) {
int unit = 0;
rep(j, 1, min(j, ch_cnt)) {
unit = plus_m(unit, mul_m(p[root][j], p[a][i - j]));
}
p[root][i] = unit;
}
ch_cnt += child;
}
}
return ch_cnt;
}
int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ '0'), ch = getchar();
return s * f;
}