[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内任 ...
随机推荐
- Python图谱
Reference: https://time.geekbang.org/column/article/94311
- 如何使 C++ 的 StringBuilder 提升 4350% 的性能?
介绍 经常出现客户端打电话抱怨说:你们的程序慢如蜗牛.你开始检查可能的疑点:文件IO,数据库访问速度,甚至查看web服务. 但是这些可能的疑点都很正常,一点问题都没有. 你使用最顺手的性能分析工具分析 ...
- mysql AND运算符 语法
mysql AND运算符 语法 作用:在 WHERE 子语句中把两个或多个条件结合起来.佛山大理石方尺 语法:SELECT * FROM 表名 WHERE 字段1 运算符 值 AND 字段2 运算符 ...
- luogu P1162 填涂颜色 x
P1162 填涂颜色 题目描述 由数字0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向.现要求把闭合圈内的所有空间都填写成2.例如:6X6的方阵(n=6),涂色前和 ...
- R 画散点图
ggplot(data=df, aes(x=n, y=rt, group=kernel, shape=kernel, colour=kernel)) + geom_point(fill="w ...
- 滑动报 Unable to preventDefault inside passive event listener due to target being treated as passive 的解决方法
google浏览器滑动出现以下问题: 解决办法如下:在html元素下添加样式 touch-action: none; html{ touch-action:none; }
- [CSP-S模拟测试]:礼物(数学)
题目传送门(内部题80) 输入格式 第一行输入一个正整数$n$. 第二行到第$n+1$行每行两个正整数$a_i$和$b_i$表示第$i$个礼物中包含$a_i$个红宝石和$b_i$个绿宝石. 输出格式 ...
- 使用vue技术应当使用的技术和兼容性选择
假如你的前端框架使用了vue,那你可以大胆地使用以下技术,并忽略其他js和css的兼容性问题,因为 关于vue的兼容性 官方给出了规定 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 ...
- vue +ts 在router的路由中import报错的解决方案
在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...
- GMM demo
# GMM model # // library(mvtnorm) ) n1 = n2 = mu1 = c(,) mu2 = c(-,-) sigma1 = matrix(c(,.,.,),nrow= ...