Jzoj P6357 小ω的图___贪心+并查集

45 篇文章 0 订阅
30 篇文章 0 订阅

题目大意:

n个点,m条边,问点1走到点n的最大路径and和。
n < = 1 0 5 , m < = 5 ∗ 1 0 5 , 边 权 < 2 63 n<=10^5,m<=5*10^5,边权<2^{63} n<=105,m<=5105,<263

分析:

因为and其实是二进制下的逐位运算,
所以我们可以贪心从二进制的高位开始枚举,
利用并查集判断这一位是否能被加入答案中,
这样显然最优

代码:

#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>

#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--) 

#define N 100005
#define M 62

using namespace std;

typedef long long ll;

struct Node{
    int u, v; ll w;
}e[N*5];
int f[N], n, m;

void read(int &x) {
	int f = 1; x = 0; char s = getchar(); 
	while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); }
	x = x * f;
}

void read1(ll &x) {
	ll f = 1ll; x = 0ll; char s = getchar();
	while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10ll + (s - '0'); s = getchar(); }
	x = x * f;
}

bool cmp(Node aa, Node bb) {
	return aa.w > bb.w;	
}

int Find(int x) {
	return (f[x] == x) ? x : (f[x] = Find(f[x])); 
}

int main() {
	read(n); read(m);
	rep(i, 1, m) read(e[i].u), read(e[i].v), read1(e[i].w);
	sort(e + 1, e + m + 1, cmp);
	int R = n; ll ans = 0;
	rwp(i, M, 0) if ((1ll << i) <= e[n].w) { R = i; break; }
	int xx, yy;
	rwp(k, R, 0) {
		ans |= (1ll << k);
		rep(i, 1, n) f[i] = i;
		rep(i, 1, m) 
		    if (e[i].w >= ans) {
  			    xx = Find(e[i].u);
			    yy = Find(e[i].v);
			    if ((e[i].w & ans) == ans && xx != yy) f[xx] = yy;
		    }
		xx = Find(1); 
		yy = Find(n);
		if (xx != yy) ans ^= (1ll << k);
	}
	printf("%lld\n", ans);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值