zoukankan      html  css  js  c++  java
  • SPOJ-HIGH

    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
    ");
                    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
    ",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
  • 相关阅读:
    阿里云应用开发
    [New Portal]Windows Azure Virtual Machine (7) 删除Virtual Machine
    Windows Azure Affinity Groups (2) 创建Windows Azure Affinity Groups 地缘组
    [New Portal]Windows Azure Virtual Machine (8) Virtual Machine高可用(上)
    [New Portal]Windows Azure Virtual Machine (6) 指定Virtual Machine存储账户
    [New Portal]Windows Azure Storage (13) 本地冗余存储 vs 地理冗余存储 (下)
    [New Portal]Windows Azure Virtual Machine (9) Virtual Machine高可用与自动负载均衡(下)
    树控件的整理
    很好用的数据恢复软件
    JS日历控件
  • 原文地址:https://www.cnblogs.com/19992147orz/p/6349242.html
Copyright © 2011-2022 走看看