CF1624G 题解

CF1624G 题解

大水题给我评个 ∗ 1900 ^*1900 1900

题面

求最小生成树,这次的权值和变成了或和。

也就是说,求 min ⁡ { w 1 or ⁡ w 2 or ⁡ … or ⁡ w n − 1 } \displaystyle\min \{w_1 \operatorname{or} w_2 \operatorname{or} \dots \operatorname{or} w_{n - 1}\} min{w1orw2ororwn1}

题解

二进制最值经典套路:位越高的必须能 0 0 0 0 0 0

既然这样, 0 0 0 的条件就是所有的边在这一位下必须为 0 0 0

如果在最高位,有一组边这一位全为 0 0 0 且可以从中挑选出生成树,那么必定选。

那么,不难想出一种用并查集维护的贪心手段:

  • 如果边权的此位为 0 0 0,插入并查集。
  • 如果并查集将所有点连起来(这里面只有一个祖先,即有生成树),那么将未选中并查集的所有边删去。
  • 否则,答案加上这一位,即 ans += (1LL << i)

Talk is cheap, give me the code!

Code

#include <bits/stdc++.h>

#define int long long
// FOR templates.
#define rep(i, s, n, k) for(int i = s;i <= n;i += k)
#define repn(i, s, n, k) for(int i = s;i < n;i += k)
#define pre(i, s, n, k) for(int i = s;i >= n;i -= k)
#define pren(i, s, n, k) for(int i = s;i > n;i -= k)
// Abbr for STL.
#define pii pair<int, int>
#define pdd pair<double, double>
#define mpi map<int, int>
#define pii pair<int, int>
#define pdd pair<double, double>
#define vc vector<int>
#define mpp map<int, int>
#define arr(k, n) int k[n]
#define all(v) v.begin(), v.end()
// CIN templates, proven very useful.
#define cn(n) int n;cin >> n
#define cm(n) cin >> n
// Abbr for funcs.
#define pb push_back
#define mset memset
// #define files
using namespace std;
const int MAXN = 0x3f3f3f3fLL;
const int MOD1 = 1000000007LL;
const int MOD2 = 998244353LL;
int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int gcd(int a, int b) {if(b == 0) return a;return gcd(b, a % b);}
inline int lowbit(int x) {return x & (-x);}
inline int lcm(int a, int b) {return a * b / gcd(a, b);}

vector<pair<pair<int, int>, int> > ge;
void add_edge(int u, int v, int w){
	ge.pb(make_pair(make_pair(u, v), w));
}
int fa[200001], ng[200001];
int FindFather(int n){
	if(fa[n] == n) return n;
	return fa[n] = FindFather(fa[n]);
}
void Union(int u, int v){
	fa[FindFather(u)] = FindFather(v);
}

signed main(){
#ifdef files
    freopen(".in", "r", stdin);
    freopen(".out", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
	cn(t); while(t--){
		cn(n); cn(m);
		ge.clear();
		rep(i, 1, n, 1) ng[i] = 0;
		repn(i, 0, m, 1){
			cn(u); cn(v); cn(w);
			add_edge(u, v, w);
		}
		int ans = 0;
		pre(i, 31, 0, 1){
			vc used;
			rep(j, 1, n, 1) fa[j] = j;
			repn(j, 0, m, 1){
				if(ng[j] || ge[j].second & (1LL << i)){
					continue;
				}
				Union(ge[j].first.first, ge[j].first.second);
				used.pb(j);
			}
			set<int> st;
			rep(j, 1, n, 1) st.insert(FindFather(j));
			if(st.size() == 1){
				int l = 0;
				repn(j, 0, m, 1){
					if(used[l] == j) l++;
					else ng[j] = 1;
				}
			}
			else{
				ans += (1 << i);
			}
		}
		cout << ans << endl;
	}
    return 0;
}

/*
 *  things to check
 *  1.  int overflow or long long memory need
 *  2.  recursion/array/binary search/dp/loop bounds
 *  3.  precision
 *  4.  special cases(n=1,bounds)
 *  5.  delete debug statements
 *  6.  initialize(especially multi-tests)
 *  7.  = or == , n or m ,++ or -- , i or j , > or >= , < or <=
 *  8.  keep it simple and stupid
 *  9.  do not delete, use // instead
 *  10. operator priority
 *  11. is there anything extra to output?
 *  12. THINK TWICE CODE ONCE, THINK ONCE DEBUG FOREVER
 *  13. submit ONCE, AC once. submit twice, WA forever
 *  14. calm down and you'll get good rank
 *  15. even a bit wrong scores zero
 *  16. ...
 **/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值