传送门:HDU - 6836
思路分析
先计算出总的生成树个数,再按位计算权值和
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 0x3f3f3f3f;
const int N = 111;
const int M = 1e4+10;
const int mod = 998244353;
#define int long long
int n,m;
int a[M],b[M],c[M],g[N][N];
ll qpow(ll x,int n) {
ll res=1;
while(n) {
if(n&1) res = res*x%mod;
x=x*x%mod;
n/=2;
}
return res;
}
void add(int u,int v) {
g[u][u]++;
g[v][v]++;
g[u][v]--;
g[v][u]--;
}
int gauss() {
int res=1;
for(int i=1; i<n; i++) {
for(int j=i+1; j<n; j++) {
while(g[j][i]) {
int t=g[i][i]/g[j][i];
for(int k=i; k<n; k++) g[i][k]=(g[i][k]-t*g[j][k]+mod)%mod;
swap(g[i],g[j]);
res=-res;
}
}
res=(res*g[i][i])%mod;
}
return (res+mod)%mod;
}
signed main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
int T;
read(T);
while(T--) {
mem(g,0);
read(n,m);
for(int i=1; i<=m; i++) {
read(a[i],b[i],c[i]);
add(a[i],b[i]);
}
ll tot = gauss();
ll ans = 0;
for(int i=0;i<=31;i++){
mem(g,0);
for(int j=1;j<=m;j++){
if((c[j]>>i)&1LL) add(a[j],b[j]);
}
ans = (ans + gauss()*(1LL<<i))%mod;
}
ans = ans * qpow(tot,mod-2) %mod;
printf("%lld\n",ans);
}
return 0;
}