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. 简单的 Nginx+Tomcat 配置负载均衡集群

    简单 Nginx+Tomcat 配置负载均衡集群 前期准备 解压两个tomcat,修改端口号 server1:8081 server:8082 同时启动 nginx官网下载解压版nginx 创建一个简 ...

  2. 对Word2Vec的理解

    1. word embedding 在NLP领域,首先要把文字或者语言转化为计算机能处理的形式.一般来说计算机只能处理数值型的数据,所以,在NLP的开始,有一个很重要的工作,就是将文字转化为数字,把这 ...

  3. 铁大树洞APP视频讲解和原型演示

    首先放上我们团队视频讲解演示的视频:https://v.youku.com/v_show/id_XNDYyMzA3MTgzNg==.html 团队名称:超能陆战队 团队成员:刘梦鑫(队长) 段行行 徐 ...

  4. sql server 存储过程的(包含事务)方法里面,采用游标循环,批量删除(修改)数据

    sqlserver 数据库 1.下面是完整的 在存储过程中 使用游标进行 循环删除的实例(包括存储过程中,事务的应用) 2.有问题的话,欢迎随时讨饶我,相信大家看下注释应该就能明白了,很简单的一个,小 ...

  5. Vue老项目支持Webpack打包

    1.老的vue项目支持webpack打包 最近在学习Vue.js.版本是2.6,webpack的版本也相对较老,是2.1.0版本.项目脚手架只配置了npm run dev和npm run build. ...

  6. Java openrasp学习记录(二)

    Author:tr1ple 主要分析以下四个部分: 1.openrasp agent 这里主要进行插桩的定义,其pom.xml中定义了能够当类重新load时重定义以及重新转换 这里定义了两种插桩方式对 ...

  7. java引用数据类型之Scanner与Random

    一 Scanner类 引用数据类型的使用 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式. 数据类型  变量名  =  new 数据类型(); 每种引用数据类型都s ...

  8. iMX283光盘资料 163网盘

    [光盘资料]EasyARM-i.MX280A_283A_287A V1.04(EasyARM-i.MX280A_283A_287A_cd).zip 提取码: y6pnW6sY

  9. [C/C++]快速读入代码(快读)

    快读 1.为什么要有快读 好吧,有些题目看上去十分简单,例如https://www.luogu.com.cn/problem/P4305这道题,实际上数据量巨多,光是一个测试点就可能有几个MB,在这种 ...

  10. linux 命令行安装谷歌浏览器

    wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 2. yum install -y ls ...