zoukankan      html  css  js  c++  java
  • 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, jj (iji≠j) are connected if and only if, aiaiAND aj0aj≠0, where AND denotes the bitwise AND operation.

    Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.

    Input

    The first line contains one integer n(1n105)(1≤n≤105) — number of numbers.

    The second line contains nn integer numbers a1,a2,,ana1,a2,…,an (0ai10180≤ai≤1018).

    Output

    If the graph doesn't have any cycles, output 1−1. Else output the length of the shortest cycle.

    Examples
    input
    Copy
    4
    3 6 28 9
    
    output
    Copy
    4
    input
    Copy
    5
    5 12 9 16 48
    
    output
    Copy
    3
    input
    Copy
    4
    1 2 4 8
    
    output
    Copy
    -1
    Note

    In the first example, the shortest cycle is (9,3,6,28)(9,3,6,28).

    In the second example, the shortest cycle is (5,12,9)(5,12,9).

    The graph has no cycles in the third example.

     代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    #define lson m<<1,l,mid
    #define rson m<<1|1,mid+1,r
    #define getmid(m) (tree[m].l+tree[m].r)>>1;
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    pair<int,int>pp;
    vector<int>vec[65];
    vector<int>G[maxn];
    ll a[maxn];
    int vis[maxn];
    int endd;
    int minn=0x3f3f3f3f;
    void  dfs(int sta,int dep)
    {
        vis[sta]=1;
        if(dep>=minn)
        {
              vis[sta]=0;
              return;
        }
        for(int t=0;t<G[sta].size();t++)
        {  
          int u=G[sta][t];
          if(u==endd&&dep>1)
          {
              minn=min(minn,dep+1);
          }
          if(vis[u]==0)
          {
              dfs(u,dep+1);
          }
        }
        vis[sta]=0;  
    }
     
    int main()
    {
        int n;
        cin>>n;
        for(int t=0;t<n;t++)
        {
            scanf("%lld",&a[t]);
        }
        for(int t=0;t<64;t++)
        {
            for(int j=0;j<n;j++)
            {
                if(a[j]&((1ll<<t)))
                {
                 vec[t].push_back(j);
                }
            }
        }
        for(int t=0;t<64;t++)
        {
            if(vec[t].size()>=3)
            {
                puts("3");
                return 0;
            }
        }
        for(int t=0;t<64;t++)
        {
            if(vec[t].size()==2)
            {
                int u=vec[t][0];
                int v=vec[t][1];
                G[u].push_back(v);
                G[v].push_back(u);
            }
        }
        for(int t=0;t<n;t++)
        {
            endd=t;
            dfs(t,0);
        }
        if(minn==0x3f3f3f3f)
        {
            puts("-1");
        }
        else
        {
        printf("%d
    ",minn);
        }
       // system("pause");
        return 0;
    }
  • 相关阅读:
    python爬虫系列之爬取多页gif图像
    python连续爬取多个网页的图片分别保存到不同的文件夹
    python多线程同步
    python多线程简单例子
    python定时器爬取豆瓣音乐Top榜歌名
    python模拟Get请求保存网易歌曲的url
    python使用get在百度搜索并保存第一页搜索结果
    python爬取某个网页的图片-如百度贴吧
    完全揭秘log file sync等待事件-转自itpub
    两表关联更新
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11379619.html
Copyright © 2011-2022 走看看