#include <stdio.h>
#define N 4000
#define MD 1000000007
int vv[N + 1], ff[N + 1], gg[N + 1];
void init() {
int i;
ff[0] = gg[0] = 1;
for (i = 1; i <= N; i++) {
vv[i] = i == 1 ? 1 : (long long) vv[i - MD % i] * (MD / i + 1) % MD;
ff[i] = (long long) ff[i - 1] * i % MD;
gg[i] = (long long) gg[i - 1] * vv[i] % MD;
}
}
int choose(int n, int k) {
return (long long) ff[n] * gg[k] % MD * gg[n - k] % MD;
}
int ll[N + 1], rr[N + 1], sz[N + 1], kk[N + 1], sz[N + 1], cc[N + 1], dp[N + 1][N + 1];
void dfs(int i, int d) {
int l = ll[i], r = rr[i], c, kl, kr, x, y, tmp;
if (i == 0)
return;
dfs(l, 0);
dfs(r, 1);
if (d == 1)
tmp = l, l = r, r = tmp;
sz[i] = sz[l] + sz[r] + 1, cc[i] = cc[l] + cc[r] + 1 - kk[i];
for (c = 0; c <= cc[i]; c++) {
x = 0;
for (kl = 0; kl <= kk[i]; kl++) {
kr = kk[i] - kl;
if (kl <= cc[l] && kr <= cc[r]) {
y = choose(kk[i], kl);
if (c <= cc[l] - kl)
y = (long long) y * dp[l][c + kl] % MD * dp[r][kr] % MD;
else {
y = (long long) y * choose(c, cc[l] - kl) % MD;
if (c < cc[i])
y = (long long) y * dp[l][cc[l]] % MD * dp[r][c + kk[i] - cc[l]] % MD;
else {
y = (long long) y * (c - cc[l] + kl) % MD;
y = (long long) y * dp[l][cc[l]] % MD * dp[r][cc[r]] % MD;
}
}
x = (x + y) % MD;
}
if (c == cc[i]) {
kr = kk[i] - kl - 1;
if (kl <= cc[l] && kr <= cc[r]) {
y = (long long) choose(kk[i], kl) * (kk[i] - kl) % MD;
y = (long long) y * choose(c, cc[l] - kl) % MD;
y = (long long) y * dp[l][cc[l]] % MD * dp[r][cc[r]] % MD;
x = (x + y) % MD;
}
}
}
dp[i][c] = x;
}
}
int main() {
int n, k, i;
init();
scanf("%d%d", &n, &k);
if (k > n) {
printf("0\n");
return 0;
}
while (k--) {
scanf("%d", &i);
kk[i]++;
}
for (i = 1; i <= n; i++)
scanf("%d%d", &ll[i], &rr[i]);
sz[0] = 0, cc[0] = 0, dp[0][0] = 1;
dfs(1, 0);
printf("%d\n", dp[1][0]);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:72:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &i);
| ^~~~~~~~~~~~~~~
Main.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d", &ll[i], &rr[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
46396 KB |
Output is correct |
2 |
Correct |
69 ms |
32480 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
14548 KB |
Output is correct |
5 |
Correct |
5 ms |
11476 KB |
Output is correct |
6 |
Correct |
9 ms |
17116 KB |
Output is correct |
7 |
Correct |
30 ms |
22184 KB |
Output is correct |
8 |
Correct |
11 ms |
18076 KB |
Output is correct |
9 |
Correct |
25 ms |
20980 KB |
Output is correct |
10 |
Correct |
118 ms |
45500 KB |
Output is correct |
11 |
Correct |
94 ms |
38616 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
93 ms |
37980 KB |
Output is correct |
14 |
Correct |
69 ms |
29000 KB |
Output is correct |
15 |
Correct |
0 ms |
356 KB |
Output is correct |
16 |
Correct |
113 ms |
43812 KB |
Output is correct |
17 |
Correct |
86 ms |
35344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16584 KB |
Output is correct |
2 |
Correct |
7 ms |
17108 KB |
Output is correct |
3 |
Correct |
8 ms |
16468 KB |
Output is correct |
4 |
Correct |
8 ms |
16656 KB |
Output is correct |
5 |
Correct |
7 ms |
16496 KB |
Output is correct |
6 |
Correct |
9 ms |
16740 KB |
Output is correct |
7 |
Correct |
8 ms |
16596 KB |
Output is correct |
8 |
Correct |
8 ms |
16676 KB |
Output is correct |
9 |
Correct |
8 ms |
16596 KB |
Output is correct |
10 |
Correct |
12 ms |
17108 KB |
Output is correct |
11 |
Correct |
9 ms |
16680 KB |
Output is correct |
12 |
Correct |
15 ms |
18260 KB |
Output is correct |
13 |
Correct |
10 ms |
16980 KB |
Output is correct |
14 |
Correct |
8 ms |
16736 KB |
Output is correct |
15 |
Correct |
8 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16752 KB |
Output is correct |
17 |
Correct |
7 ms |
16520 KB |
Output is correct |
18 |
Correct |
11 ms |
17164 KB |
Output is correct |
19 |
Correct |
7 ms |
16596 KB |
Output is correct |
20 |
Correct |
4 ms |
8404 KB |
Output is correct |
21 |
Correct |
7 ms |
15700 KB |
Output is correct |
22 |
Correct |
7 ms |
16340 KB |
Output is correct |
23 |
Correct |
8 ms |
16268 KB |
Output is correct |
24 |
Correct |
7 ms |
16520 KB |
Output is correct |
25 |
Correct |
7 ms |
14940 KB |
Output is correct |
26 |
Correct |
7 ms |
16368 KB |
Output is correct |
27 |
Correct |
7 ms |
15840 KB |
Output is correct |
28 |
Correct |
6 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
125 ms |
46396 KB |
Output is correct |
21 |
Correct |
69 ms |
32480 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
7 ms |
14548 KB |
Output is correct |
24 |
Correct |
5 ms |
11476 KB |
Output is correct |
25 |
Correct |
9 ms |
17116 KB |
Output is correct |
26 |
Correct |
30 ms |
22184 KB |
Output is correct |
27 |
Correct |
11 ms |
18076 KB |
Output is correct |
28 |
Correct |
25 ms |
20980 KB |
Output is correct |
29 |
Correct |
118 ms |
45500 KB |
Output is correct |
30 |
Correct |
94 ms |
38616 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
93 ms |
37980 KB |
Output is correct |
33 |
Correct |
69 ms |
29000 KB |
Output is correct |
34 |
Correct |
0 ms |
356 KB |
Output is correct |
35 |
Correct |
113 ms |
43812 KB |
Output is correct |
36 |
Correct |
86 ms |
35344 KB |
Output is correct |
37 |
Correct |
7 ms |
16584 KB |
Output is correct |
38 |
Correct |
7 ms |
17108 KB |
Output is correct |
39 |
Correct |
8 ms |
16468 KB |
Output is correct |
40 |
Correct |
8 ms |
16656 KB |
Output is correct |
41 |
Correct |
7 ms |
16496 KB |
Output is correct |
42 |
Correct |
9 ms |
16740 KB |
Output is correct |
43 |
Correct |
8 ms |
16596 KB |
Output is correct |
44 |
Correct |
8 ms |
16676 KB |
Output is correct |
45 |
Correct |
8 ms |
16596 KB |
Output is correct |
46 |
Correct |
12 ms |
17108 KB |
Output is correct |
47 |
Correct |
9 ms |
16680 KB |
Output is correct |
48 |
Correct |
15 ms |
18260 KB |
Output is correct |
49 |
Correct |
10 ms |
16980 KB |
Output is correct |
50 |
Correct |
8 ms |
16736 KB |
Output is correct |
51 |
Correct |
8 ms |
16724 KB |
Output is correct |
52 |
Correct |
9 ms |
16752 KB |
Output is correct |
53 |
Correct |
7 ms |
16520 KB |
Output is correct |
54 |
Correct |
11 ms |
17164 KB |
Output is correct |
55 |
Correct |
7 ms |
16596 KB |
Output is correct |
56 |
Correct |
4 ms |
8404 KB |
Output is correct |
57 |
Correct |
7 ms |
15700 KB |
Output is correct |
58 |
Correct |
7 ms |
16340 KB |
Output is correct |
59 |
Correct |
8 ms |
16268 KB |
Output is correct |
60 |
Correct |
7 ms |
16520 KB |
Output is correct |
61 |
Correct |
7 ms |
14940 KB |
Output is correct |
62 |
Correct |
7 ms |
16368 KB |
Output is correct |
63 |
Correct |
7 ms |
15840 KB |
Output is correct |
64 |
Correct |
6 ms |
16212 KB |
Output is correct |
65 |
Correct |
9 ms |
15044 KB |
Output is correct |
66 |
Correct |
8 ms |
16852 KB |
Output is correct |
67 |
Correct |
7 ms |
15376 KB |
Output is correct |
68 |
Correct |
7 ms |
16724 KB |
Output is correct |
69 |
Correct |
63 ms |
16716 KB |
Output is correct |
70 |
Correct |
25 ms |
16640 KB |
Output is correct |
71 |
Correct |
8 ms |
16684 KB |
Output is correct |
72 |
Correct |
28 ms |
16652 KB |
Output is correct |
73 |
Correct |
7 ms |
16468 KB |
Output is correct |
74 |
Correct |
5 ms |
11280 KB |
Output is correct |
75 |
Correct |
8 ms |
16596 KB |
Output is correct |
76 |
Correct |
123 ms |
46368 KB |
Output is correct |
77 |
Correct |
151 ms |
46504 KB |
Output is correct |
78 |
Correct |
3 ms |
6356 KB |
Output is correct |
79 |
Correct |
70 ms |
32932 KB |
Output is correct |
80 |
Correct |
7 ms |
17132 KB |
Output is correct |
81 |
Correct |
121 ms |
46192 KB |
Output is correct |
82 |
Correct |
7 ms |
16468 KB |
Output is correct |
83 |
Correct |
7 ms |
16608 KB |
Output is correct |
84 |
Correct |
0 ms |
340 KB |
Output is correct |
85 |
Correct |
5 ms |
11236 KB |
Output is correct |
86 |
Correct |
5 ms |
11156 KB |
Output is correct |
87 |
Correct |
7 ms |
16596 KB |
Output is correct |
88 |
Correct |
7 ms |
16476 KB |
Output is correct |
89 |
Correct |
8 ms |
16608 KB |
Output is correct |
90 |
Correct |
8 ms |
16532 KB |
Output is correct |
91 |
Correct |
8 ms |
16724 KB |
Output is correct |
92 |
Correct |
8 ms |
16724 KB |
Output is correct |
93 |
Correct |
9 ms |
16960 KB |
Output is correct |
94 |
Correct |
16 ms |
18388 KB |
Output is correct |
95 |
Correct |
19 ms |
18808 KB |
Output is correct |
96 |
Correct |
8 ms |
16596 KB |
Output is correct |
97 |
Correct |
8 ms |
16604 KB |
Output is correct |
98 |
Correct |
8 ms |
16596 KB |
Output is correct |
99 |
Correct |
10 ms |
16964 KB |
Output is correct |
100 |
Correct |
16 ms |
18132 KB |
Output is correct |
101 |
Correct |
15 ms |
17800 KB |
Output is correct |
102 |
Correct |
5 ms |
10196 KB |
Output is correct |
103 |
Correct |
6 ms |
15488 KB |
Output is correct |
104 |
Correct |
7 ms |
15940 KB |
Output is correct |