SP104高速道路(行列の木、ガウスの消去)

行列木定理裸のタイトル

//#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("\n\n----------\n\n")
#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\n");
            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\n", 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;
}

おすすめ

転載: www.cnblogs.com/bingoyes/p/11250192.html