【BZOJ】4894: 天赋

题解

这道题是求一个有向图的外向生成树

入度矩阵对应着外向生成树,出度矩阵对应着内向生成树,知道了这个就可以求出基尔霍夫矩阵了,同时n - 1阶主子式一定要删掉根节点的一行一列

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
//#define ivorysi
#define pb push_back
#define space putchar(' ')
#define enter putchar('
')
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mo 974711
#define RG register
#define MAXN 200005
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 + c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) putchar('-'),x = -x;
    if(x >= 10) out(x / 10);
    putchar('0' + x % 10);
}
const int MOD = 1000000007;
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
int inc(int a,int b) {
    a = a + b;
    if(a >= MOD) a -= MOD;
    return a;
}

int fpow(int x,int c) {
    int res = 1,t = x % MOD;
    while(c) {
	if(c & 1) res = mul(res,t);
	t = mul(t,t);
	c >>= 1;
    }
    return res;
}


int N,g[305][305],ind[305];
char s[305];
int Calc() {
    int res = 1;
    for(int i = 2 ; i <= N ; ++i) {
	int l = i;
	for(int j = i + 1; j <= N ; ++j) {
	    if(abs(g[j][i]) > abs(g[l][i])) l = j;
	}
	if(l != i) {
	    for(int j = i ; j <= N ; ++j) swap(g[l][j],g[i][j]);
	    res = -res;
	}
	for(int j = i + 1 ; j <= N ; ++j) {
	    int t = mul(g[j][i],fpow(g[i][i],MOD - 2));
	    for(int k = i ; k <= N ; ++k) {
		g[j][k] = inc(g[j][k],MOD - mul(t,g[i][k]));
	    }
	}
    }
    if(res < 0) res = MOD - 1;
    for(int i = 2 ; i <= N ; ++i) {
	res = mul(res,g[i][i]);
    }
    return res;
}
void Solve() {
    read(N);
    for(int i = 1 ; i <= N ; ++i) {
	scanf("%s",s + 1);
	for(int j = 1 ; j <= N ; ++j) {
	    g[i][j] = s[j] - '0';
	    if(g[i][j]) {
		++ind[j];
		g[i][j] = MOD - 1;
	    }
	}
    }
    for(int i = 1 ; i <= N ; ++i) g[i][i] = ind[i];
    out(Calc());enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
原文地址:https://www.cnblogs.com/ivorysi/p/9116360.html