跳到正文

剪贴板

剪贴板 9lychgxv

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
    if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
    if(x > y) x = y;
}

inline int popcnt(int x){
    return __builtin_popcount(x);
}

inline int ctz(int x){
    return __builtin_ctz(x);
}

template <int P>
class mod_int
{
    using Z = mod_int;

private:
    static int mo(int x) { return x < 0 ? x + P : x; }

public:
    int x;
    int val() const { return x; }
    mod_int() : x(0) {}
    template <class T>
    mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
    bool operator==(const Z &rhs) const { return x == rhs.x; }
    bool operator!=(const Z &rhs) const { return x != rhs.x; }
    Z operator-() const { return Z(x ? P - x : 0); }
    Z pow(long long k) const
    {
        Z res = 1, t = *this;
        while (k)
        {
            if (k & 1)
                res *= t;
            if (k >>= 1)
                t *= t;
        }
        return res;
    }
    Z &operator++()
    {
        x < P - 1 ? ++x : x = 0;
        return *this;
    }
    Z &operator--()
    {
        x ? --x : x = P - 1;
        return *this;
    }
    Z operator++(int)
    {
        Z ret = x;
        x < P - 1 ? ++x : x = 0;
        return ret;
    }
    Z operator--(int)
    {
        Z ret = x;
        x ? --x : x = P - 1;
        return ret;
    }
    Z inv() const { return pow(P - 2); }
    Z &operator+=(const Z &rhs)
    {
        (x += rhs.x) >= P && (x -= P);
        return *this;
    }
    Z &operator-=(const Z &rhs)
    {
        (x -= rhs.x) < 0 && (x += P);
        return *this;
    }
    Z operator-()
    {
        return -x;
    }
    Z &operator*=(const Z &rhs)
    {
        x = 1ULL * x * rhs.x % P;
        return *this;
    }
    Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                  \
    friend T operator o(const Z &lhs, const Z &rhs) \
    {                                               \
        Z res = lhs;                                \
        return res o## = rhs;                       \
    }
    setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO

    friend istream& operator>>(istream& is, mod_int& x)
    {
        long long tmp;
        is >> tmp;
        x = tmp;
        return is;
    }
    friend ostream& operator<<(ostream& os, const mod_int& x)
    {
        os << x.val();
        return os;
    }
};

using Z = mod_int<mod>;
Z power(Z p,ll k){
    Z ans = 1;
    while(k){
        if(k % 2 == 1) ans *= p;
        p *= p;
        k /= 2;
    }
    return ans;
}
int testid,TEST;
int n,m;
int msk[25],sum[1 << 15];
Z P[505],iP[505];

int lowbit(int x){
	return x & (-x);
}
int cross(int S,int T){
	assert((S & T) == 0);
	return sum[S + T] - sum[S] - sum[T];
}

Z f[1 << 15];

Z ans[1 << 15],cof[1 << 15],dp[1 << 15];
void solve(){
	scanf("%d%d",&n,&m);
	P[0] = 1;
	rep(i,1,n * n) P[i] = P[i - 1] * 2;
	rep(i,0,n * n) iP[i] = Z(1) / P[i];
	fill(msk,msk + n,0);
	rep(i,1,m){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		u--;v--;
//		assert(w == 1);
		msk[u] |= 1 << v;msk[v] |= 1 << u;
	}

	rep(S,1,(1 << n) - 1){
		int u = ctz(S);
		sum[S] = sum[S - (1 << u)] + popcnt(msk[u] & S);
	}
	rep(S,0,(1 << n) - 1) ans[S] = cof[S] = dp[S] = 0;
	cof[0] = -1;dp[0] = 1;
	rep(S,1,(1 << n) - 1){
		for(int T = S;T;T = (T - 1) & S) if(lowbit(S) == lowbit(T)) cof[S] -= cof[S - T] * ans[T] * iP[cross(S - T,T) * 2];
		for(int T = S;T;T = (T - 1) & S){
			dp[S] += dp[S - T] * cof[T] * iP[cross(S - T,T)];
		}
		ans[S] = 1 - dp[S];

		cof[S] += ans[S];
		dp[S] += ans[S];
	}

	Z answer = 0;
	rep(S,0,(1 << n) - 1) f[S] = 0;
	f[(1 << n) - 1] = 1;
	per(S,(1 << n) - 1,0){
		for(int T = S - lowbit(S);T;T = (T - 1) & S) f[T] -= iP[cross(S - T,T)] * f[S];
	}
	rep(i,0,n - 1){
		rep(S,0,(1 << n) - 1) if((S >> i) & 1) f[S - (1 << i)] += f[S];
	}
	rep(S,0,(1 << n) - 1){
		int ssum = 0;
		rep(u,0,n - 1){
			if((S >> u) & 1) continue;
			ssum += popcnt(msk[u] & S);
		}
		answer += iP[ssum] * ans[S] * f[S];
	}
	printf("%d\n",answer.val());
}

int main(){
#ifdef cxy
	freopen("years2.in","r",stdin);
#endif
	scanf("%d%d",&testid,&TEST);
	while(TEST--) solve();
	return 0;
}