#P5333. [JSOI2019] 神经网络

[JSOI2019] 神经网络

Description

After a Martian is born, its neural network can be seen as a forest consisting of several undirected trees $\{T_1(V_1, E_1), T_2(V_2, E_2),\ldots T_m(V_m, E_m)\}$. As the Martian grows older, the number of neural connections keeps increasing. Initially, the set of newly grown connections in the neural network is E=E^\ast = \varnothing. The neural network grows according to the following rule:

  • If nodes uViu \in V_i and vVjv \in V_j belong to different undirected trees TiT_i and TjT_j respectively (iji \neq j), then EE^\ast should contain the edge (u,v)(u, v).

Finally, after no more neural network connections can grow, the connections between nodes in the neural network can be regarded as an undirected graph G(V,E)G(V,E), where

$$V=V_1\cup V_2\cup \ldots \cup V_m,E=E_1\cup E_2\cup \ldots \cup E_m\cup E^\ast$$

Martians make decisions by creating cycles in G(V,E)G(V, E). For different external inputs, Martians will create different neural connection cycles and thus produce different responses. To understand how complex Martian behavior patterns are, JYY decides to compute the number of Hamiltonian cycles in GG.

A Hamiltonian cycle of G(V,E)G(V, E) is a simple cycle that starts from the first node of the first tree, visits every other node in VV exactly once, and then returns to the first node of the first tree.

Input Format

The first line contains mm, the number of undirected trees in the Martian neural network at the beginning.
Then there are mm parts, where the ii-th part describes the tree TiT_i.
For TiT_i, the first line contains kik_i, the number of nodes in the tree Ti(Vi,Ei)T_i(V_i, E_i). Assume Vi={v1,v2,,vki}V_i = \{v_1, v_2,\ldots ,v_{k_i}\}.
In the next ki1k_i - 1 lines, each line contains two integers x,yx, y, meaning there is a tree edge between nodes vxv_x and vyv_y (1x,yki1 \le x, y \le k_i), i.e., (vx,vy)Ei(v_x, v_y) \in E_i.

Output Format

Since the number of Hamiltonian cycles can be very large, output a non-negative integer equal to the answer modulo 998244353998244353.

2
3
1 2
1 3
2
1 2
12

Hint

Translated by ChatGPT 5