CF1205B·Shortest Cycle

15 篇文章 4 订阅
5 篇文章 0 订阅

初见安~这里是传送门:CF #580 Div1 B & Div2 D Shortest Cycle

Description

You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii, jj (i≠ji≠j) are connected if and only if, aiai AND aj≠0aj≠0, where AND denotes the bitwise AND operation.

Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.

Input

The first line contains one integer nn (1≤n≤105)(1≤n≤105) — number of numbers.

The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤10180≤ai≤1018).

Output

If the graph doesn't have any cycles, output −1−1. Else output the length of the shortest cycle.

input

4
3 6 28 9

output

4

input

5
5 12 9 16 48

output

3

input

4
1 2 4 8

output

-1

Note

In the first example, the shortest cycle is (9,3,6,28)(9,3,6,28).

In the second example, the shortest cycle is (5,12,9)(5,12,9).

The graph has no cycles in the third example.

Sol

昨晚打的时候被这个题卡死了……因为最小环很简单,但是连边是一个难点……

于是昨晚Div1进了前500的candidate大佬hby讲述了一个很玄妙的作法:

a的范围是1e18,也就是说二进制下最多60几位。如果存在有一位为1的数的数量大于2,那么最小环的数量就可以直接得到为3。原理很简单——因为这一位为1的几个数都是互相连通的,也就是说这几个数可以组成一个无向完全图,最小环直接就是3了。

再看,考虑极端情况:如果每一位都刚好只有两个数的时候,就是n=63*2的时候有可能出现。那么如果n > 126呢?是不是就一定会出现上一段的情况了?是的。所以对于n>130【开大点儿保险】,可以直接输出答案为3

可是这样真的就可以过了吗?

如果给你1e5个0,是不是就挂掉了?是的。

所以还有一个很关键很关键的步骤——把0全部去掉,看剩下的干货的数量和130的关系。

所以至此,我们真正可以用暴力处理的n的范围就在130以内了。

对于最小环,可以用dfs求,也可以用Floyd【hby大佬说dfs的复杂度可能会过高】。本狸就用Floyd求最小环了……反正n的范围已经很小了,暴力连边也是不会有问题的~

上代码——

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define maxn 100005
#define int long long
using namespace std;
typedef long long ll;
int read() {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
	return x * f;
}

ll n, a[maxn], tot = 0;
ll dis[200][200], w[200][200];
signed main() {
	n = read();
	for(int i = 1; i <= n; i++){
		a[++tot] = read();
		if(!a[tot]) tot--;//tot计数 
	}
	
	if(tot > 130) {puts("3"); return 0;}
	n = tot;//后来才发现没有去掉0……所以直接给n了 
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) w[i][j] = dis[i][j] = 99999999;//初始化 
	}
	
	for(int i = 2; i <= n; i++) {//暴力连边 
		for(int j = 1; j < i; j++)
			if(a[i] & a[j]) dis[i][j] = dis[j][i] = w[i][j] = w[j][i] = 1;
	}
	

	ll ans = 99999999;
	for(int k = 1; k <= n; k++) {//Floyd求最小环 
		for(int i = 1; i < k; i++) for(int j = i + 1; j < k; j++) {
			ans = min(ans, dis[i][j] + w[i][k] + w[k][j]);//找最小环 
		}	
		
		for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++)
			dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);//松弛走最短路 
			
		
	}
	if(ans == 99999999) puts("-1");//如果没有环出现 
	else printf("%lld\n", ans);
	return 0;
}

所以这个题似乎……很水?然而思维复杂度莫名有点儿高?

迎评:)
——End——

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值