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

题目链接

题解:现在有n个数,要是a[i]&a[j](i≠j)不等于0的话i跟j之间有一条边,现在让你求出这n个点里面的最小环,要是没有最小环则输出-1。

题解:首先我们明白&操作是在二进制下,某一个都是1,&操作该位才是1,现在我们就提前处理出来,0-64位每位都多少有在该位是1,要是有3个数在某一位都是1,那不用说这三个数肯定是构成环了,直接输出3。否则我们直接Floyd找最小环就行了,可能有些人会认为Floyd,O(n^3)的复杂度过不了,但是在不满足每一位都没有三个数在该位是1的条件下,说明剩下的最多128个点(肯定没有124个点,因为每个数肯定没有64位),所以直接找就行了。

我DFS找环也过了,但是时间比Floyd慢,62ms的是Floyd,现在把两种代码都发出来吧。

 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;
}

 

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值