zoukankan      html  css  js  c++  java
  • 算法复习——矩阵树定理(spoj104)

    题目:

    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

    题解

      矩阵树定理的模板题

      关于矩阵数定理的证明估计在我碰线性代数前是不会了解的··而且证明太麻烦了估计学了线性代数也不会去学证明2333

      但是结论很好背啊····

      对于求解无向图的生成树方案树问题,我们构造一个矩阵,对角线map[i,i]为点i的度数,如果i,j连边的话map[i,j]和map[j,i]都减1(考虑到重边的情况,没有重边直接就是-1),然后去掉矩阵   最后一行最后一列,将矩阵剩余部分进行高斯消元,最后对角线乘积的绝对值就是答案了····

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<cstring>
    #include<string>
    #include<algorithm>
    using namespace std;
    const int N=20;
    int T,n,m;
    double map[N][N],ans;
    inline int R()
    {
      char c;int f=0;
      for(c=getchar();c<'0'||c>'9';c=getchar());
      for(;c<='9'&&c>='0';c=getchar())
        f=(f<<3)+(f<<1)+c-'0';
      return f;
    }
    inline void solve()
    {
      for(int i=1;i<=n;i++)  
      {
        bool flag=false;
        if(!map[i][i])
        {
          for(int j=i+1;j<=n;j++)
            if(map[j][i])
            {  
              flag=true;
              for(int k=i;k<=n;k++)
                swap(map[i][k],map[j][k]);
            }
          if(!flag)  {ans=0;return;}
        }
        for(int j=i+1;j<=n;j++)
        {
          double temp=map[j][i]/map[i][i];
          for(int k=i;k<=n;k++)
            map[j][k]-=temp*map[i][k];
        }
      }
      for(int i=1;i<=n;i++)
        ans*=map[i][i];
      ans=(ans<0?-ans:ans);
    }
    int main()
    {
      //freopen("a.in","r",stdin);
      T=R();  
      while(T--)
      {  
        memset(map,0,sizeof(map));ans=1.0;
        n=R(),m=R();int a,b;
        if(n==1)  
        {  
          cout<<"1"<<endl;
          continue;
        }
        n--;
        for(int i=1;i<=m;i++)
        {
          a=R(),b=R();
          map[a][a]++,map[b][b]++;
          map[a][b]--,map[b][a]--;
        }
        solve();
        printf("%.0lf
    ",ans);
        scanf("
    ");
      }
      return 0;
    }






  • 相关阅读:
    触发器
    数据库一键退出脚本
    集合
    Android 自定义控件之继承ViewGroup创建新容器
    web学习测试环境
    ref:ubuntu下如何批量修改文件后缀名
    ref:Adding AFL Bloom Filter to Domato for Fun
    ref:phpstorm配置远程调试(xdebug)(docker中)
    ref:PHP反序列化漏洞成因及漏洞挖掘技巧与案例
    ref:【干货分享】PHP漏洞挖掘——进阶篇
  • 原文地址:https://www.cnblogs.com/AseanA/p/7597727.html
Copyright © 2011-2022 走看看