題目鏈接:點擊查看
題目大意:給出一張由 n 個點和 m 條邊組成的無向圖,對于任意一個生成樹,其權值為 n - 1 條邊的邊權進行二進制的 and 運算,現在需要在圖中任意選擇一個生成樹,問期望權值是多少
題目分析:需要對題意進行轉換,根據期望的公式 E( aX + bY ) = aE( X ) + bE( Y ) ,又因為 and 運算對于每一位都是相互獨立的,所以拆位然后單獨討論
首先求出來 sum 為原圖中有多少個生成樹,對于二進制的每一位 i 來說,令所有第 i 位為 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=110;const int mod=998244353;struct Edge
{int u,v,w;
}edge[N*N];int n,m;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;
}LL cal(int bit)
{memset(a,0,sizeof(a));for(int i=1;i<=m;i++){if(bit==-1||(edge[i].w>>bit)&1){int x=edge[i].u,y=edge[i].v;a[x][x]=(a[x][x]+1)%mod;a[y][y]=(a[y][y]+1)%mod;a[x][y]=(a[x][y]-1+mod)%mod;a[y][x]=(a[y][x]-1+mod)%mod;}}return Gauss(n);
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){scanf("%d%d",&n,&m);for(int i=1;i<=m;i++)scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);LL sum=q_pow(cal(-1),mod-2);LL ans=0;for(int i=0;i<=30;i++)ans=(ans+cal(i)*sum%mod*(1<<i)%mod)%mod;printf("%lld\n",ans);}return 0;
}
?
總結
以上是生活随笔為你收集整理的HDU多校6 - 6836 Expectation(矩阵树定理+高斯消元求行列式)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。