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

  • 相关阅读:
    c++构造函数析构函数调用顺序
    c++隐藏实例
    c++子类和父类成员函数重名
    C++虚函数·
    c/c++字符数组和字符串大揭秘
    python 基础回顾 一
    python java scala 单例模式
    推荐一款好用并且免费的markdown软件 Typora
    java 的垃圾回收机制 【转】
    python的垃圾回收机制【转】
  • 原文地址:https://www.cnblogs.com/Accepting/p/11575451.html
Copyright © 2011-2022 走看看