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(图论,最小环)的更多相关文章

  1. CF 1206D - Shortest Cycle Floyd求最小环

    Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...

  2. D. Shortest Cycle(floyd最小环)

    D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. [Codeforces 1205B]Shortest Cycle(最小环)

    [Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...

  4. 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 ...

  5. Codeforces 1206 D - Shortest Cycle

    D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...

  6. D. Shortest Cycle

    D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...

  7. B. Shortest Cycle 无向图求最小环

    题意: 给定 n 个点,每个点有一个权值a[i],如果a[u]&a[v] != 0,那么就可以在(u,v)之间连一条边,求最后图的最小环(环由几个点构成) 题解:逻辑运算 & 是二进制 ...

  8. 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 ...

  9. 并不对劲的复健训练-CF1205B Shortest Cycle

    题目大意 有\(n\)(\(n\leq 10^5\))个数\(a_1,...,a_n\)(\(a\leq 10^{18}\)).有一个图用这个方法生成:若\(a_i\)按位与\(a_j\)不为0,则在 ...

随机推荐

  1. ajaxSubmit 实现图片上传 SSM maven

    文件上传依赖: <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> & ...

  2. Python在线IDE | 谷歌Colaboratory云端IDE介绍

    目录 1. 推荐阅读 2. 本文地址 3. 按 4. 使用篇 6. 教学篇 1. 推荐阅读 Python基础入门一文通 | Python2 与Python3及VSCode下载和安装.PyCharm破解 ...

  3. LNMP集群架构篇

    一.LNMP介绍 1.使前端web服务和后端存储服务进行串联 2.主要实现处理php动态请求 工作原理: L:linux  N:nginx  M:mysql   P:php 二.lnmp部署 我的环境 ...

  4. MYSQL学习笔记——数据类型

    mysql的数据类型可以分为三大类,分别是数值数据类型.字符串数据类型以及日期时间数据类型. 数值数据类型                                               ...

  5. no hash tools

    import itertools class Set(list):    def __init__(self, params):        super(Set, self).__init__()  ...

  6. [HEOI2015]兔子与樱花(贪心)

    [HEOI2015]兔子与樱花 Description 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由\(n\)个树枝分叉点组成,编号从\ ...

  7. Angular Viewchild undefined

    Angular的viewchild在使用的时候报错 undefined 1 检查是否在元素上打上标识 #xxx 2 查看引用元素时的时机 是否在AfterViewInit之后 3 检查元素是否在*ng ...

  8. 【vue】vue不足 待补强

    83719279 9:56:03尤其是路由 声明周期 父子通信 组件通信 以及钩子函数83719279 9:56:17这些我都不敢用,只能用最原始的方法83719279 9:56:32还有es6 js ...

  9. Spring3.x 升级至 Spring4.x 详解

    1 升级依赖包 1.1 Maven 项目 1.1.1 更新 spring 依赖版本 打开 pom.xml,把所有 spring3.x 的版本号更新为 spring4.x.建议使用属性配置,形如: &l ...

  10. TCP TIME_WAIT和CLOSE_WAIT

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484451.html 使用如下指令查看当前Server的TCP状态 netstat -n | awk ...