luogu#P4336. [SHOI2016] 黑暗前的幻想乡

    ID: 3297 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2016各省省选矩阵树定理上海生成树容斥原理

[SHOI2016] 黑暗前的幻想乡

Background

The quadrennial Gensokyo election has begun. Recently, the biggest problem in Gensokyo is that many mysterious youkai have flooded in, disrupting its former order. However, the establishment youkai (and humans) such as Reimu Hakurei and Yukari Yakumo keep talking about equality for all youkai and diversity in Gensokyo, while offering no reasonable solutions to the pressing issues Gensokyo now faces.

Yuuka Kazami is one of the few great youkai in Gensokyo who realized the severity of the problem. She bravely stepped up to run in the election, proposing a series of plans such as building a wall on Gensokyo’s border (and making humans pay for it) and vigorously promoting infrastructure to reduce unemployment. She became the unexpected dark horse of the election year and successfully became the leader of Gensokyo.

Problem Description

After taking office, Yuuka’s first measure is to build Gensokyo’s highways. There are nn cities in Gensokyo, and initially there are no roads. Yuuka promised voters to cut taxes, so she plans to build only n1n - 1 roads to connect all these cities. There are exactly n1n - 1 construction companies, and each company wants to gain some benefits during road construction. Although these companies did not give Yuuka money before the election, she still intends to maintain good relations with them, since she is counting on them to help her build the wall. So she plans to have each construction company be responsible for exactly one road.

Each construction company has told Yuuka between which pairs of cities it is capable of building a road. Yuuka will choose n1n - 1 edges that can connect all cities in Gensokyo, then assign each edge to a construction company that can build it, with each company building exactly one edge.

Yuuka now wants to know how many possible schemes there are in total. Two schemes are different if and only if either the set of edges to be built is different, or the assignment of companies to edges is different.

Input Format

The first line contains an integer nn, the number of cities.

Lines 22 through nn: line i+1i + 1 describes the list of roads that the ii-th construction company can build. It starts with a non-negative integer mim_i, the number of roads it can build; then follow mim_i pairs of integers u,vu, v, each pair denoting the endpoints of an edge. Within a company’s list, there are no duplicate edges and no self-loops.

Output Format

Output a single integer on one line: the number of all possible schemes modulo 109+710^9+7.

4
2 3 2 4 2
5 2 1 3 1 3 2 4 1 4 3
4 2 1 3 2 4 1 4 2
17

Hint

Constraints

  • For 20%20\% of the test points, n5n \le 5.
  • For 50%50\% of the test points, n8n \le 8.
  • For 60%60\% of the test points, n10n \le 10.
  • For 100%100\% of the test points, 2n172 \leq n \le 17, 0min(n1)20 \leq m_i \leq \frac{n(n - 1)}{2}, 1u,vn1 \leq u, v \leq n.

Translated by ChatGPT 5