题目链接:点击查看
题目大意:给出一张 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 定理(矩阵树定理模板题)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。