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
    评论
k-shortest c是一种图算法,用于寻找图中从一个起点到一个终点的k条最短路径。该算法可以应用于许多实际问题,例如网络路由,行程规划等。 k-shortest c算法的基本原理是在每一次迭代中,从起点到终点寻找一条最短路径,并将其添加到结果集中。然后,根据路径长度对图中的边进行更新,使得之后的迭代可以找到更短的路径。这个过程会重复k次,直到找到k条最短路径为止。 该算法可以通过以下步骤实现: 1. 设置起点和终点,并初始化结果集为空。 2. 在每次迭代中,使用Dijkstra或A*等最短路径算法找到一条最短路径。 3. 将找到的最短路径添加到结果集中。 4. 更新路径中的边的权值,使得之后的迭代可以找到更短的路径。 5. 重复步骤2-4,直到找到k条最短路径为止。 k-shortest c算法的优点是能够在不遍历整个图的情况下找到多条最短路径,从而减少了计算量。它可以提供多个解决方案供选择,使得问题的解决更加灵活。 然而,k-shortest c算法也存在一些问题。首先,随着k值的增加,算法的计算复杂度也会增加,因此在实际应用中需要权衡计算资源和最优解之间的平衡。其次,算法在处理大规模图形时可能会受到内存和计算能力的限制。 在总结中,k-shortest c是一种寻找k条最短路径的图算法,适用于各种实际问题。它通过迭代找到一条条最短路径,并更新路径的边权值,以获得更多的解决方案。尽管算法存在一些限制,但它仍然是解决某些问题的有效工具。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值