当前位置:   article > 正文

CodeForces - 1624G MinOr Tree(贪心)_codeforces - 1624e

codeforces - 1624e

题目链接:点击这里

题目大意:
给定 n n n 个点, m m m 条边的一个图,求该条的最小或运算( or \text{or} or)生成树,即求其生成树中,所有边权或运算之最小的那个生成树

题目分析:
二进制逐位考虑,我们从高位向低位考虑,由于我们希望或运算答案最小,所以我们肯定想让高位尽可能为 0 0 0 ,那么我们就可以看这一位为 0 0 0 的那些边是否可以构成一颗生成树,如果可以,那么就把这一位是 1 1 1 的边标记上永不再用(因为选择这条边会使或运算的结果这一位为 1 1 1);如果不能构成生成树,那么最后运算后结果的这一位就不可避免的要是 1 1 1 ,就要对答案统计上这一位的贡献
如此往复上过程逐位考虑即可

具体细节见代码:

// Problem: G. MinOr Tree
// Contest: Codeforces - Codeforces Round #764 (Div. 3)
// URL: https://codeforces.com/contest/1624/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
//#define int  ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
struct Edge{
	int u,v,val;
}edge[maxn];
int n,m,fa[maxn];
bool vis[maxn];
int find(int u)
{
	return fa[u]==u ? u : fa[u]=find(fa[u]);
}
int main()
{
	int t = read();
	while(t--)
	{
		n = read(),m = read();
		for(int i = 1;i <= m;i++) edge[i].u = read(),edge[i].v = read(),edge[i].val = read();
		int res = 0;
		for(int i = 1;i <= m;i++) fa[i] = i,vis[i] = 0;
		for(int i = 30;~i;i--)
		{
			for(int j = 1;j <= n;j++) fa[j] = j;
			for(int j = 1;j <= m;j++)
			{
				if(vis[j] || edge[j].val>>i&1) continue;
				int fax = find(edge[j].u),fay = find(edge[j].v);
				if(fax != fay) fa[fax] = fay;
			}
			int cnt = 0;
			for(int j = 1;j <= n;j++) cnt += fa[j]==j;
			if(cnt == 1)
			{
				for(int j = 1;j <= m;j++) if(edge[j].val>>i&1) vis[j] = 1;
			}
			else res |= (1<<i);
		}
		printf("%d\n",res);
	}
	return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/52960
推荐阅读
相关标签
  

闽ICP备14008679号