[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,则在 ...
随机推荐
- Spark Streaming Transformations
map(func):对DStream中的所有的元素进行func转换生成新的DStream flatMap(func):和map方法类似,先对DStream中的元素进行func运算,然后压平,就是说,如 ...
- Maya多版本下载与激活方法
目录 1. 安装激活说明 1. 安装激活说明 Maya2019:https://www.cnblogs.com/coco56/p/11425559.html Maya2017:https://www. ...
- tailf 跟踪日志文件
1.命令功能 tailf 跟踪日志文件增长,作用跟tail –f相同.tailf将输出文件的最后10行,然后等待文件增长. 2.语法格式 tailf option file 参数说明 参数 参数说 ...
- 新特性2-lambda表达式
最近几天学习了一下lambda表达式,看了不少博客,感觉有一篇博客总结的一句话总结的很好:lambda表达式是一段可以传递的代码,它的核心思想是将面向对象中的传递数据变成传递行为.其实以前也有传递行为 ...
- postgresql windows 服务启动失败
1命令行 启动服务 pg_ctl -D "C:\Program Files\PostgreSQL\9.1\data" start 2 查看状态 pg_ctl -D "C: ...
- ps:图层知识
如果我们要改变下左图上方的蓝色小球位置,就需要先创建一个符合小球大小的选区,这并不困难,可以使用椭圆选框工具来创建一个正圆的选区(可通过[空格 CTRL 单击图像]放大图像).之后使用移动工具移动选区 ...
- sed \s
export m1=`free|cut -d ":" -f2|sed -e "s/^\s\s*//g"|head -2|tail -1|cut -d ' ' - ...
- bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...
- 了解卷积神经网络如何使用TDA学习
在我之前的文章中,我讨论了如何对卷积神经网络(CNN)学习的权重进行拓扑数据分析,以便深入了解正在学习的内容以及如何学习它. 这项工作的重要性可归纳如下: 它使我们能够了解神经网络如何执行分类任务. ...
- Nginx-配置负载均衡实例
配置负载均衡实例 实现效果: 配置负载均衡 实验代码 1) 首先准 备两个同时启动的 Tomcat 2) 在 nginx.conf 中进行配置 随着互联网信息的爆炸性增长,负载均衡(load bala ...