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

  • 相关阅读:
    【转载】mysqldump的single-transaction和master-data
    MySQL 从库日志比主库多
    Error_code: 2003
    通过替换frm文件方式修改表结构
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
    批量kill mysql processlist进程
    libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.9-1.el6.x86_64
    MySQL 5.7.9的多源复制
    Java-Clone 对象拷贝
    Windows 运行库
  • 原文地址:https://www.cnblogs.com/Accepting/p/11575451.html
Copyright © 2011-2022 走看看