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:\n",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

猜你喜欢

转载自www.cnblogs.com/Accepting/p/11575451.html