zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    HDU1879 kruscal 继续畅通工程
    poj1094 拓扑 Sorting It All Out
    (转)搞ACM的你伤不起
    (转)女生应该找一个玩ACM的男生
    poj3259 bellman——ford Wormholes解绝负权问题
    poj2253 最短路 floyd Frogger
    Leetcode 42. Trapping Rain Water
    Leetcode 41. First Missing Positive
    Leetcode 4. Median of Two Sorted Arrays(二分)
    Codeforces:Good Bye 2018(题解)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6408096.html
Copyright © 2011-2022 走看看