zoukankan      html  css  js  c++  java
  • A Bug's Life POJ 2492

    D - A Bug's Life 二分图 并查集

    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.

    Input

    The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

    Output

    The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

    Sample Input

    2
    3 3
    1 2
    2 3
    1 3
    4 2
    1 2
    3 4

    两种方法  

    1:二分图的判定 ,如果说其构成的图为二分图   那么没有问题,否则 有问题

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int N= 2000+7; 
    vector<int >ve[N];
    int mark[N];
    bool flag=false ;
    bool bfs(int x){
        queue<int >que;
        que.push(x);
        mark[x]=1;
        while(que.size()){
            int x1=que.front();
            que.pop();
            for(int i=0;i<ve[x1].size();i++){
                int dx=ve[x1][i]; 
                if(mark[x1]==1){
                    if(mark[dx]==1)    return true;
                    else if(mark[dx]==0){
                        mark[dx]=2;
                        que.push(dx);
                    }
                }
                else {
                    if(mark[dx]==2) return true;
                    else if(mark[dx]==0){
                        mark[dx]=1;
                        que.push(dx);
                    }
                }
            }
        }
        return false;
    }
    int main(){
        int t,k=0;
        cin>>t;
        while(t--){
            flag =false ;
            int n,m;
            k++;
            scanf("%d%d",&n,&m);
            memset(mark,0,sizeof(mark));
            for(int i=1;i<=n;i++){
                ve[i].clear();
            }
            for(int i=1;i<=m;i++){
                int x,y;
                scanf("%d%d",&x,&y);
                ve[x].push_back(y);
                ve[y].push_back(x);
            }
            for(int i=1;i<=n;i++){
                if(mark[i]==0){
                    if(bfs(i)){
                        flag=true;
                        break;
                    }
                }
            }
            printf("Scenario #%d:
    ",k);
            if(flag) puts("Suspicious bugs found!");
            else puts("No suspicious bugs found!");
            puts("");
        }
        return 0;
    }

    2并查集

    链接”:::https://www.cnblogs.com/geloutingyu/p/6117262.html

  • 相关阅读:
    File
    Include and Require
    Date and Time
    css3的nth-child选择器使用示例
    document.title
    php获取从百度搜索进入网站的关键词的详细代码
    Iphone手机、安卓手机浏览器控制默认缩放大小的方法
    离线宝调用
    织梦DedeCMS网站地图模板
    禁止选择文本和禁用右键 v3.0
  • 原文地址:https://www.cnblogs.com/Accepting/p/11575451.html
Copyright © 2011-2022 走看看