題目鏈接:點擊查看
題目大意:給出一張 n 個點 m 條邊組成的圖,可能是有向圖也可能是無向圖,定義生成樹的權值為所有邊權的乘積:
如果是無向圖,求所有生成樹的權值之和如果是有向圖,求所有以點 1 為根的外向樹的生成樹權值之和
題目分析:在有向圖中是要求以點 1 為根的外向樹,所有可以直接刪掉第一行和第一列求解,有向圖的外向樹是需要維護入度,這個別弄混了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=310;const int mod=1e9+7;LL a[N][N];LL q_pow(LL a,LL b)
{LL ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}return ans;
}LL Gauss(int n)
{LL ans=1;for(int i=2;i<=n;i++){for(int j=i+1;j<=n;j++)if(!a[i][i]&&a[j][i]) {ans=-ans;swap(a[i],a[j]);break;}LL inv=q_pow(a[i][i],mod-2);for(int j=i+1;j<=n;j++){LL temp=a[j][i]*inv%mod;for(int k=i;k<=n;k++)a[j][k]=(a[j][k]-a[i][k]*temp%mod+mod)%mod;}ans=ans*a[i][i]%mod;}return ans;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int n,m,t;scanf("%d%d%d",&n,&m,&t);while(m--){int x,y,w;scanf("%d%d%d",&x,&y,&w);if(!t){a[x][x]=(a[x][x]+w)%mod;a[y][y]=(a[y][y]+w)%mod;a[x][y]=(a[x][y]-w+mod)%mod;a[y][x]=(a[y][x]-w+mod)%mod;}else{a[y][y]=(a[y][y]+w)%mod;a[x][y]=(a[x][y]-w+mod)%mod;}}printf("%lld\n",Gauss(n));return 0;
}
?
總結
以上是生活随笔為你收集整理的洛谷 - P6178 【模板】Matrix-Tree 定理(矩阵树定理模板题)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。