#include <bits/stdc++.h>
using namespace std;
#define Task "problemname"
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 4e3 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, k, i;
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 - mod % i] * (mod / i + 1) % mod;
ff[i] = (long long) ff[i - 1] * i % mod;
gg[i] = (long long) gg[i - 1] * vv[i] % mod;
}
}
int choose(int n, int k) {
return (long long) ff[n] * gg[k] % mod * gg[n - k] % mod;
}
int ll[N + 1], rr[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] % mod * dp[r][kr] % mod;
else {
y = (long long) y * choose(c, cc[l] - kl) % mod;
if (c < cc[i])
y = (long long) y * dp[l][cc[l]] % mod * dp[r][c + kk[i] - cc[l]] % mod;
else {
y = (long long) y * (c - cc[l] + kl) % mod;
y = (long long) y * dp[l][cc[l]] % mod * dp[r][cc[r]] % mod;
}
}
x = (x + y) % mod;
}
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) % mod;
y = (long long) y * choose(c, cc[l] - kl) % mod;
y = (long long) y * dp[l][cc[l]] % mod * dp[r][cc[r]] % mod;
x = (x + y) % mod;
}
}
}
dp[i][c] = x;
}
}
void process() {
init();
scanf("%d%d", &n, &k);
if (k > n) {
printf("0\n");
return;
}
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;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
if (fopen(Task".INP", "r")) {
freopen(Task".INP", "r", stdin);
freopen(Task".OUT", "w", stdout);
}
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
// dont stop
Compilation message
Main.cpp: In function 'void process()':
Main.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d", &i);
| ~~~~~^~~~~~~~~~
Main.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d%d", &ll[i], &rr[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:106:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(Task".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:107:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(Task".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
392 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
63656 KB |
Output is correct |
2 |
Correct |
69 ms |
63316 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
10 ms |
63068 KB |
Output is correct |
5 |
Correct |
8 ms |
62812 KB |
Output is correct |
6 |
Correct |
9 ms |
63048 KB |
Output is correct |
7 |
Correct |
28 ms |
63040 KB |
Output is correct |
8 |
Correct |
12 ms |
63064 KB |
Output is correct |
9 |
Correct |
23 ms |
63056 KB |
Output is correct |
10 |
Correct |
107 ms |
63572 KB |
Output is correct |
11 |
Correct |
85 ms |
63572 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
83 ms |
60720 KB |
Output is correct |
14 |
Correct |
56 ms |
58460 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
102 ms |
62824 KB |
Output is correct |
17 |
Correct |
84 ms |
58704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
62296 KB |
Output is correct |
2 |
Correct |
8 ms |
63044 KB |
Output is correct |
3 |
Correct |
8 ms |
62300 KB |
Output is correct |
4 |
Correct |
8 ms |
62300 KB |
Output is correct |
5 |
Correct |
8 ms |
62296 KB |
Output is correct |
6 |
Correct |
9 ms |
62296 KB |
Output is correct |
7 |
Correct |
8 ms |
62320 KB |
Output is correct |
8 |
Correct |
9 ms |
62556 KB |
Output is correct |
9 |
Correct |
9 ms |
62300 KB |
Output is correct |
10 |
Correct |
12 ms |
62468 KB |
Output is correct |
11 |
Correct |
8 ms |
62552 KB |
Output is correct |
12 |
Correct |
15 ms |
62812 KB |
Output is correct |
13 |
Correct |
9 ms |
62556 KB |
Output is correct |
14 |
Correct |
10 ms |
62300 KB |
Output is correct |
15 |
Correct |
9 ms |
62300 KB |
Output is correct |
16 |
Correct |
10 ms |
62420 KB |
Output is correct |
17 |
Correct |
8 ms |
62296 KB |
Output is correct |
18 |
Correct |
12 ms |
62488 KB |
Output is correct |
19 |
Correct |
8 ms |
62556 KB |
Output is correct |
20 |
Correct |
5 ms |
33372 KB |
Output is correct |
21 |
Correct |
8 ms |
60076 KB |
Output is correct |
22 |
Correct |
8 ms |
62224 KB |
Output is correct |
23 |
Correct |
8 ms |
62044 KB |
Output is correct |
24 |
Correct |
8 ms |
62300 KB |
Output is correct |
25 |
Correct |
8 ms |
57948 KB |
Output is correct |
26 |
Correct |
8 ms |
62296 KB |
Output is correct |
27 |
Correct |
9 ms |
62240 KB |
Output is correct |
28 |
Correct |
8 ms |
62044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
392 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
118 ms |
63656 KB |
Output is correct |
21 |
Correct |
69 ms |
63316 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
10 ms |
63068 KB |
Output is correct |
24 |
Correct |
8 ms |
62812 KB |
Output is correct |
25 |
Correct |
9 ms |
63048 KB |
Output is correct |
26 |
Correct |
28 ms |
63040 KB |
Output is correct |
27 |
Correct |
12 ms |
63064 KB |
Output is correct |
28 |
Correct |
23 ms |
63056 KB |
Output is correct |
29 |
Correct |
107 ms |
63572 KB |
Output is correct |
30 |
Correct |
85 ms |
63572 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
83 ms |
60720 KB |
Output is correct |
33 |
Correct |
56 ms |
58460 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
102 ms |
62824 KB |
Output is correct |
36 |
Correct |
84 ms |
58704 KB |
Output is correct |
37 |
Correct |
8 ms |
62296 KB |
Output is correct |
38 |
Correct |
8 ms |
63044 KB |
Output is correct |
39 |
Correct |
8 ms |
62300 KB |
Output is correct |
40 |
Correct |
8 ms |
62300 KB |
Output is correct |
41 |
Correct |
8 ms |
62296 KB |
Output is correct |
42 |
Correct |
9 ms |
62296 KB |
Output is correct |
43 |
Correct |
8 ms |
62320 KB |
Output is correct |
44 |
Correct |
9 ms |
62556 KB |
Output is correct |
45 |
Correct |
9 ms |
62300 KB |
Output is correct |
46 |
Correct |
12 ms |
62468 KB |
Output is correct |
47 |
Correct |
8 ms |
62552 KB |
Output is correct |
48 |
Correct |
15 ms |
62812 KB |
Output is correct |
49 |
Correct |
9 ms |
62556 KB |
Output is correct |
50 |
Correct |
10 ms |
62300 KB |
Output is correct |
51 |
Correct |
9 ms |
62300 KB |
Output is correct |
52 |
Correct |
10 ms |
62420 KB |
Output is correct |
53 |
Correct |
8 ms |
62296 KB |
Output is correct |
54 |
Correct |
12 ms |
62488 KB |
Output is correct |
55 |
Correct |
8 ms |
62556 KB |
Output is correct |
56 |
Correct |
5 ms |
33372 KB |
Output is correct |
57 |
Correct |
8 ms |
60076 KB |
Output is correct |
58 |
Correct |
8 ms |
62224 KB |
Output is correct |
59 |
Correct |
8 ms |
62044 KB |
Output is correct |
60 |
Correct |
8 ms |
62300 KB |
Output is correct |
61 |
Correct |
8 ms |
57948 KB |
Output is correct |
62 |
Correct |
8 ms |
62296 KB |
Output is correct |
63 |
Correct |
9 ms |
62240 KB |
Output is correct |
64 |
Correct |
8 ms |
62044 KB |
Output is correct |
65 |
Correct |
8 ms |
57948 KB |
Output is correct |
66 |
Correct |
8 ms |
62376 KB |
Output is correct |
67 |
Correct |
10 ms |
59996 KB |
Output is correct |
68 |
Correct |
8 ms |
62296 KB |
Output is correct |
69 |
Correct |
63 ms |
62456 KB |
Output is correct |
70 |
Correct |
29 ms |
62296 KB |
Output is correct |
71 |
Correct |
9 ms |
62300 KB |
Output is correct |
72 |
Correct |
26 ms |
62308 KB |
Output is correct |
73 |
Correct |
8 ms |
62448 KB |
Output is correct |
74 |
Correct |
8 ms |
60508 KB |
Output is correct |
75 |
Correct |
8 ms |
62300 KB |
Output is correct |
76 |
Correct |
113 ms |
63496 KB |
Output is correct |
77 |
Correct |
120 ms |
63428 KB |
Output is correct |
78 |
Correct |
8 ms |
62812 KB |
Output is correct |
79 |
Correct |
65 ms |
63296 KB |
Output is correct |
80 |
Correct |
8 ms |
63320 KB |
Output is correct |
81 |
Correct |
111 ms |
63516 KB |
Output is correct |
82 |
Correct |
9 ms |
62296 KB |
Output is correct |
83 |
Correct |
8 ms |
62300 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
8 ms |
58972 KB |
Output is correct |
86 |
Correct |
8 ms |
60564 KB |
Output is correct |
87 |
Correct |
9 ms |
62300 KB |
Output is correct |
88 |
Correct |
8 ms |
62300 KB |
Output is correct |
89 |
Correct |
8 ms |
62300 KB |
Output is correct |
90 |
Correct |
8 ms |
62300 KB |
Output is correct |
91 |
Correct |
9 ms |
62300 KB |
Output is correct |
92 |
Correct |
8 ms |
62404 KB |
Output is correct |
93 |
Correct |
10 ms |
62556 KB |
Output is correct |
94 |
Correct |
17 ms |
62656 KB |
Output is correct |
95 |
Correct |
22 ms |
62808 KB |
Output is correct |
96 |
Correct |
8 ms |
62300 KB |
Output is correct |
97 |
Correct |
8 ms |
62300 KB |
Output is correct |
98 |
Correct |
9 ms |
62396 KB |
Output is correct |
99 |
Correct |
10 ms |
62300 KB |
Output is correct |
100 |
Correct |
15 ms |
62548 KB |
Output is correct |
101 |
Correct |
14 ms |
62436 KB |
Output is correct |
102 |
Correct |
6 ms |
39516 KB |
Output is correct |
103 |
Correct |
9 ms |
60176 KB |
Output is correct |
104 |
Correct |
8 ms |
62044 KB |
Output is correct |