BZOJ5297 [Cqoi2018]社交網絡 【矩陣樹定理】


題目鏈接

BZOJ5297

題解

最近這玩意這么那么火
這題要用到有向圖的矩陣樹定理
主對角線上對應入度
剩余位置如果有邊則為\(-1\),不然為\(0\)

\(M_{i,i}\)即為以\(i\)為根的有向圖生成樹個數

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 255,maxm = 100005,INF = 1000000000,P = 10007;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int qpow(int a,int b){
    int ans = 1;
    for (; b; b >>= 1,a = a * a % P)
        if (b & 1) ans = ans * a % P;
    return ans;
}
int inv(int x){return qpow(x,P - 2);}
int A[maxn][maxn],n,m;
int gause(){
    int rev = 1;
    for (int i = 2; i <= n; i++){
        int j = i;
        for (int k = i + 1; k <= n; k++)
            if (abs(A[k][i]) > abs(A[j][i]))
                j = k;
        if (j != i){
            for (int k = i; k <= n; k++) swap(A[i][k],A[j][k]);
            rev = -rev;
        }
        for (j = i + 1; j <= n; j++){
            int t = A[j][i] * inv(A[i][i]) % P;
            for (int k = i; k <= n; k++){
                A[j][k] = ((A[j][k] - A[i][k] * t % P) % P + P) % P;
            }
        }
    }
    int re = 1;
    for (int i = 2; i <= n; i++)
        re = re * A[i][i] % P;
    re = (re * rev % P + P) % P;
    return re;
}
int main(){
    n = read(); m = read();
    int a,b;
    while (m--){
        a = read(); b = read();
        if (a == b) continue;
        A[b][a] = -1;
        A[a][a]++;
    }
    printf("%d\n",gause());
    return 0;
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com