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. DevOps - DevOps精要 - 歧途

    前言 如果在实施DevOps的过程中,周围没有一个人支持你,也没有得到领导和团队成员的理解: 如果在采用DevOps的工具和方法之后,难以获得明显的效率提升,甚至得到了不少的消极反馈: 那就需要反省一 ...

  2. unix 命令

    ubuntu  命令窗口的打开 打开命令行窗口: Ctrl+Alt+T 在打开的命令行窗口中打开一个新的Tab: Ctrl+Shift+T 在同一窗口的Tab间切换: Ctrl+Page Up 或者 ...

  3. centos7 双网口绑定

    1.关闭和停止NetworkManager服务 systemctl stop NetworkManager.service # 停止NetworkManager服务 systemctl disable ...

  4. Linux 缺少 mime.types 文件 mailcap

    问题描述: 一个项目当中使用的是 ossfs 挂载的一个 oss,在系统上传附件时,比如图片或视频时, 它的头信息为,application/octet-stream,上传后直接为二进制文件,访问的话 ...

  5. P1993 小K的农场(差分约束)

    小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...

  6. DAO工具类的封装源码

    详细源码见下表,绝对原创,转载请注明出处! package com.ydj.util; import java.sql.Connection; import java.sql.PreparedStat ...

  7. php中比较复杂但又常用的字符串函数

    php系统核心库自带的函数中,字符串比数组函数较为简单,但还是有一些较为复杂但又很常用的函数,比如下面的这些函数 explode()函数 用一个字符串来分割另一个字符串,返回结果是一个数组 explo ...

  8. oracle加密--wallet

    SQL> SELECT * FROM V$ENCRYPTION_WALLET; WRL_TYPE WRL_PARAMETER STATUS -------------------- ------ ...

  9. 使用keepalived实现kubenetes apiserver高可用

    # 安装 nginx yum install nginx -y # 配置nginx4层代理 /etc/nginx/nginx.conf stream { upstream kube-apiserver ...

  10. Android--文件存取

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundExc ...