zoukankan      html  css  js  c++  java
  • 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;
    }
    

  • 相关阅读:
    Codeforces Round #384 (Div. 2) 解题报告
    Codeforces Round #383 (Div. 2) 解题报告
    (DFS、bitset)AOJ-0525 Osenbei
    (DFS、全排列)POJ-3187 Backward Digit Sums
    自考新教材-p169
    自考新教材-p167
    自考新教材-p166
    自考新教材-p165
    自考新教材-p161
    自考新教材-p159
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11250192.html
Copyright © 2011-2022 走看看