#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#pragma GCC target("avx512f")
using namespace std;
template <class T>
using Ve = vector<T>;
#define ALL(v) (v).begin(), (v).end()
#define pii pair<ll, ll>
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, a, b) for(int i = (a); i >= (b); --i)
#define pb push_back
bool Mbe;
ll read() {
ll x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
void write(ll x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const ll Mod = 1e9 + 7, inv2 = (Mod + 1) >> 1;
ll n, m, cnte[(1 << 15) + 9], pw2[509], ipw2[509];
ll f[(1 << 15) + 9], g[(1 << 15) + 9], h[(1 << 15) + 9];
ll pw(ll x, ll p) {
ll res = 1;
while(p) {
if(p & 1) res = res * x % Mod;
x = x * x % Mod, p >>= 1;
}
return res;
}
Ve<pii> vec[509];
ll fa[20];
ll nodes, node[20], nid[20];
ll tot, pset[20], components[(1 << 15) + 9], rid[20], roots[20], pa[20];
ll orista[(1 << 15) + 9], psta[(1 << 15) + 9], coef[(1 << 15) + 9], scoef[(1 << 15) + 9];
ll res[(1 << 15) + 9], tsum[(1 << 15) + 9], tfa[20];
bool used[20];
ll find(ll x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
ll tfind(ll x) {
return tfa[x] == x ? x : tfa[x] = tfind(tfa[x]);
}
ll cross(ll S, ll T) {
return cnte[S | T] - cnte[S] - cnte[T];
}
void work(ll edg, Ve<ll> pt) {
tot = 0, nodes = 0;
for(ll i : pt) node[nodes] = i, nid[i] = nodes, ++nodes;
for(ll i : pt) {
if(find(i) == i) roots[tot] = i, rid[i] = tot, ++tot;
}
memset(pset, 0, sizeof(pset));
rep(i, 0, nodes - 1) {
pa[i] = rid[find(node[i])];
pset[pa[i]] |= (1 << i);
}
memset(cnte, 0, sizeof(cnte));
for(auto [x, y] : vec[edg]) {
if(find(x) == find(y)) continue;
if(count(ALL(pt), x) && count(ALL(pt), y)) cnte[(1 << nid[x]) | (1 << nid[y])]++;
}
rep(i, 0, nodes - 1) {
rep(j, 0, (1 << nodes) - 1) {
if((j >> i) & 1) cnte[j] += cnte[j ^ (1 << i)];
}
}
rep(i, 0, (1 << tot) - 1) {
psta[i] = 0;
rep(j, 0, tot - 1) {
if((i >> j) & 1) psta[i] |= pset[j];
}
}
rep(i, 0, (1 << nodes) - 1) {
orista[i] = 0, components[i] = 0;
rep(j, 0, nodes - 1) {
if((i >> j) & 1) {
orista[i] |= (1 << node[j]);
components[i] |= (1 << pa[j]);
}
}
}
rep(i, 0, (1 << nodes) - 1) {
coef[i] = 1;
rep(j, 0, tot - 1) {
ll st = pset[j] & i;
if(!st) continue;
coef[i] = coef[i] * h[orista[st]] % Mod;
}
}
memcpy(tsum, h, sizeof(h));
rep(i, 0, n - 1) {
rep(j, 0, (1 << n) - 1) {
if((j >> i) & 1) tsum[j] = (tsum[j] + tsum[j ^ (1 << i)]) % Mod;
}
}
rep(i, 0, (1 << nodes) - 1) {
scoef[i] = 1;
rep(j, 0, tot - 1) {
ll st = pset[j] & i;
if(!st) continue;
scoef[i] = scoef[i] * tsum[orista[st]] % Mod;
}
}
memset(g, 0, sizeof(g));
g[0] = Mod - 1;
rep(S, 1, (1 << nodes) - 1) {
ll cS = components[S], nd = psta[cS], sum = 0;
for(ll cT = (cS - 1) & cS; cT; cT = (cT - 1) & cS) {
ll T = psta[cT], nT = T & S, nor = T ^ nT;
ll ce = cnte[nd ^ T] * 2 + cross(nd ^ T, nor) * 2 + cross(nd ^ T, nT);
sum = (sum + pw2[ce] * g[nT]) % Mod;
}
g[S] = (pw2[cnte[nd] * 2] - sum + Mod) % Mod;
}
memset(f, 0, sizeof(f));
f[0] = 1;
rep(S, 1, (1 << nodes) - 1) {
f[S] = g[S]; ll cS = components[S], lowbit = cS & (-cS);
for(ll cT = (cS - 1) & cS; cT; cT = (cT - 1) & cS) {
if(!(cT & lowbit)) continue;
ll T = psta[cT], nT = T & S;
ll ce = cross(T ^ nT, psta[cS ^ cT] ^ S ^ nT) * 2 + cross(nT, psta[cS ^ cT] ^ S ^ nT) + cross(T ^ nT, S ^ nT);
f[S] = (f[S] + g[S ^ nT] * f[nT] % Mod * pw2[ce]) % Mod;
}
}
memset(res, 0, sizeof(res));
rep(T, 1, (1 << nodes) - 1) {
ll cT = components[T];
for(ll cS = cT; cS; cS = (cS - 1) & cT) {
ll S = psta[cS] & T;
ll wT = psta[cT], wS = psta[cS];
ll ce = cnte[(1 << nodes) - 1 - wT] * 2 + cross(wT ^ T, (1 << nodes) - 1 - wT) * 2
+ cross(T, (1 << nodes) - 1 - wT) + cross((wT ^ wS) ^ (T ^ S), wS ^ S) * 2
+ cross(T ^ S, wS ^ S) + cross((wT ^ wS) ^ (T ^ S), S);
ll val = f[S] * g[T ^ S] % Mod * pw2[ce] % Mod * scoef[(1 << nodes) - 1 - wT] % Mod * coef[T] % Mod;
res[S] = (res[S] - val + Mod) % Mod;
}
}
rep(S, 1, (1 << nodes) - 1) {
ll st = orista[S];
h[st] = res[S];
}
}
void solve() {
n = read(), m = read();
rep(i, 0, m) vec[i].clear();
iota(fa, fa + n, 0ll);
memset(h, 0, sizeof(h));
rep(i, 0, n - 1) h[1 << i] = 1;
rep(i, 1, m) {
ll x = read(), y = read(), z = read();
--x, --y; vec[z].pb({x, y});
}
ll ru = 1;
rep(edg, 1, m) {
if(vec[edg].empty()) continue;
memset(used, 0, sizeof(used));
bool flag = 0;
for(auto [x, y] : vec[edg]) {
if(find(x) == find(y)) {
ru = ru * 4 % Mod;
continue;
}
flag = 1, used[find(x)] = used[find(y)] = 1;
}
if(!flag) continue;
memcpy(tfa, fa, sizeof(fa));
for(auto [x, y] : vec[edg]) tfa[tfind(x)] = tfind(y);
memset(rid, 0, sizeof(rid));
rep(i, 0, n - 1) {
if(tfind(i) != i) continue;
Ve<ll> pt;
bool flag = 0;
rep(j, 0, n - 1) {
if(tfind(j) == i) {
if(find(i) != find(j)) flag = 1;
pt.pb(j);
}
}
if(flag) work(edg, pt);
}
for(auto [x, y] : vec[edg]) fa[find(x)] = find(y);
}
rep(i, 1, (1 << n) - 1) h[i] = h[i] * ru % Mod;
ll ans = 0;
rep(i, 1, (1 << n) - 1) ans += h[i];
ans %= Mod;
ll iv = ipw2[m * 2];
ans = ans * iv % Mod;
write(ans), putchar('\n');
}
bool Med;
int main() {
// freopen("years.in", "r", stdin);
// freopen("years.out", "w", stdout);
cerr << fabs(&Med - &Mbe) / 1048576.0 << "MB\n";
ll T = read(); T = read();
pw2[0] = ipw2[0] = 1;
rep(i, 1, 500) pw2[i] = pw2[i - 1] * 2 % Mod, ipw2[i] = ipw2[i - 1] * inv2 % Mod;
while(T--) solve();
cerr << "\n" << clock() * 1.0 / CLOCKS_PER_SEC * 1000 << "ms\n";
return 0;
}剪贴板
剪贴板 e2jmf8l6
剪贴板信息
剪贴板信息
- 收录
- 更新