• SPOJ HIGH Highways


      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
    

    图论 生成树 Matrix-Tree定理

    按照给出的边关系建立邻接矩阵和度数矩阵,直接套用矩阵树定理。

    给出的图可能不连通……无解时候要输出0

    ↑天真的我压根儿没想到无解的情况,WA了一串

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<queue>
     6 #include<cmath>
     7 using namespace std;
     8 const double eps=1e-3;
     9 const int mxn=20;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10-'0'+ch;ch=getchar();}
    14     return x*f;
    15 }
    16 double G[mxn][mxn],A[mxn][mxn];
    17 int n,m;
    18 void Solve(){
    19     int i,j;
    20     double ans=1;
    21     for(i=1;i<n;i++){
    22         int p=i;
    23         if(fabs(G[i][i])<eps){
    24             for(j=i+1;j<n;j++){
    25                 if(fabs(G[j][i])>eps)p=j;
    26             }
    27             if(p==i){
    28                 printf("0
    ");return;
    29             }
    30             for(j=1;j<n;j++)swap(G[i][j],G[p][j]);
    31             ans=-ans;
    32         }
    33         for(j=i+1;j<n;j++){
    34             double c=G[j][i]/G[i][i];
    35             for(int k=i;k<n;k++){
    36                 G[j][k]-=c*G[i][k];
    37             }
    38         }
    39         ans*=G[i][i];
    40     }
    41     printf("%.0f
    ",ans);
    42     return;
    43 }
    44 int main(){
    45     int i,j,u,v;
    46     int T;
    47     scanf("%d",&T);
    48     while(T--){
    49         memset(G,0,sizeof G);
    50         memset(A,0,sizeof A);
    51         scanf("%d%d",&n,&m);
    52         for(i=1;i<=m;i++){
    53             scanf("%d%d",&u,&v);
    54             ++G[u][u];
    55             ++G[v][v];
    56             ++A[u][v];++A[v][u];
    57         }
    58         for(i=1;i<=n;i++)
    59             for(j=1;j<=n;j++){
    60                 G[i][j]-=A[i][j];
    61         }
    62         Solve();
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    【工业控制】What is a Waveform
    【工业控制】什么是波形
    【工业控制】学习喷墨打印技术 怎么能不知道波形
    【工业控制】UV打印机喷头波形和墨水关系
    VMware-workstation-full序列码
    【Centos】Centos7.5取消自动锁屏功能
    微信公众平台开发入门教程(图文)
    微信公众平台开发(34)微相册
    微信公众平台开发(33)在线点歌/在线音乐
    微信公众平台开发(32)快递物流
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6408096.html
Copyright © 2020-2023  润新知