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, jj (i≠ji≠j) are connected if and only if, aiaiAND aj≠0aj≠0, where AND denotes the bitwise AND operation.
Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
The first line contains one integer nn (1≤n≤105)(1≤n≤105) — number of numbers.
The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤10180≤ai≤1018).
If the graph doesn't have any cycles, output −1−1. Else output the length of the shortest cycle.
4
3 6 28 9
4
5
5 12 9 16 48
3
4
1 2 4 8
-1
In the first example, the shortest cycle is (9,3,6,28)(9,3,6,28).
In the second example, the shortest cycle is (5,12,9)(5,12,9).
The graph has no cycles in the third example.
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define lson m<<1,l,mid
#define rson m<<1|1,mid+1,r
#define getmid(m) (tree[m].l+tree[m].r)>>1;
const int maxn=1e5+;
typedef long long ll;
using namespace std;
pair<int,int>pp;
vector<int>vec[];
vector<int>G[maxn];
ll a[maxn];
int vis[maxn];
int endd;
int minn=0x3f3f3f3f;
void dfs(int sta,int dep)
{
vis[sta]=;
if(dep>=minn)
{
vis[sta]=;
return;
}
for(int t=;t<G[sta].size();t++)
{
int u=G[sta][t];
if(u==endd&&dep>)
{
minn=min(minn,dep+);
}
if(vis[u]==)
{
dfs(u,dep+);
}
}
vis[sta]=;
}
int main()
{
int n;
cin>>n;
for(int t=;t<n;t++)
{
scanf("%lld",&a[t]);
}
for(int t=;t<;t++)
{
for(int j=;j<n;j++)
{
if(a[j]&((1ll<<t)))
{
vec[t].push_back(j);
}
}
}
for(int t=;t<;t++)
{
if(vec[t].size()>=)
{
puts("");
return ;
}
}
for(int t=;t<;t++)
{
if(vec[t].size()==)
{
int u=vec[t][];
int v=vec[t][];
G[u].push_back(v);
G[v].push_back(u);
}
}
for(int t=;t<n;t++)
{
endd=t;
dfs(t,);
}
if(minn==0x3f3f3f3f)
{
puts("-1");
}
else
{
printf("%d\n",minn);
}
// system("pause");
return ;
}
Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)的更多相关文章
- 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路
B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...
- Codeforces Round #580 (Div. 1)
Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...
- Codeforces Round #580 (Div. 1) A-E
Contest Page A Tag:构造 将$a_i$看做一个无穷数列,$i > 2n$时$a_i = a_{i - 2n}$.设$sgn_i = \sum\limits_{j=i+1}^{i ...
- Codeforces Round #580 (Div. 2)
这次比上次多A了一道,但做得太慢,rating还是降了. Problem A Choose Two Numbers 题意:给出两个集合A,B,从A,B中分别选出元素a,b使得a+b既不属于集合A,又不 ...
- Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;const int maxn=300;cons ...
- Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(DFS)
链接: https://codeforces.com/contest/1230/problem/E 题意: Kamil likes streaming the competitive programm ...
- Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)
题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...
- Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)
https://codeforces.com/contest/1065 题意 给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复 思路 假如只有一个数 ...
随机推荐
- SQL关联查询
从2张或多张表中,取出有关联的数据 关联查询一共有几种情况: 内连接:INNER JOIN .CROSS JOIN (1)形式一 select 字段列表 from A表 inner join B表 o ...
- 【小白学AI】XGBoost 推导详解与牛顿法
文章转自公众号[机器学习炼丹术],关注回复"炼丹"即可获得海量免费学习资料哦! 目录 1 作者前言 2 树模型概述 3 XGB vs GBDT 3.1 区别1:自带正则项 3.2 ...
- 2020-04-29:现在你有个秒杀抢购的app,用户不断大量增加,技术层面,你要怎么做
2020-04-29:现在你有个秒杀抢购的app,用户不断大量增加,技术层面,你要怎么做,才能既满足用户需求,又能扛住压力,还能帮公司合理支出?福哥答案2020-04-29: 限流(杀部分用户祭天). ...
- C#LeetCode刷题之#892-三维形体的表面积(Surface Area of 3D Shapes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4136 访问. 在 N * N 的网格上,我们放置一些 1 * 1 ...
- C#设计模式之16-迭代器模式
迭代器模式(Iterator Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/417 访问. 迭代器模式 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题之#20-有效的括号(Valid Parentheses)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4018 访问. 给定一个只包括 '(',')','{','}',' ...
- Mac开发工具
便捷管理你的Mac App Homebrew:https://brew.sh/index_zh-cn 强大的文本编辑器 Sublime Text:http://www.sublimetext.com ...
- 在UOS 上部署 django + uwsgi + nginx 流程
前言:这篇主要是流程,不喜勿喷,虽然我知道在部署过程中 参照博客写的越详细越好. 强大的百度会解决一切的 爬了诸多坑 ,心累,必须总结!! 最近 芯片封锁闹的很凶 支持国产!! UOS 统一操作系统 ...
- SpringBoot ---yml 整合 Druid(1.1.23) 数据源
SpringBoot ---yml 整合 Druid(1.1.23) 数据源 搜了一下,网络上有在配置类写 @Bean 配置的,也有 yml 配置的. 笔者尝试过用配置类配置 @Bean 的方法,结果 ...