• SP104 Highways (矩阵树,高斯消元)


    矩阵树定理裸题

    //#include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 17;
    const double eps = 1e-8;
    
    int n;
    double a[N][N];
    inline void Gauss(){
    	--n;
    	R(i,1,n){
    		int r = i;
    		R(j,i + 1,n){
    			if(fabs(a[j][i]) > fabs(a[r][i])){
    				r = j;
    			}
    		}
    		if(fabs(a[r][i]) < eps){
    			printf("0
    ");
    			return;
    		}
    		if(r != i){
    			R(j,1,n)
    				swap(a[r][j], a[i][j]);
    		}
    		R(j,i + 1,n){
    			double t = a[j][i] / a[i][i];
    			R(k,i,n){
    				a[j][k] -= t * a[i][k];
    			}
    		}
    	}
    	double ans = 1;
    	R(i,1,n){
    		ans *= a[i][i];
    	}
    	printf("%.0f
    ", fabs(ans));
    }
    
    int main(){
    //FileOpen();
    	int Tasks;
    	io >> Tasks;
    	while(Tasks--){
    		int m;
    		io >> n >> m;
    		Fill(a, 0);
    		R(i,1,m){
    			int u, v;
    			io >> u >> v;
    			++a[u][u];
    			++a[v][v];
    			--a[u][v];
    			--a[v][u];
    		}
    		Gauss();
    	}
    	return 0;
    }
    

  • 相关阅读:
    js浅拷贝和深拷贝
    使用slice和concat对数组的深拷贝和浅拷贝
    JS数组常用方法---8、concat方法
    JS数组常用方法---7、join方法
    js中将类数组转换为数组的几种方法
    JS 使用const声明常量的本质(很多人都有误解)
    JS中对象数组按照对象的某个属性进行排序
    vue源码分析参考---2、数据代理
    vue源码分析参考---1、准备工作
    ES6课程---5、形参默认值
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11250192.html
Copyright © 2020-2023  润新知