[Codeforces 1205B]Shortest Cycle(最小环)
[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(最小环)的更多相关文章
- @codeforces - 1205B@ Shortest Cycle
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个长度为 n 的正整数序列 a1, a2, ..., an ...
- 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 ...
- CF 1206D - Shortest Cycle Floyd求最小环
Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...
- Codeforces 1206 D - Shortest Cycle
D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...
- D. Shortest Cycle(floyd最小环)
D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- D. Shortest Cycle
D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...
- [CF580C]Shortest Cycle(图论,最小环)
Description: 给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环. Solut ...
- B. Shortest Cycle 无向图求最小环
题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...
- codeforces 962F.simple cycle(tarjan/点双连通分量)
题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...
随机推荐
- Github使用进阶
1 Github常用词: watch:会持续收到该项目的动态 fork:复制某个项目到自己的Github仓库中 star:可以理解为点赞 clone:将项目下载至本地 follow:关注你感兴趣的作者 ...
- 046:ORM模型介绍
ORM模型介绍: 随着项目越来越大,采用写原生SQL的方式在代码中会出现大量的SQL语句,那么问题就出现了: 1.SQL语句重复利用率不高,越复杂的SQL语句条件越多,代码越长.会出现很多相近的SQL ...
- 【leetcode】1124. Longest Well-Performing Interval
题目如下: We are given hours, a list of the number of hours worked per day for a given employee. A day i ...
- jQuery 遍历 - 同胞(siblings)
同胞拥有相同的父元素. 通过 jQuery,您能够在 DOM 树中遍历元素的同胞元素. 在 DOM 树中水平遍历 有许多有用的方法让我们在 DOM 树进行水平遍历: siblings() next() ...
- 特征提取算法(4)——LoG特征提取算法
目录 1.介绍 2.LoG原理 3.数学原理 4.模板性质 1.介绍 LoG(DoG是一阶边缘提取)是二阶拉普拉斯-高斯边缘提取算法,先高斯滤波然后拉普拉斯边缘提取. Laplace算子对通过图像进行 ...
- shell后台进程
建立duplicate_removal.sh文件,内容如下:#!/bin/bashmysql -vvv -u root -p123456 test -e "truncate t_target ...
- Oracle JET(三)Oracle JET 响应布局
Oracle JET 提供了一系列的 CSS 样式以直接使用. Oracle JET CSS样式链接:http://docs.oracle.com/middleware/jet310/jet/refe ...
- requirejs define a module
https://requirejs.org/docs/api.html#define Define a Module § 1.3 A module is different from a tradit ...
- C/C++判断字符串是否包含某个子字符串
C风格 #include <iostream> #include <string> #include <cstring> using namespace std; ...
- sh/bash/csh/Tcsh/ksh/pdksh等shell的区别
w shell confusion..what is diff between bash, ksh, csh, tcsh..?? http://www.linuxquestions.org/ques ...