zoukankan      html  css  js  c++  java
  • SPOJ

    Highways

    题目链接:https://vjudge.net/problem/SPOJ-HIGH

    Description:

    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.

    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

    题意:

    给出n个点,以及m条无向边,问生成树的个数。

    题解:

    生成树计数模板题。。

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    const int N = 20;
    int t;
    int n,m;
    ll b[N][N];
    int g[N][N];
    ll Det(int n){
        int i,j,k;
        ll ret = 1;
        for(i=2;i<=n;i++){
            for(j = i+1;j <= n;j++){
                while(b[j][i]){
                    ll tmp=b[i][i]/b[j][i];//不存在除不尽的情况
                    for(k = i;k <= n;k++)
                        b[i][k] -= tmp*b[j][k];
                    for(k=i;k<=n;k++)
                        swap(b[i][k],b[j][k]);
                    ret = -ret;
                }
            }
            if(!b[i][i]) return 0;
            ret *= b[i][i];
        }
        if(ret < 0) ret = -ret;
        return ret;
    }
    int main(){
        cin>>t;
        while(t--){
            scanf("%d%d",&n,&m);
            memset(b,0,sizeof(b));
            memset(g,0,sizeof(g));
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                g[u][v]=g[v][u]=1;
            }
            for(int i=1;i<=n;i++){
                for(int j=i;j<=n;j++){
                    if(g[i][j]){
                        b[i][i]++;b[j][j]++;
                        b[i][j]=b[j][i]=-1;
                    }
                }
            }
            cout<<Det(n)<<endl;
        }
        return 0;
    }
  • 相关阅读:
    A1049. 命题逻辑
    矩形面积交:输出0.00
    完美的代价
    枚举孪生素数对
    改变参数的两种方法
    二面准备:React、Typescript、其他基础补充
    【TypeScript】基础及问题汇总
    【React】做一个百万答题小项目
    【React】相关题目总结
    【React】半小时深刻理解《半小时深刻理解React》(老套娃了)
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10392873.html
Copyright © 2011-2022 走看看