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的自然数不能重复 思路 假如只有一个数 ...
随机推荐
- Semantic Monocular SLAM for Highly Dynamic Environments面向高动态环境的语义单目SLAM
一.摘要 当前单目SLAM系统能够实时稳定地在静态环境中运行,但是由于缺乏明显的动态异常处理能力,在动态场景变化与运动中往往会失败.作者为解决高度动态环境中的问题,提出一种语义单目SLAM架构,结合基 ...
- Chrome-AdGuard 无与伦比的广告拦截扩展
一款无与伦比的广告拦截扩展,对抗各式广告与弹窗. AdGuard 广告拦截器可有效的拦截所有网页上的所有类型的广告,甚至是在 Facebook.Youtube 以及其他万千网站上的广告! AdGuar ...
- Python下载网络图片方法汇总与实现
本文介绍下载python下载网络图片的方法,包括通过图片url直接下载.通过re/beautifulSoup解析html下载以及对动态网页的处理等. 很多人学习python,不知道从何学起.很多人 ...
- 迭代列表不要For循环,这是Python列表推导式最基本的概念
如果你还在使用 For 循环迭代列表,那么你需要了解了解列表推导式,看看它的基本概念都是什么. 列表解析式(List comprehension)或者称为列表推导式,是 Python 中非常强大和优雅 ...
- 教你看懂Docker和K8S!
转载于 https://my.oschina.net/jamesview/blog/2994112 2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫“dotCloud”的公司. 这家公司主要 ...
- C# winform 弹出窗体给父窗体传值
Winform程序有很多传值的方法,抱着学习的态度.利用委托注册事件的方法,给窗体统一添加事件: 首先定义一个Frm_Base: namespace 任意 { public partial class ...
- 图论算法(一)存图与STL第六弹——vector容器
图论算法(一)存图 我发现我的博客阅读量贼低,问小伙伴们,ta们都说这些博客太长了QAQ! 今天来个短亿点的(也短不了多少……) 进入正题,图论究竟是什么? 图论就是给你一张图,让你在这张图上进行各种 ...
- 【算法•日更•第三十期】区间动态规划:洛谷P4170 [CQOI2007]涂色题解
废话不多说,直接上题: P4170 [CQOI2007]涂色 题目描述 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符 ...
- hadoop2.6虚拟机安装
Linux环境设置/*安装Hadoop集群时要确保以下几个方面1.本机与虚拟机之间是否可以通信(Ping).如果不行检查下面 1.1本机防火墙关闭(开启情况是本机可以ping虚拟机,虚拟机不能ping ...
- SparkStreaming概述
Spark Streaming 是Spark核心API的一个扩展,可以实现高吞吐量的.具备容错机制的实时流数据的处理. ◆ 支持从多种数据源获取数据,包括Kafka.Flume.Twitter.Zer ...