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
| #include <cstdio> #include <vector> #include <queue> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long LL; const int N = 2e4 + 10, M = 110, mod = 998244353; vector <int> g[N]; int K, n[N], m[N], d[N], td[N], f[N], A[M][M]; int det (int n) { int res = 1; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { while (A[i][i]) { int t = A[j][i] / A[i][i]; for (int k = i; k <= n; k++) (A[j][k] -= (LL)t * A[i][k] % mod) %= mod; swap(A[i], A[j]); res *= -1; } swap(A[i], A[j]); res *= -1; } for (int i = 1; i <= n; i++) res = (LL)res * A[i][i] % mod; return res; } int main () { int T; read(T); while (T--) { read(K); for (int i = 1; i <= K; ++i) read(n[i]), n[i] += n[i - 1]; for (int i = 1; i < K; ++i) read(m[i]); for (int i = 1; i <= n[K]; ++i) td[i] = 0, g[i].clear(); for (int i = 1; i < K; ++i) for (int a, b; m[i]; --m[i]) { read(a, b); a += n[i - 1], b += n[i]; ++td[b], g[a].push_back(b); } for (int st = 1; st <= n[1]; ++st) { queue <int> q; for (int i = 1; i <= n[K]; ++i) d[i] = td[i], f[i] = 0; f[st] = 1; for (int i = 1; i <= n[K]; ++i) if (!d[i]) q.push(i); while (!q.empty()) { int t = q.front(); q.pop(); for (int i : g[t]) { (f[i] += f[t]) %= mod; if (!--d[i]) q.push(i); } } for (int i = n[K - 1] + 1; i <= n[K]; ++i) A[st][i - n[K - 1]] = f[i]; } write((det(n[1]) + mod) % mod), puts(""); } return 0; }
|