jzoj5899 资源运输 矩阵树定理
Description
Solution
这真的是noip难度吗(;′⌒`)
考试的时候漏掉这个大裸题了。我们知道矩阵树定理可以求出生成树的方案,变元矩阵树定理可以求出所有生成树边权乘积的和。这两个除一下就是答案了
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define copy(x,t) memcpy(x,t,sizeof(x))
typedef long long LL;
const int MOD=998244353;
const int N=305;
LL a[N][N],b[N][N],c[N][N];
LL ksm(LL x,LL dep) {
LL ret=1;
for (;dep;dep>>=1) {
(dep&1)?(ret=ret*x%MOD):0;
x=x*x%MOD;
}
return ret;
}
int det(int n) {
rep(i,1,n) rep(j,1,n) a[i][j]=(a[i][j]%MOD+MOD)%MOD;
LL ret=1;
rep(i,1,n) {
rep(j,i+1,n) {
while (a[j][i]) {
int tmp=a[i][i]/a[j][i];
rep(k,i,n) a[i][k]=(a[i][k]-a[j][k]*tmp%MOD+MOD)%MOD;
std:: swap(a[i],a[j]); ret=-ret;
}
}
ret=ret*a[i][i]%MOD;
}
return (ret%MOD+MOD)%MOD;
}
int main(void) {
int n,m; LL ans=0; scanf("%d%d",&n,&m);
rep(i,1,m) {
int x,y,w; scanf("%d%d%d",&x,&y,&w);
b[x][y]-=w; b[y][x]-=w;
b[x][x]+=w; b[y][y]+=w;
c[x][y]-=1; c[y][x]-=1;
c[x][x]+=1; c[y][y]+=1;
}
copy(a,c);
ans=det(n-1);
copy(a,b);
ans=det(n-1)*ksm(ans,MOD-2)%MOD;
printf("%lld\n", ans);
return 0;
}