D - Shortest Cycle

思路:n大于某个值肯定有个三元环,否则floyd找最小环。

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define double long double
#define y1 y11
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb emplace_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define pdd pair<double, double>
#define mem(a, b) memset(a, b, sizeof(a))
#define debug(x) cerr << #x << " = " << x << "\n";
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//head const int N = 1e5 + 5;
const int INF = 1e8;
LL a[N];
int mp[205][205], dis[205][205];
int n, cnt;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), cnt += a[i]==0;
sort(a+1, a+1+n, greater<LL>());
n -= cnt;
if(n >= 200) {
printf("3\n");
return 0;
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if(i == j) continue;
if(((a[i]&a[j]) != 0)) mp[i][j] = dis[i][j] = 1;
else mp[i][j] = dis[i][j] = INF;
}
}
int ans = INF;
for (int k = 1; k <= n; ++k) {
for (int i = 1; i < k; ++i) {
for(int j = i+1; j < k; ++j) {
ans = min(ans, dis[i][j]+mp[j][k]+mp[k][i]);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
}
}
}
if(ans == INF) printf("-1");
else printf("%d\n", ans);
return 0;
}

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

  3. codeforces 1051F The Shortest Statement

    题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...

  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 - 1205B@ Shortest Cycle

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 n 的正整数序列 a1, a2, ..., an ...

  8. codeforce D. Shortest Cycle(floyd求最短环)

    题目链接:http://codeforces.com/contest/1206/problem/D 给n个点,如果点a[ i ] &a[ j ] 不为0,则点a[ i ] 和 a[ j ] 直 ...

  9. Codeforces 932.C Permutation Cycle

    C. Permutation Cycle time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. 网页布局 CSS实现DIV并列等高

    同事去笔试遇到的问题 要求3列div根据内容的变化通过css实现等高 效果图: 原理: 同列的div设置一个父级 overflow:hidden 超出部分隐藏 给同列的div设置css margin- ...

  2. SQL触发器中的inserted表和deleted表

    开发也有年头了,但是触发器确实用的比较少,但是无容置疑触发器确实不错, 最近项目要求需要用到的触发器特别多.频繁,觉得很有必要记录和积累下. 在触发器语句中用两个特殊的表一个是deleted表和ins ...

  3. Mui manifest.json文档说明

    Mui官方地址:https://ask.dcloud.net.cn/article/94 保存在这里,太难找了!!!!!! 以下是完整的manifest.json配置文件,在HBuilder|HBui ...

  4. 039 Android SQLite数据库(了解)

    1.介绍 注意:当有大量相似结构的数据需要存储的时候,需要使用数据库. 2.SQLiteOpenHelper简介 注意:数据库的创建方法总结: (1)定义一个类继承SQLiteOpenHelper o ...

  5. IdentityServer4 学习三

    ClientCredentials客户端类型实现 客户端应用向IdentityServer请求AccessToken,IdentityServer验证通过把AccessToken返回给客户端应用,客户 ...

  6. Java 发送http GET/POST请求

    最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...

  7. 剑指offer48:不用加减乘除做加法

    1 题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 2 思路和方法 位运算符:两个数异或(^)[1^0=1, 1^1=0, 0^0=0, 0^1=1, 5^5 ...

  8. C++Primer 5th Chap10 Generic Algorithms(未完)

    大多数算法定义在头文件algorithm中,在头文件numeric中定义了数值泛型算法. 以find算法为例:在容器的两个迭代器指定的范围内遍历,查找特定值. auto result= cout< ...

  9. Python 数据结构理解分享

    摘要:分享学习Python数据结构的一些理解,主要包含序列(如列表和元组),映射(如字典)以及集合3中基本的数据结构,以及可变和不可变数据类型. Python 中的数据结构是根据某种方式将数据元素组合 ...

  10. Scratch编程:画多边形(八)

    “ 上节课的内容全部掌握了吗?反复练习了没有,编程最好的学习方法就是练习.练习.再练习.一定要记得多动手.多动脑筋哦~~” 01 — 游戏介绍 这节我们将实现:程序提示用户输入数据,然后根据用户的输入 ...