# Codeforces Round #580 (Div. 2) D. Shortest Cycle（Floyd找最小环或DFS）

## 题目链接

Floyd：

#pragma comment(linker, "/STACK:102400000,102400000")

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>

const int mod = 998244353;
const int maxn = 2e2 + 5;
const int inf = 1e7;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int n;
int maps[maxn][maxn], dist[maxn][maxn];
int sum[maxn];

int Floyd(int n) {

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j)
dist[i][j] = maps[i][j];
}
int ans = inf;
for (int k = 0; k < n; k++) {
for (int i = 0; i < k; i++)
for (int j = i + 1; j < k; j++)
ans = min(ans, dist[i][j] + maps[i][k] + maps[k][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
if (ans == inf)
ans = -1;
return ans;
}

int main() {
cin >> n;
ll temp;
vector<ll> q;
for (int i = 0; i < maxn; i++)
for (int j = i + 1; j < maxn; j++)
maps[i][j] = maps[j][i] = inf;
for (int i = 1; i <= n; i++) {
scanf("%lld", &temp);
if (temp)
q.push_back(temp);
int pos = 0;
while (temp) {
if (temp % 2)
sum[pos]++;
temp /= 2, pos++;
}
}
if (n <= 2)
puts("-1");
else {
for (int i = 0; i <= 64; i++) {
if (sum[i] >= 3) {
cout << 3 << endl;
return 0;
}
}
for (int i = 0; i < q.size(); i++) {
for (int j = i + 1; j < q.size(); j++) {
if (q[i] & q[j])
maps[i][j] = maps[j][i] = 1;
}
}
printf("%d\n", Floyd(q.size()));
}
return 0;
}

DFS：

#pragma comment(linker, "/STACK:102400000,102400000")

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>

const int mod = 998244353;
const int maxn = 2e5 + 5;
const int inf = 1e9;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int st, n;
int ans;
vector<int> s[maxn];
vector<int> maps[maxn];
bool flag = 0;

void dfs(int pos, int fa, int sum) {
if (pos == st && sum) {
ans = min(ans, sum);
return;
} else if (sum >= n) {
flag = 1;
return;
} else if (sum >= ans || flag) {
return;
}
for (int i = 0; i < maps[pos].size(); i++) {
if (fa == maps[pos][i])
continue;
dfs(maps[pos][i], pos, sum + 1);
}
}

int main() {
cin >> n;
ll temp;
for (int i = 1; i <= n; i++) {
scanf("%lld", &temp);
int pos = 0;
while (temp) {
if (temp % 2)
s[pos].push_back(i);
temp /= 2, pos++;
}
}
if (n <= 2)
puts("-1");
else {
for (int i = 0; i <= 64; i++) {
if (s[i].size() >= 3) {
cout << 3 << endl;
return 0;
}
}
for (int i = 0; i <= 64; i++) {
if (s[i].size() <= 1)
continue;
for (int j = 0; j < s[i].size(); j++) {
for (int k = j + 1; k < s[i].size(); k++) {
maps[s[i][j]].push_back(s[i][k]);
maps[s[i][k]].push_back(s[i][j]);
}
}
}
ans = inf;
for (int i = 1; i <= n; i++) {
st = i, flag = 0;
dfs(st, -1, 0);
}
if (ans != inf)
printf("%d\n", ans);
else
puts("-1");
}
return 0;
}

08-25 47

08-19 60

08-19 51

03-13 100

08-19 66

02-26 39

08-19 178

08-19 78

08-19 28

09-26 28

#### Codeforces Round #580 (Div. 2)D. Shortest Cycle(容斥+floy最小环)

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。