[codeforces1205B]Shortest Cycle

本文探讨了在特定条件下寻找图中最小环长的算法。通过枚举位运算和Floyd算法,解决由数字间位与操作定义的图中寻找最短环的问题。适用于竞赛编程及图论研究。

time limit per test : 1 second
memory limit per test : 256 megabytes

You are given nnn integer numbers a1,a2,…,ana_1,a_2,…,a_na1,a2,,an. Consider graph on nnn nodes, in which nodes i,j(ii, j (ii,j(ij)j)j) are connected if and only if, aia_iai AND aja_jaj ≠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 n(1≤n≤105)n(1≤n≤10^5)n(1n105) — number of numbers.
The second line contains nnn integer numbers a1,a2,…,an(0≤ai≤1018)a_1,a_2,…,a_n (0≤a_i≤10^{18})a1,a2,,an(0ai1018).
Output

If the graph doesn’t have any cycles, output −1−11. Else output the length of the shortest cycle.
Examples
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).
In the second example, the shortest cycle is (5,12,9).
The graph has no cycles in the third example.

题意:
给定nnn个数字a1a_1a1 ~ ana_nan,在aia_iai,aja_jaj之间有连边,当且仅当iiijjjaia_iai AND aja_jaj000,求形成的图的最小环。

题解:
首先枚举每一位出现的次数,如果次数大于2,那么答案就是3
如果没有出现次数大于2的,那么将所有位出现次数为2的位所对应的数字取出(最多120个),然后建图之后用floyd计算最小环。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=1999122700;
vector<ll>poc[64];
ll mp[504][504],roc[504][504],a[100004];
int n,cnt;
ll gac[504];
int main(){
	memset(mp,0,sizeof(mp));
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		for(int j=0;j<=60;j++){
			if(a[i]&(1LL<<j))poc[j].push_back(i);
		}
	}
	for(int i=0;i<=60;i++){
		if(poc[i].size()>=3)return puts("3"),0;
	}
	for(int i=0;i<=60;i++){
		if(poc[i].size()==2){
			for(int j=0;j<poc[i].size();j++){
				gac[++cnt]=poc[i][j];
			}
		}
	}
	sort(gac+1,gac+cnt+1);
	cnt=unique(gac+1,gac+cnt+1)-gac-1;
	for(int i=1;i<=cnt;i++){
		for(int j=1;j<=cnt;j++){
			mp[i][j]=INF;
			roc[i][j]=INF;
		}
	}
	for(int i=1;i<=cnt;i++){
		for(int j=1;j<=cnt;j++){
            if(i==j)continue;
			if(a[gac[i]]&a[gac[j]]){
                mp[i][j]=1;
                roc[i][j]=1;
			}
		}
	}
	/*
	for(int i=1;i<=cnt;i++){
		for(int j=1;j<=cnt;j++){
			if(mp[i][j]==INF)cout<<"       -1";
			else cout<<"       "<<mp[i][j];
		}
		cout<<endl;
	}
    */
	ll ans=INF;
	for(int k=1;k<=cnt;k++){
		for(int i=1;i<k;i++){
            for(int j=i+1;j<k;j++){
                ans=min(ans,roc[i][k]+roc[k][j]+mp[i][j]);
            }
		}
        for(int i=1;i<=cnt;i++){
            for(int j=1;j<=cnt;j++){
                mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
            }
        }
	}
    if(ans==INF)puts("-1");
    else printf("%lld\n",ans);
    /*
	for(int i=1;i<=cnt;i++)cout<<a[gac[i]]<<" ";cout<<endl;
	for(int i=1;i<=cnt;i++){
		for(int j=1;j<=cnt;j++){
			if(mp[i][j]==INF)cout<<"       -1";
			else cout<<"       "<<mp[i][j];
		}
		cout<<endl;
	}
    */
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值