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

题面

给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i->j的边和j->i的边看作一条。问连边完图的最小环长度

\(n \leq 10^5,0 \leq a_i \leq 10^{18}\)

分析

我们按位考虑.显然满足第i位为1的所有数两两之间都有边,构成一个完全图.

统计第i位为1的数,如果第i位为1的数超过2个,就直接输出3(这3个构成一个最小环)。如果有2个,就连一条边.注意点的编号要离散化,因为前面可能有很多0,导致满足条件的(i,j)编号很大。

因为要建图的时候,每一位最多建一条边,边数<64,点数<128,floyd求最小环\(O(n^3)\)可以卡过

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f3f3f3f3f3f
#define maxv 1000
#define maxn 100000
using namespace std;
typedef long long ll;
int n;
ll a[maxn+5];
vector<int>vis[70];
int cnt=0;
int tp[maxn+5];
ll ans=0;
ll edge[maxv+5][maxv+5];
ll dist[maxv+5][maxv+5];
void floyd(){
for(int k=1;k<=cnt;k++){
for(int i=1;i<k;i++){
for(int j=i+1;j<k;j++){
if(dist[i][j]==INF||edge[i][k]==INF||edge[k][j]==INF) continue;
//防止加法溢出
if(dist[i][j]+edge[i][k]+edge[k][j]<ans){
ans=dist[i][j]+edge[i][k]+edge[k][j];
}
}
}
for(int i=1;i<=cnt;i++){
for(int j=1;j<=cnt;j++){
if(dist[i][j]>dist[i][k]+dist[k][j]){
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
for(ll i=0;i<64;i++){
for(int j=1;j<=n;j++){
if(a[j]&(1ll<<i)) vis[i].push_back(j);
}
}
for(int i=0;i<64;i++){
if(vis[i].size()>2){
printf("3\n");
return 0;
}
}
for(int i=0;i<64;i++){
if(vis[i].size()==2){
tp[++cnt]=vis[i][0];
tp[++cnt]=vis[i][1];
}
}
sort(tp+1,tp+1+cnt);
cnt=unique(tp+1,tp+1+cnt)-tp-1;
memset(edge,0x3f,sizeof(edge));
memset(dist,0x3f,sizeof(dist));
ans=INF;
for(int i=0;i<64;i++){
if(vis[i].size()==2){
int u=lower_bound(tp+1,tp+1+cnt,vis[i][0])-tp;
int v=lower_bound(tp+1,tp+1+cnt,vis[i][1])-tp;
// printf("%d %d\n",u,v);
edge[u][v]=edge[v][u]=1;
}
}
memcpy(dist,edge,sizeof(edge));
floyd();
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}

[Codeforces 1205B]Shortest Cycle(最小环)的更多相关文章

  1. @codeforces - 1205B@ Shortest Cycle

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

  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. CF 1206D - Shortest Cycle Floyd求最小环

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

  4. Codeforces 1206 D - Shortest Cycle

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

  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. [CF580C]Shortest Cycle(图论,最小环)

    Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...

  8. B. Shortest Cycle 无向图求最小环

    题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...

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

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

随机推荐

  1. 接触oracle的第二天

    1.2 Sqlplus 这是一个轻量级的功能强大的客户端, 是 dba 必须掌握的工具. 我们可以配置 sqlplus 的一些行为,两个命令: show. 用来显示配置参数 set. 用来设置配置参数 ...

  2. MySQL 5.7

    博客地址:https://www.cnblogs.com/runningStudy/p/6444047.html mysql官网下载地址:https://downloads.mysql.com/arc ...

  3. cookie、session和中间件

    目录 cookie和session cookie与session原理 cookie Google浏览器查看cookie Django操作cookie 获取cookie 设置cookie 删除cooki ...

  4. javaScript中的 call 和 apply

    call 和apply都可以实现函数的调用 // 普通函数的调用 function foo() { console.log('foo'); } foo(); // foo foo.call(); // ...

  5. LeetCode--094--二叉树的中序遍历(python)

    递归 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # ...

  6. [UVA160]Factors and Factorials 题解

    前言 这道题目本身毫无技术含量珂言,但是输出格式珂以调一年 题解 这道题让我们求\(N!\)中每个质数的个数. 一种方法是直接模拟,枚举\(N!\)中的每个元素,然后暴力查看每个数含有有多少质数. 但 ...

  7. HDU - 6578 Blank DP + 滚动数组

    HDU - 6578 Blank 题意 给你\(\{0,1,2, 3\}\)四个数,分别填入长度为\(n\)的数列中,有\(m\)个限制条件,\(l_{i}, r_{i}, x_{i}\)表示在\([ ...

  8. 2018 CCPC 秦皇岛 I (状压DP)

    题意: 首先t组数据  (t<=5),一个n代表有n件东西,每个东西可以代表两个物品,商品或者袋子,每个都有个值,如果这个要代表袋子的话,当前就代表是容量,而且必须把其他几件不是袋子的物品放一些 ...

  9. android中各种组件的生命周期问题

    1,activiy生命周期 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/ 结合ativity的状态转换来看才 ...

  10. kubeadm快速部署kubernetes(十九)

    安装要求 部署Kubernetes集群机器需要满足以下几个条件: 一台或多台机器,操作系统 CentOS7.x-86_x64 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘30GB或更多 ...