#include<bits/stdc++.h> // #define SINGLE_INPUT #define ll long long #define ull unsigned long long #define N 500005 #define MOD 998244353 usingnamespace std;
voidunite(int u, int v){ int fu = find(u), fv = find(v); if (fu != fv) { // 小集合fu 加入到 大集合fv,按秩归并 if (fa[fu] < fa[fv]) swap(fu, fv); fa[fv] += fa[fu]; fa[fu] = fv; } } };
voidsol(){ int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i] >> w[i]; } auto check = [&](int msk) { Dsu dsu(n + 1); for (int i = 1; i <= m; i++) { if ((w[i] | msk) == msk) { dsu.unite(x[i], y[i]); } } int neg = 0; for (int i = 1; i <= n; i++) { if (dsu.fa[i] < 0) neg++; } return neg == 1; // 只有一个根,可以生成树 }; int mask = (1 << 30) - 1; for (int i = 29; i >= 0; i--) { if (check(mask ^ (1 << i))) mask ^= (1 << i); } cout << mask << "\n"; }