D. Shortest Cycle
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

算法:floyd最小环

题解:根据题意建图,跑最小环模板。

#include <iostream>
#include <cstdio> using namespace std; #define INF 0x3f3f3f3f
const int maxn = 1e5+; typedef long long ll; int n;
ll arr[maxn];
ll dis[][];
ll maze[][]; int floyd() {
ll res = INF;
for(int k = ; k <= n; k++) {
//下面两个循环先算res的原因是:你从i~j的最短路还没有经过k
//其实就是我先不管i~k,k~i这条边(等同于删除),然后找到i~j这条边的权值,再加上i~k,k~j这条边就是一个环了,找到最小的情况
for(int i = ; i < k; i++) {
for(int j = i + ; j < k; j++) {
res = min(res, dis[i][j] + maze[i][k] + maze[k][j]);
}
}
//这个循环就是floyd更新多源最短路
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
return res;
} int main() {
scanf("%d", &n);
int k = ;
for(int i = ; i <= n; i++) {
ll x;
scanf("%I64d", &x);
if(x != ) { //将0去掉,0按位与任何数就是0,不存在有这种情况的环
arr[++k] = x;
}
}
n = k;
if(n > * ) { //因为每位数最多64位,因为没有0,所以这64位上,必定有有一位有值,当同一位的值超过3时,最小环就由这三个数组成
printf("3\n");
return ;
}
//建图
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(arr[i] & arr[j]) {
dis[i][j] = dis[j][i] = maze[i][j] = maze[j][i] = ;
} else {
dis[i][j] = dis[j][i] = maze[i][j] = maze[j][i] = INF;
}
}
}
int ans = floyd();
if(ans == INF) { //没有形成环
printf("-1\n");
} else {
printf("%d\n", ans);
}
return ;
}

D. Shortest Cycle(floyd最小环)的更多相关文章

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

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

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

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

  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. Codeforces 1206 D - Shortest Cycle

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

  5. D. Shortest Cycle

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

  6. hdoj 1599 find the mincost route【floyd+最小环】

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. POJ 1734.Sightseeing trip (Floyd 最小环)

    Floyd 最小环模板题 code /* floyd最小环,记录路径,时间复杂度O(n^3) 不能处理负环 */ #include <iostream> #include <cstr ...

  8. HDU - 6080 :度度熊保护村庄 (凸包,floyd最小环)(VJ1900题达成)

    pro:二维平面上,给定N个村庄.M个士兵驻守,把村庄围住,现在我们想留下更多的士兵休息,使得剩下的士兵任然满足围住村庄.N,M<500: sol:即是要找一个最小的环,环把村庄围住. 由于是环 ...

  9. HDU1599(Floyd最小环)

    Floyd最小环理解+模板: https://www.cnblogs.com/DF-yimeng/p/8858184.html 除了上述博文里写的,我再补充几点我的理解. 1.为什么先枚举ij求经过i ...

随机推荐

  1. Python 正则表达模块详解

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  2. 并不对劲的CSP-S2019

    day1 对题的第一印象: t1:颇有"小凯的疑惑"之风(赛后发现确实如此,因为最好写的正解也可以直接输出) t2:log方会被卡吧?好像倍增一个log?(赛后发现有很好写的线性做 ...

  3. ListUtils的简单集合操作和原理

    1 Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId>com ...

  4. MySQL去除查询结果重复值

    下面先来看看例子: table  id name  1 a  2 b  3 c  4 c  5 b 库结构大概这样,这只是一个简单的例子,实际情况会复杂得多. 比如我想用一条语句查询得到name不重复 ...

  5. ADO与达梦7产生的一个未知问题

    采用OLEDB与达梦7建立数据库连接 连接成功 查询表成功 打开表成功 当进行到addnew 操作时  报异常,未知错误 而且是仅针对这张表 ,其他表都没有问题 当清空数据后可以再插入一次数据,之后就 ...

  6. 用python 打印出爱心

    其实,如果程序员真的很浪漫,普通人不懂,科技兴旺,也许你是惊呆了!!!!! 今天,泰泰又给你带来了一个“程序员技术(浪漫)表现”教程.飞鲸水龙头有希望它能在这个七月前夜帮到你.如果使用成功,记得给泰泰 ...

  7. Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...

  8. 使用pycharm 编写代码 并在远程主机上运行

    一 要求 远程主机有python解释器 二 在菜单栏,File -> Settings… -> Project ×× -> Project Interpreter,点击右侧 Add按 ...

  9. ACM算法模板整理

    史诗级ACM模板整理 基本语法 字符串函数 istream& getline (char* s, streamsize n ); istream& getline (char* s, ...

  10. 微信小程序中concat 和push的区别

    push和concat二者功能很相像,但有两点区别. 先看如下例子: var arr = []; arr.push(1); arr.push(2); arr.push([3, 4]) arr.push ...