[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. FAT12 img tool

    NJU/2019/OS Description: CODE: Main.cpp: /* @author: Edwin Xu @Date:2019/11/13 @Note: just ASCII */ ...

  2. 在浏览器下载pdf,或者txt文档是会直接打开

    window.location.href = url会直接打开,解释大概是因为浏览器自身可以解析.pdf或者txt.解决方法如下: 本来就要用a标签里面加上download属性的,结果发现不行,就算了 ...

  3. 01 Netty是什么?

    01 Netty是什么? IO编程 我们简化下场景:客户端每隔两秒发送一个带有时间戳的 "hello world" 给服务端,服务端收到之后打印. 为了方便演示,下面例子中,服务端 ...

  4. php开发IDE选择

    优先选择Netbeans,理由如下:: 1.ZendStudio有的方便特性Netbeans也提供,如:ctrl+f5也支持ctrl+shift+r的文件选择功能,[git | svn]团队代码管理. ...

  5. BFC、IFC、GFC和FFC

    基本概念 Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很多个Box 组成的.元素的类型和 display 属性,决定了这个 Box 的类型. 不同类型的 Box, 会参与不 ...

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

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

  7. JavaScript modularity with RequireJS (from spaghetti code to ravioli code)

    http://netmvc.blogspot.com/2012/11/javascript-modularity-with-requirejs.html Today I would like to d ...

  8. php system exexc 立即返回

    有时候会用到php调用服务器端的其它可执行文件,system和exec函数都是阻塞执行的,执行完第三方程序再返回. 如果我们需要立即返回,让第三方程序在后台继续执行,调用方式如下: linux,noh ...

  9. How to derive mean and variance of a Gaussian?

    PRML exercise 1.8: To derive mean: change of variable z = x - u, use symmetry To derive variance: di ...

  10. LinkedTransferQueue 源码分析

    LinkedTransferQueue LinkedTransferQueue 能解决什么问题?什么时候使用 LinkedTransferQueue? 1)LinkedTransferQueue 是基 ...