#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define _zero(a) memset(a, 0, sizeof(a))
#define _far(a) memset(a, 0x3f,sizeof (a))
#define __far(a) memset(a, -0x3f,sizeof (a))
#define far 0x3f3f3f3f
#define endl '\n'
#define int long long
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define rep(a,b,c) for(int (a) = (b);(a)<=(c);(a)++)
#define per(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
#define sqr(a) (a)*(a)
#define VI vector<int>
#define VII vector<vector<int>>
#define pb push_back
#define PII pair<int,int>
typedef long long ll;
typedef double dd;
typedef long double ld;
using namespace std;
const int inf = 1e10;
const int mod = 998244353;
const int maxn = 2e5 + 1000;
const ld pi = atan2(0, -1);//arctan(y/x);
const ld eps = 1e-4;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
const int N = 2e5 + 100, M = 4e5 + 100;
int n, m;
int u[M], v[M], w[M];
int fa[N];
bool cnt[30];
void init()
{
rep(i,1,n) fa[i] = i;
}
int get(int x)
{
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
void merge_(int x, int y)
{
fa[get(x)] = get(y);
}
bool judge()
{
int res = get(1);
rep(i,1,n) if (res != get(i)) return false;
return true;
}
void solve()
{
_zero(cnt);
int ans = 0;
per(idx,29,0)
{
init();
rep(e,1,m)
{
int val = w[e];
bool use = true;
per(i,29,idx) if (!cnt[i] && (val >> i) & 1) use = false;
if (!use) continue;
merge_(u[e], v[e]);
}
if (!judge())
{
ans += 1 << idx;
cnt[idx] = 1;
}
}
cout << ans << endl;
}
signed main()
{
IOS;
cin >> n >> m;
rep(i,1,m) cin >> u[i] >> v[i] >> w[i];
solve();
return 0;
}
每日一题 Minimum Or Spanning Tree
最新推荐文章于 2024-11-11 23:32:39 发布