@description@

给定一个长度为 n 的正整数序列 a1, a2, ..., an。

考虑建一张 n 个点的图。假如 ai AND aj ≠ 0,则在 i, j 之间连无向边。

求在这张图上的最小环。

Input

第一行一个整数 n 表示序列长度 (1≤n≤10^5)

第二行包含 n 个整数 a1,a2,…,an (0≤ai≤10^18)。

Output

如果图中不含任何环,输出 -1。

否则输出最小环长度。

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

第一个样例答案为 (9,3,6,28)。

第二个样例答案为 (5,12,9)。

第三个样例没有环。

@solution@

其实是一道很 sb 的题。。。

考虑假如某个二进制位上存在至少三个数该位为 1,则存在一个长度为 3 的环,显然该环最小。

因为最多有 60 个二进制位,每个位上存在最多 2 个数该位为 1 才有考虑的价值。

而在这种情况,因为非 0 的元素总会占一个二进制位的 1,所以最多会有 120 个非 0 元素;而为 0 的元素就是个孤立点,不需要管它。

所以直接当非 0 的点数 < 120(代码中写的是 300比较方便copy模板)时才用 Floyd 跑最小环,否则直接输出 3。

@accepted code@

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 300;
int G[MAXN + 5][MAXN + 5], A[MAXN + 5][MAXN + 5];
ll a[MAXN + 5];
int n, cnt;
int main() {
scanf("%d", &n);
for(int i=1;i<=n;i++) {
ll x; scanf("%lld", &x);
if( x ) a[++cnt] = x;
if( cnt > MAXN ) {
puts("3");
return 0;
}
}
for(int i=1;i<=cnt;i++)
for(int j=1;j<=cnt;j++)
if( a[i] & a[j] ) A[i][j] = G[i][j] = 1;
else A[i][j] = G[i][j] = MAXN + 5;
int ans = MAXN + 5;
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, A[i][k] + A[k][j] + G[i][j]);
for(int i=1;i<=cnt;i++)
for(int j=1;j<=cnt;j++)
G[i][j] = min(G[i][k] + G[k][j], G[i][j]);
}
if( ans == MAXN + 5 ) puts("-1");
else printf("%d\n", ans);
}

@details@

所以,我至今不知道为什么我当时会卡在这种 sb 题上面。。。

@codeforces - 1205B@ Shortest Cycle的更多相关文章

  1. [Codeforces 1205B]Shortest Cycle(最小环)

    [Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...

  2. Codeforces 1206 D - Shortest Cycle

    D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...

  3. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

  4. CF 1206D - Shortest Cycle Floyd求最小环

    Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...

  5. D. Shortest Cycle(floyd最小环)

    D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. D. Shortest Cycle

    D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...

  7. codeforces 962F.simple cycle(tarjan/点双连通分量)

    题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...

  8. Codeforces 938G Shortest Path Queries [分治,线性基,并查集]

    洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再 ...

  9. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

随机推荐

  1. Python学习(一) 安装,环境搭建,IDE

    第一篇废话太多了,我的博客最主要的是给自己看的,大家觉得还凑合也可以看看,能说自己想法的就更好了,因为一个人的思想是有局限性的.集思广益,自己的认知才不会被禁锢. 注:其他的系统没装,在Windows ...

  2. Nginx 函数解析之ngx_http_get_forwarded_addr_internal

    static ngx_int_t ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr, u_cha ...

  3. 第十章—DOM(一)——Document类型

    DOCUMENT类型 JS通过document类型表示文档,在文档中document对象是HTMLDocument的一个实例,表示整个HTML页面.document对象是window对象的一个属性,因 ...

  4. 痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU外设那些事(2)- 善变的FlexRAM

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RTyyyy系列MCU的FlexRAM外设. 本文是外设系列第二篇,上一篇讲的是离内核最近的高速缓存L1 Cache, ...

  5. Python 五个知识点搞定作用域

    Python 五个知识点搞定作用域 1.块级作用域 想想此时运行下面的程序会有输出吗?执行会成功吗? #块级作用域 if 1 == 1: name = "lzl" print(na ...

  6. IO流-文件操作

    一.字节流读/写 文件 1.字节流 方式读取文件

  7. oracle 写存储过程有返回值时 注意在loop循环处添加返回值:=

    例子: create or replace procedure p_xl is v_count NUMBER(10); begin for rs in(select yhbh from dbyh) l ...

  8. PHP学习(类和对象)——基本概念

    类是面向对象程序设计的基本概念,通俗的理解类就是对现实中某一个种类的东西的抽象, 比如汽车可以抽象为一个类,汽车拥有名字.轮胎.速度.重量等属性,可以有换挡.前进.后退等操作方法. 每个类的定义都以关 ...

  9. RabbitMQ默认端口

    4369 (epmd), 25672 (Erlang distribution)5672, 5671 (AMQP 0-9-1 without and with TLS)15672 (if manage ...

  10. 通过IP地址訪问Jbossserver上的应用

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/liu765023051/article/details/28882533 环境介绍 Web项目中.在 ...