题面
题解
如果没有建筑公司的限制,那么就是个\(\mathrm{Matrix\;tree}\)板子
其实有了也一样
发现\(n\leq 17\),考虑容斥
每次钦定一些建筑公司,计算它们包含的边的生成树的方案数
复杂度\(\mathrm{O}(2^nn^3)\)
代码
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != '-' && (!isdigit(ch))) ch = getchar();
if(ch == '-') w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(20), Mod(1e9 + 7);
int n, a[N][N], m[N], popcnt[1 << 17], ans;
int U[N][N * N], V[N][N * N];
int solve(int S)
{
for(RG int i = 1; i <= n; i++)
for(RG int j = 1; j <= n; j++)
a[i][j] = 0;
for(RG int i = 1; i < n; i++)
if(S & (1 << (i - 1))) for(RG int j = 1; j <= m[i]; j++)
{
int x = U[i][j], y = V[i][j];
++a[x][x], ++a[y][y], --a[y][x], --a[x][y];
}
for(RG int i = 1; i <= n; i++)
for(RG int j = 1; j <= n; j++)
a[i][j] = (a[i][j] + Mod) % Mod;
int ans = 1;
for(RG int i = 2; i <= n; i++)
{
for(RG int j = i + 1; j <= n; j++)
while(a[j][i])
{
int t = a[i][i] / a[j][i];
for(RG int k = i; k <= n; k++)
a[i][k] = (a[i][k] - 1ll * a[j][k] * t % Mod + Mod) % Mod,
std::swap(a[i][k], a[j][k]);
ans = Mod - ans;
}
ans = 1ll * ans * a[i][i] % Mod;
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read();
for(RG int i = 1; i < n; i++)
{
m[i] = read();
for(RG int j = 1; j <= m[i]; j++)
U[i][j] = read(), V[i][j] = read();
}
for(RG int i = 0; i < 1 << (n - 1); i++)
popcnt[i] = popcnt[i >> 1] + (i & 1);
for(RG int i = 0; i < 1 << (n - 1); i++)
ans = (ans + (((n - 1 - popcnt[i]) & 1) ?
Mod - solve(i) : solve(i))) % Mod;
printf("%d\n", ans);
return 0;
}