zoukankan      html  css  js  c++  java
  • SPOJ104 Highways 【矩阵树定理】

    SPOJ104 Highways


    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


    矩阵树定理模板题,有一些细节:

    • 发现自由元立即切出去
    • 输出答案时向下取整(懵)

    然后就没了

    #include<bits/stdc++.h>
    using namespace std;
    #define N 3010 
    int n,m;
    double g[N][N];
    void gauss(){
        n--;
        for(int i=1;i<=n;i++){
            int r=i;
            for(int j=i+1;j<=n;j++)
                if(fabs(g[j][i])>fabs(g[r][i]))r=j;
            if(r!=i)for(int j=1;j<=n+1;j++)swap(g[i][j],g[r][j]);
            if(!g[i][i]){cout<<0<<endl;return;}
            for(int k=i+1;k<=n;k++){
                double f=g[k][i]/g[i][i];
                for(int j=i;j<=n+1;j++)g[k][j]-=f*g[i][j];
            }
        }
        double ans=1;
        for(int i=1;i<=n;i++)ans*=g[i][i];
        printf("%.0f
    ",abs(ans));//一定要加 向下取整 
    }
    int main(){
        int T;cin>>T;
        while(T--){
            memset(g,0,sizeof(g));
            cin>>n>>m;
            for(int i=1;i<=m;i++){
                int x,y;cin>>x>>y;
                g[x][x]++;g[y][y]++;
                g[x][y]--;g[y][x]--;
            }
            gauss();
        }
        return 0;
    }
  • 相关阅读:
    将小度WiFi改造为无线网卡(小度WiFi能够接收WiFi信号)
    百度8秒教育片
    MATLAB r2014a 下载+安装+激活
    lsmod
    ipython与python的区别
    ssh远程登录Ubuntu报错:Permission denied, please try again.
    windows系统安装ubuntu后,grub中没有windows启动项
    面向对象三大特征
    进程和线程
    tar 命令
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9676400.html
Copyright © 2011-2022 走看看