[CF580C]Shortest Cycle(图论,最小环)
Description:
给 \(n\) 个点的图,点有点权 \(a_i\) ,两点之间有边当且仅当 \(a_i\ \text{and}\ a_j \not= 0\),边权为1,求最小环。
Solution:
按每一位考虑若当前这一位为 1 的点超过了 2 个,那么答案就为 3 。
否则只会连一条边,于是最多只有 \(60\) 条边,枚举每条边删掉,求最短路 (边权为1,bfs) 即可。
#include <iostream>
#include <set>
#include <queue>
#include <cstring>
#include <cstdio>
#include <fstream>
typedef long long LL;
typedef unsigned long long uLL;
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define MP(x, y) std::make_pair(x, y)
#define DE(x) cerr << x << endl;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO cerr << "GO" << endl;
using namespace std;
inline void proc_status()
{
ifstream t("/proc/self/status");
cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
const int maxN = 1e5 + 2;
int n;
LL a[maxN];
int dis[maxN];
bool vis[maxN];
int ans(0x3f3f3f3f);
vector<int> g[maxN];
set<pair<int, int> > S;
void add(int u, int v)
{
g[u].push_back(v);
g[v].push_back(u);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("xhc.in", "r", stdin);
freopen("xhc.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 0; i < 62; ++i)
{
int cnt = 0;
for (int j = 1; j <= n; ++j)
{
if (a[j] >> i & 1)
cnt++;
}
if (cnt >= 3)
{
cout << 3 << endl;
return 0;
}
if (cnt != 2)
continue;
int first = 0, second = 0;
for (int j = 1; j <= n; ++j)
if (a[j] >> i & 1)
{
if (!first)
{
first = j;
}
else
{
second = j;
break;
}
}
S.insert(MP(first, second));
}
for (auto p : S)
add(p.first, p.second);
for (auto p : S)
{
int s = p.first, t = p.second;
memset(vis, 0, sizeof vis);
memset(dis, 0x3f, sizeof dis);
vis[s] = 1;
queue<int> q;
q.push(s);
dis[s] = 0;
while (q.size())
{
int u = q.front();
q.pop();
for (int v : g[u])
{
if (u == s and v == t)
continue;
if (!vis[v])
{
q.push(v);
vis[v] = 1;
dis[v] = dis[u] + 1;
}
}
}
if (dis[t] < 0x3f3f3f3f) chkmin(ans, dis[t] + 1);
}
if (ans < 0x3f3f3f3f) cout << ans << endl;
else cout << -1 << endl;
return 0;
}
[CF580C]Shortest Cycle(图论,最小环)的更多相关文章
- CF 1206D - Shortest Cycle Floyd求最小环
Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...
- D. Shortest Cycle(floyd最小环)
D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- [Codeforces 1205B]Shortest Cycle(最小环)
[Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...
- 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 ...
- Codeforces 1206 D - Shortest Cycle
D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...
- D. Shortest Cycle
D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...
- B. Shortest Cycle 无向图求最小环
题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论
D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...
- 并不对劲的复健训练-CF1205B Shortest Cycle
题目大意 有\(n\)(\(n\leq 10^5\))个数\(a_1,...,a_n\)(\(a\leq 10^{18}\)).有一个图用这个方法生成:若\(a_i\)按位与\(a_j\)不为0,则在 ...
随机推荐
- moongoose对象无法新增删除属性
昨天用nodes中的moongoose去查询一个结果遇到一个大坑,这个坑貌似用moongoose可能会遇到.背景是这样的,我在nodejs中去查询document,得到的可以看作是一个对象list.在 ...
- JavaScript基础1——在末尾添加节点
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript —— 关于for in 与 for of 的区别
for in是ES5标准,遍历key,遍历的是数组的索引(即键名): for of是ES6标准,遍历value,遍历的是数组元素值: Object.prototype.objCustom = func ...
- Redux中间件之redux-thunk使用详解
Redux的核心概念其实很简单:将需要修改的state都存入到store里,发起一个action用来描述发生了什么,用reducers描述action如何改变state tree .创建store的时 ...
- windows下如何安装pip
在安装pip前,请确认win系统中已经安装好了python,和easy_install工具 Python完成后 配置环境变量 在环境变量中添加Python目录 (1) 右键点击"计算机&qu ...
- ArrayList,Vector, LinkedList的存储性能和特性?
ArrayList,Vector, LinkedList的存储性能和特性? ArrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入或删除时非常麻烦. ...
- Python实例教程
转自:http://codingdict.com/article/9026 Python 100例-01 题目: 输有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数? Python 1 ...
- python-Exception异常使用
Exception #自定义异常类 ,MyInputExcp继承Exception异常 class MyInputExcp(Exception): def __init__(self, lenght, ...
- action function
Action委托具有Action<T>.Action<T1,T2>.Action<T1,T2,T3>……Action<T1,……T16>多达16个的重载 ...
- Android逆向之旅---解析编译之后的Dex文件格式
一.前言 新的一年又开始了,大家是否还记得去年年末的时候,我们还有一件事没有做,那就是解析Android中编译之后的classes.dex文件格式,我们在去年的时候已经介绍了: 如何解析编译之后的xm ...