SPOJ HIGH Highways(生成树计数,Matrix-Tree定理)

Time Limit: 7000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu

 Status

Description

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be connected directly. Your task is to count how many ways there are to build such a network that between every two cities there exists exactly one path. Two networks differ if there are two cities that are connected directly in the first case and aren't in the second case. At most one highway connects two cities. No highway connects a city to itself. Highways are two-way.

Input

The input begins with the integer t, the number of test cases (equal to about 1000). Then t test cases follow. The first line of each test case contains two integers, the number of cities (1<=n<=12) and the number of direct connections between them. Each next line contains two integers a and b, which are numbers of cities that can be connected. Cities are numbered from 1 to n. Consecutive test cases are separated with one blank line.

Output

The number of ways to build the network, for every test case in a separate line. Assume that when there is only one city, the answer should be 1. The answer will fit in a signed 64-bit integer.

Example

Sample input:
4
4 5
3 4
4 2
2 3
1 2
1 3

2 1
2 1

1 0

3 3
1 2
2 3
3 1

Sample output:
8
1
1
3




题意:

高速公路(SPOJ p104 Highways)
 一个有n座城市的组成国家,城市1至n编号,其中一些城市之间可以修建高速公路。现在,需要有选择的修建一些高速公路,从而组成一个交通网络。你的任务是计算有多少种方案,使得任意两座城市之间恰好只有一条路径?
数据规模:1≤n≤12。


题解:

生成树计数问题

Matrix-Tree定理(Kirchhoff矩阵-树定理)。Matrix-Tree定理是解决生成树计数问题最有力的武器之一。它首先于1847年被Kirchhoff证明。在介绍定理之前,我们首先明确几个概念:
1、G的度数矩阵D[G]是一个n*n的矩阵,并且满足:当i≠j时,dij=0;当i=j时,dij等于vi的度数。
2、G的邻接矩阵A[G]也是一个n*n的矩阵, 并且满足:如果vi、vj之间有边直接相连,则aij=1,否则为0。
我们定义G的Kirchhoff矩阵(也称为拉普拉斯算子)C[G]为C[G]=D[G]-A[G],则Matrix-Tree定理可以描述为:G的所有不同的生成树的个数等于其Kirchhoff矩阵C[G]任何一个n-1阶主子式的行列式的绝对值。所谓n-1阶主子式,就是对于r(1≤r≤n),将C[G]的第r行、第r列同时去掉后得到的新矩阵,用Cr[G]表示。


可用的高斯消元板子:(A数组存Kirchhoff矩阵)

bool isFreeX[MAXN];
LL A[MAXN][MAXN];       //存储矩阵,下标都从0开始 
LL gauss(int n, int m) {//求行列式
    for(int i = 0; i < m; ++i) isFreeX[i] = false;
    LL ret = 1, neg = 0;
    int r = 1, c = 1;//求n-1阶的行列式,去掉第一阶,所以从1开始
    for(; r < n && c < m; ++r, ++c) {
        int p = r;
        for(; p < n; ++p) if(A[p][c]) break;
        if(p == n) {--r; isFreeX[c] = true; continue;}
        if(p != r) {
            neg ^= 1;
            for(int i = c; i <= m; ++i) swap(A[p][i], A[r][i]);
        }
        //eliminate coefficient
        for(int i = r + 1; i < n; ++i) {
            while(A[i][c]) {
                LL delta = A[i][c] / A[r][c];
                for(int j = c; j <= m; ++j) {
                    A[i][j] += mod - delta * A[r][j] % mod;
                    A[i][j] %= mod;
                }
                if(!A[i][c]) break;
                neg ^= 1;
                for(int j = c; j <= m; ++j) swap(A[r][j], A[i][j]);
            }
        }
    }
    if(r != n) return 0;
    //0-r-1求n阶行列式,1-r-1求n-1阶行列式
    for(int i = 1; i < r; ++i) ret = ret * A[i][i] % mod;
    if(neg) ret = (-ret + mod) % mod;
    return ret;
}



AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=20;
ll G[MAXN][MAXN],C[MAXN][MAXN];
bool isFreeX[MAXN];
ll A[MAXN][MAXN];       //存储矩阵,下标都从0开始 
ll ab(ll x)
{
	return x>0? x:-x;
}
ll gauss(int n, int m) {//求行列式
    for(int i = 0; i < m; ++i) isFreeX[i] = false;
    ll ret = 1, neg = 0;
    int r = 1, c = 1;//求n-1阶的行列式,去掉第一阶,所以从1开始
    for(; r < n && c < m; ++r, ++c) {
        int p = r;
        for(; p < n; ++p) if(A[p][c]) break;
        if(p == n) {--r; isFreeX[c] = true; continue;}
        if(p != r) {
            neg ^= 1;
            for(int i = c; i <= m; ++i) swap(A[p][i], A[r][i]);
        }
        //eliminate coefficient
        for(int i = r + 1; i < n; ++i) {
            while(A[i][c]) {
                ll delta = A[i][c] / A[r][c];
                for(int j = c; j <= m; ++j) {
                    A[i][j] += - delta * A[r][j];
                }
                if(!A[i][c]) break;
                neg ^= 1;
                for(int j = c; j <= m; ++j) swap(A[r][j], A[i][j]);
            }
        }
    }
    if(r != n) return 0;
    //0-r-1求n阶行列式,1-r-1求n-1阶行列式
    for(int i = 1; i < r; ++i) ret = ret * A[i][i];
    return ab(ret);
}

int main()
{
	int cas;
	scanf("%d",&cas);
	while(cas--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		memset(G,0,sizeof(G));
		memset(C,0,sizeof(C));
		while(m--)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			u--,v--;
			G[u][v]=G[v][u]=1;
			C[u][u]++,C[v][v]++;
		}
		for(int i=0;i<n;i++)
		 for(int j=0;j<n;j++)
		 A[i][j]=C[i][j]-G[i][j];
		ll ans=gauss(n,n);
		printf("%lld\n",ans);
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值