跳到正文

剪贴板

剪贴板 q4u44lpo

原文
剪贴板信息

剪贴板信息

收录
更新
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];

struct eg{
    int u,v,w;
}E[505];
bool cmp(eg p,eg q){
    return p.w < q.w;
}
int lowbit(int x){
    return x & (-x);
}

int fa[25],bel[2][25],_msk[25],ext[2][1 << 15],con[1 << 15];
int fnd(int u){
    if(fa[u] == u) return u;
    return fa[u] = fnd(fa[u]);
}

int mrg(int u,int v){
    u = fnd(u);v = fnd(v);
    if(u == v) return 0;
    fa[u] = v;
    return 1;
}

void init(){
    rep(u,0,n - 1) _msk[u] = 0;
    rep(u,0,n - 1) _msk[fnd(u)] |= 1 << u;
    fill(con,con + (1 << n),0);
    rep(u,0,n - 1) bel[1][u] = _msk[fnd(u)];
    con[0] = 1;
    rep(u,0,n - 1){
        for(int S = _msk[u];S;S = (S - 1) & _msk[u]) con[S] = 1;
    }
    rep(S,1,(1 << n) - 1){
        int u = ctz(S);
        ext[1][S] = ext[1][S - (1 << u)] | bel[1][u];
    }
}

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

int ok(int S,int T){
    return (ext[0][S] & ext[0][T]) == 0;
}
Z f[1 << 15],g[1 << 15],tf[1 << 15];

Z ans[1 << 15],cof[1 << 15],dp[1 << 15];
Z res[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){
        scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
        E[i].u--;E[i].v--;
    }
    sort(E + 1,E + m + 1,cmp);
    rep(u,0,n - 1) fa[u] = u;
    init();
    rep(S,0,(1 << n) - 1) res[S] = 0;
    rep(i,0,n - 1) res[1 << i] = 1;
    for(int l = 1,r;l <= m;l = r + 1){
        r = l;
        while(r < m && E[r].w == E[r + 1].w) r++;
        rep(u,0,n - 1){
            swap(bel[0][u],bel[1][u]);
            msk[u] = 0;
        }
        rep(S,0,(1 << n) - 1) swap(ext[0][S],ext[1][S]);
        int succ = 0;
        rep(i,l,r){
            if(mrg(E[i].u,E[i].v)) succ = 1;
            msk[E[i].u] |= 1 << E[i].v;msk[E[i].v] |= 1 << E[i].u;
        }  
        init();
        if(!succ) continue;
        rep(S,1,(1 << n) - 1){
            int u = ctz(S);
            sum[S] = sum[S - (1 << u)] + popcnt(msk[u] & S);
        }
        int stable = 0;
        rep(u,0,n - 1) if(bel[0][u] == bel[1][u]) stable |= 1 << u;
        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){
            if(!con[S] || (S & stable)) continue;
            for(int T = S;T;T = (T - 1) & S){
                if(lowbit(S) == lowbit(T) && ok(S - T,T)) cof[S] -= cof[S - T] * ans[T] * iP[cross(ext[0][S - T],T) + cross(ext[0][T],S - T)]; 
            }
            for(int T = S;T;T = (T - 1) & S){
                if(ok(S - T,T)) dp[S] += dp[S - T] * cof[T] * iP[cross(ext[0][S - T],T)];
            }
            ans[S] = 1 - dp[S];
            cof[S] += ans[S];
            dp[S] += ans[S];
        } 
        rep(S,0,(1 << n) - 1){
            g[S] = 0;
            if(!con[S] || (S & stable)) continue;
            int ssum = 0;
            rep(u,0,n - 1){
                if((ext[0][S] >> u) & 1) continue;
                if((ext[1][S] >> u) & 1) ssum += popcnt(msk[u] & S);      
            }       
            g[S] = iP[ssum] * ans[S];
        }
        cof[0] = 1;
        rep(S,1,(1 << n) - 1){
            int u = ctz(S);
            cof[S] = cof[S - (bel[0][u] & S)] * res[(bel[0][u] & S)];
        }
        rep(S,0,(1 << n) - 1){
            f[S] = 0;
            if(!con[S] || (S & stable)) continue;
            int SS = ext[1][S],fail = 0;
            rep(u,0,n - 1) if(((SS >> u) & 1) && (S & bel[0][u]) == 0) fail = 1;
            f[S] = 1 - fail;
        }
        per(S,(1 << n) - 1,0){
            if(S & stable) continue;
            for(int T = S - lowbit(S);T;T = (T - 1) & S) if(ok(S - T,T)) f[T] -= iP[cross(S - T,ext[0][T])] * f[S] * cof[S - T];
        }
        rep(S,0,(1 << n) - 1) f[S] *= cof[S];
        rep(S,0,(1 << n) - 1) tf[S] = 0;
        rep(S,0,(1 << n) - 1){
            if((S & stable)) continue;
            for(int T = S;T;T = (T - 1) & S) if(ok(S - T,T)) tf[T] += f[S];
        }
        rep(S,0,(1 << n) - 1) if((S & stable) == 0) res[S] = tf[S] * g[S];
    }
    Z answer = 0;
    rep(S,1,(1 << n) - 1) answer += res[S];
    printf("%d\n",answer.val());
}

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