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, aiaiAND 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.

Examples
input

Copy
4
3 6 28 9
output

Copy
4
input

Copy
5
5 12 9 16 48
output

Copy
3
input

Copy
4
1 2 4 8
output

Copy
-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.

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define lson m<<1,l,mid
#define rson m<<1|1,mid+1,r
#define getmid(m) (tree[m].l+tree[m].r)>>1;
const int maxn=1e5+;
typedef long long ll;
using namespace std;
pair<int,int>pp;
vector<int>vec[];
vector<int>G[maxn];
ll a[maxn];
int vis[maxn];
int endd;
int minn=0x3f3f3f3f;
void dfs(int sta,int dep)
{
vis[sta]=;
if(dep>=minn)
{
vis[sta]=;
return;
}
for(int t=;t<G[sta].size();t++)
{
int u=G[sta][t];
if(u==endd&&dep>)
{
minn=min(minn,dep+);
}
if(vis[u]==)
{
dfs(u,dep+);
}
}
vis[sta]=;
}
 
int main()
{
int n;
cin>>n;
for(int t=;t<n;t++)
{
scanf("%lld",&a[t]);
}
for(int t=;t<;t++)
{
for(int j=;j<n;j++)
{
if(a[j]&((1ll<<t)))
{
vec[t].push_back(j);
}
}
}
for(int t=;t<;t++)
{
if(vec[t].size()>=)
{
puts("");
return ;
}
}
for(int t=;t<;t++)
{
if(vec[t].size()==)
{
int u=vec[t][];
int v=vec[t][];
G[u].push_back(v);
G[v].push_back(u);
}
}
for(int t=;t<n;t++)
{
endd=t;
dfs(t,);
}
if(minn==0x3f3f3f3f)
{
puts("-1");
}
else
{
printf("%d\n",minn);
}
// system("pause");
return ;
}

Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)的更多相关文章

  1. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  2. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  3. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  4. Codeforces Round #580 (Div. 1) A-E

    Contest Page A Tag:构造 将$a_i$看做一个无穷数列,$i > 2n$时$a_i = a_{i - 2n}$.设$sgn_i = \sum\limits_{j=i+1}^{i ...

  5. Codeforces Round #580 (Div. 2)

    这次比上次多A了一道,但做得太慢,rating还是降了. Problem A Choose Two Numbers 题意:给出两个集合A,B,从A,B中分别选出元素a,b使得a+b既不属于集合A,又不 ...

  6. Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;const int maxn=300;cons ...

  7. Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)

    链接: https://codeforces.com/contest/1230/problem/E 题意: Kamil likes streaming the competitive programm ...

  8. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  9. Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)

    https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...

随机推荐

  1. 在centOS7上安装一套java运行环境

    前提:更换centos的默认yum源,提高下载速度. 备份系统自带的yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS- ...

  2. GitLab 数据库

    访问 GitLab 数据库 步骤 用的 Docker Gitlab,首先进入容器 docker exec -it gitlab /bin/bash `` 找到数据库配置文件 ```bash /var/ ...

  3. Vue中diff算法的理解

    Vue中diff算法的理解 diff算法用来计算出Virtual DOM中改变的部分,然后针对该部分进行DOM操作,而不用重新渲染整个页面,渲染整个DOM结构的过程中开销是很大的,需要浏览器对DOM结 ...

  4. java方法中参数传递与随机点名器、库存管理案例

    一 参数传递 1.定义: 参数传递,可以理解当我们要调用一个方法时,我们会把指定的数值,传递给方法中的参数, 这样方法中的参数就拥有了这个指定的值,可以使用该值,在方法中运算了.这种传递方式,我 们称 ...

  5. 2020-06-20:一句话总结ZK?

    福哥答案2020-06-20: 这道题价值不大,但是面试题里有这道题. 分布式协调服务,注册服务和发现,树形结构,监听机制,过半机制. ZooKeeper是源代码开放的分布式协调服务,由雅虎公司创建, ...

  6. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  7. 代码优化实战,3行代码解决了一百个if else!

    事情是这样的,前段时间做代码review的时候,发现项目中有一个方法代码量超鸡儿多,而且大部分都是写的参数校验的代码,得,我们先抓着缕一缕需求先. 产品需求 找到产品要到了需求文档,需求是这样得: e ...

  8. hdfs学习(一)

    一.hdfs概述 介绍: 在现代的企业环境中,单机容量往往无法存储大量数据,需要跨机器存储.统一管理分布在集群上的文件系统称为分布式文件系统 .HDFS(Hadoop Distributed File ...

  9. 题解 poj 3304

    题目描述 线段和直线判交板子题 分析题目,如果存在这一条直线,那么过这条直线作垂线,一定有一条垂线穿过所有线段,否则不存在.题目转化为寻找一条直线与所有线段有交点. 直线线段判交方法: 1.先判断线段 ...

  10. troubleshoot之:分析OutOfMemoryError异常

    目录 简介 OutOfMemoryError java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMemoryError: GC Ov ...