#P3349. [ZJOI2016] 小星星

    ID: 2398 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2016浙江枚举,暴力容斥

[ZJOI2016] 小星星

Description

Xiao Y is a skillful girl who likes making small handmade accessories. She has nn little stars, strung together by mm colored thin threads, and each thread connects two stars.

One day she found that her accessory had been damaged, and many threads were removed. The accessory now has only n1n-1 threads, but through these threads, the stars are still strung together; that is, these stars form a tree through these threads. Xiao Y found the design blueprint and wants to know which original stars on the blueprint correspond to the current stars in the accessory. If two stars are connected by a thread in the current accessory, then the corresponding stars must also be connected by a thread on the original blueprint. Xiao Y wants to know how many possible correspondences there are.

Only if you tell her the correct answer will she give you the accessory as a gift.

Input Format

The first line contains 22 positive integers n,mn, m, representing the number of stars and the number of threads in the original accessory.

The next mm lines each contain 22 positive integers u,vu, v, indicating that in the original accessory, stars uu and vv are connected by a thread. The stars are numbered starting from 11. It is guaranteed that uvu\neq v, and there is at most one thread between each pair of stars.

The next n1n-1 lines each contain 22 positive integers u,vu, v, indicating that in the current accessory, stars uu and vv are connected by a thread. It is guaranteed that these stars are connected through the threads.

Output Format

Output a single line containing one integer, the number of possible correspondences.

If no feasible correspondence exists, output 0.

4 3
1 2
1 3
1 4
4 1
4 2
4 3
6

Hint

For 100%100\% of the testdata, n17n\leq 17, m12n(n1)m\leq \frac 12n(n-1).

Translated by ChatGPT 5