首页 > 其他 > 详细

SPOJ-HIGH

时间:2017-01-25 12:34:04      阅读:354      评论:0      收藏:0      [点我收藏+]

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

就是说一个无向图,求生成树个数
矩阵树定理
技术分享
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define eps 1e-9
#define N 20
int n,m;
int d[N][N],c[N][N];
double a[N][N];
void gauss()
{
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n+1;j++)
        {
            if(j==n+1)
            {
                printf("0\n");
                return;
            }
            if(fabs(a[j][i])>eps)
            {
                for(int k=i;k<=n;k++) swap(a[j][k],a[i][k]);
                break;
            }
        }
        for(int j=i+1;j<=n;j++)
        {
            double t=a[j][i]/a[i][i];
            for(int k=1;k<=n;k++) a[j][k]=a[j][k]-t*a[i][k];
        }
    }
    double ans=1;
    for(int i=1;i<=n;i++) ans*=a[i][i];
    printf("%.0lf\n",abs(ans));
}
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&m);
        n--;
        for(int i=1;i<=m;i++)
        {
            int u,v; scanf("%d%d",&u,&v);
            d[u][u]++; d[v][v]++;
            c[u][v]++; c[v][u]++;
        }
        for(int i=1;i<=n;i++) 
            for(int j=1;j<=n;j++) a[i][j]=d[i][j]-c[i][j];
        gauss();
    }
    return 0;
}
View Code

 

SPOJ-HIGH

原文:http://www.cnblogs.com/19992147orz/p/6349242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!