spoj104 HIGH - Highways [Matrix-Tree定理]

in 题目 with 0 comment

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.


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


Solution

生成树计数问题,可以用matrix-tree定理直接求解(模板题)

matrix-tree定理简述:
令E(G)表示图G的邻接矩阵,若$i\rightarrow j$连通,则$E[i][j]=1$
令D(G)表示图G的度数矩阵,对角线处元素值为该点的度数,其余位置为0
令M(G)=D(G)-E(G),G的生成树个数等于M的任一n-1阶主子式的行列式(任意去除一行一列)


Code

#include<bits/stdc++.h>
#define maxn 15
#define eps 1e-6
// #define DEBUG
using namespace std;
int cnt,n,m;
double a[maxn][maxn];

inline int dcmp(double x)
{
  if(abs(x)<eps) return 0;
  else return x>0?1:-1;
}

inline int read()
{
  char ch;
  int sign=1,read=0;
  do
    ch=getchar();
  while((ch<'0'||ch>'9')&&ch!='-');
  if(ch=='-') sign=-1,ch=getchar();
  while(ch>='0'&&ch<='9')
  {
    read=read*10+ch-'0';
    ch=getchar();
  }
  return read*sign;
}

void init()
{
  memset(a,0,sizeof(a));
  n=read(),m=read();
  if (n == 1) return;
  for(int i=1;i<=m;++i)
  {
    int u=read(),v=read();
    a[u][v]=a[v][u]=-1;
    a[u][u]++,a[v][v]++;
  }
#ifdef DEBUG
  for(int i=1;i<=n;++i){
    for(int j=1;j<=n;++j)
      cout<<a[i][j]<<" ";
    cout<<endl;}
#endif
  n--;
}

void gauss()
{
  if(n==1)
  {
    printf("1\n");
    return;
  }
  for(int i=1;i<=n;++i)
  {
    if(!dcmp(a[i][i]))
    {
      int k=i;
      while(!dcmp(a[k][k])) k++;
      swap(a[k],a[i]);
    }
    for(int j=i+1;j<=n;++j)
    {
      for(int k=i+1;k<=n;++k)
        a[j][k]-=a[i][k]*(a[j][i]/a[i][i]);
      a[j][i]=0;
    }
  }
  double ans=1;
  for(int i=1;i<=n;++i) ans*=a[i][i];
  printf("%.0lf\n",ans);
}

int main()
{
  cnt=read();
  while(cnt--)
  {
    init();
    gauss();
  }
  return 0;
}

/*
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
*/

扫描二维码,在手机上阅读!
Responses