luogu P6178 【模板】Matrix-Tree 定理

luogu P6178 【模板】Matrix-Tree 定理

1.无向图

假设现在给定一个图 G。

度数矩阵D:若存在边$ (x,y,z)(x,y,z)$ ,则 D [ x ] [ x ] + = z ; D [ y ] [ y ] + = z ; D [ x ] [ x ] + = z ; D [ y ] [ y ] + = z D[x][x]+=z;D[y][y]+=z;D[x][x]+=z;D[y][y]+=z ;

邻接矩阵C:若存在边 ( x , y , z ) ( x , y , z ) (x,y,z)(x,y,z) ,则 C [ x ] [ y ] + = z ; C [ y ] [ x ] + = z ; C [ x ] [ y ] + = z ; C [ y ] [ x ] + = z C[x][y]+=z;C[y][x]+=z;C[x][y]+=z;C[y][x]+=z ;

图G的基尔霍夫矩阵 A = D C A = D C A=D−CA=D−C

删去任意一行和任意一列,求剩下的矩阵行列式即可。

2.有向图

假设现在给定一个图G.

度数矩阵D:若存在边$ (x,y,z)(x,y,z) , ,则 外向树中 D[y][y]+=z;D[y][y]+=z$; 内向树中 D [ x ] [ x ] + = z ; D [ x ] [ x ] + = z D[x][x]+=z;D[x][x]+=z ;

邻接矩阵C:若存在边 ( x , y , z ) ( x , y , z ) (x,y,z)(x,y,z) ,则 内向树和外向树中均为 C [ x ] [ y ] + = z ; C [ x ] [ y ] + = z C[x][y]+=z;C[x][y]+=z ;

图G的基尔霍夫矩阵 A = D C A = D C A=D−CA=D−C

删去指定的根所在的行和列,求剩下的矩阵行列式即可。

代码

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define endl '\n'
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-')    f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f * x;
}

ll A[310][310];

const int mod = 1e9 + 7;

ll quick_pow(ll a, ll n, ll mod) {
    ll ans = 1;
    while(n) {
        if(n & 1) ans = ans * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ans;
}

ll inv(ll a, ll n) {
    return quick_pow(a, mod - 2, mod);
}

ll gauss(int n){
    ll ans = 1;
    for(int i = 2; i <= n; i++){
        for(int j = i; j <= n; j++) {
            if(A[j][i]){
                for(int k = i; k <= n; k++) swap(A[i][k], A[j][k]);
                if(i != j) ans = -ans;
                break;
            }
        }
        if(!A[i][i]) return 0;
        for(ll j = i + 1, iv = inv(A[i][i], mod); j <= n; j++) {
                ll t = A[j][i] * iv % mod;
                for(int k = i; k <= n; k++)
                    A[j][k] = (A[j][k] - t * A[i][k] % mod + mod) % mod;
        }
        ans = (ans * A[i][i] % mod + mod) % mod;
    }
    return ans;
}

void add(int x, int y, int w) {
    (A[x][y] -= w) %= mod;
    (A[y][y] += w) %= mod;
}

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int n = read(), m = read(), t = read();
    for(int i = 1; i <= m; i++) {
        int x = read(), y = read(), w = read();
        if(!t) {
            add(x, y, w);
            add(y, x, w);
        }
        else {
            add(x, y, w);
        }
    }
    printf("%lld\n", gauss(n));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45483201/article/details/107866536