zoukankan      html  css  js  c++  java
  • (简单) POJ 2492 A Bug's Life,二分染色。

    Description

    Background 
    Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 
    Problem 
    Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

      判断奇环,二分染色bfs,并查集都可做。

    代码如下:

    // ━━━━━━神兽出没━━━━━━
    //      ┏┓       ┏┓
    //     ┏┛┻━━━━━━━┛┻┓
    //     ┃           ┃
    //     ┃     ━     ┃
    //     ████━████   ┃
    //     ┃           ┃
    //     ┃    ┻      ┃
    //     ┃           ┃
    //     ┗━┓       ┏━┛
    //       ┃       ┃
    //       ┃       ┃
    //       ┃       ┗━━━┓
    //       ┃           ┣┓
    //       ┃           ┏┛
    //       ┗┓┓┏━━━━━┳┓┏┛
    //        ┃┫┫     ┃┫┫
    //        ┗┻┛     ┗┻┛
    //
    // ━━━━━━感觉萌萌哒━━━━━━
    
    // Author        : WhyWhy
    // Created Time  : 2015年07月17日 星期五 20时49分42秒
    // File Name     : 2492.cpp
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    const int MaxN=2010;
    
    int map1[MaxN][MaxN];
    int flag=0;
    int vis[MaxN];
    int N;
    int que[MaxN],first,last;
    
    bool bfs(int u)
    {
        int v;
    
        first=last=0;
        que[last++]=u;
        vis[u]=1;
    
        while(last-first)
        {
            u=que[first++];
    
            for(v=1;v<=N;++v)
                if(map1[u][v]==flag && v!=u)
                {
                    if(vis[v]==vis[u])
                        return 1;
    
                    if(!vis[v])
                    {
                        vis[v]=-vis[u];
                        que[last++]=v;
                    }
                }
        }
    
        return 0;
    }
    
    bool getans()
    {
        memset(vis,0,sizeof(vis));
        first=last=0;
    
        for(int i=1;i<=N;++i)
            if(!vis[i])
                if(bfs(i))
                    return 1;
    
        return 0;
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    
        int T,cas=1;
        int Q;
        int a,b;
    
        scanf("%d",&T);
    
        while(T--)
        {
            scanf("%d %d",&N,&Q);
            ++flag;
    
            while(Q--)
            {
                scanf("%d %d",&a,&b);
                map1[a][b]=map1[b][a]=flag;
            }
    
            printf("Scenario #%d:
    ",cas++);
    
            if(getans())
                puts("Suspicious bugs found!
    ");
            else
                puts("No suspicious bugs found!
    ");
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    云计算是什么?它有哪些形式?
    TensorFlow从0到1之浅谈深度学习(5)
    excel如何快速统计出某一分类的最大值?
    Excel怎样根据出生日期,快速计算出其年龄呢?
    Excel只想显示一部分日期,怎样把其余部分隐藏起来?
    Excel数据透视表的日常应用技巧
    人工智能(机器学习)学习之路推荐
    人工智能之常用数据结构与算法(python)
    excel如何快速计算日期对应的生肖?
    excel 如何制作带下拉框的动态折线图表
  • 原文地址:https://www.cnblogs.com/whywhy/p/4655688.html
Copyright © 2011-2022 走看看