hdu 1829 A Bug's Life(判断二分図)

4434 ワード

テーマリンク:http://acm.hdu.edu.cn/showproblem.php?pid=1829
A Bug's Life
Time Limit:15000/5000 MS(Java/Others)    メモリLimit:32768/32768 K(Java/Others)
Total Submission(s):1212    Acceepted Submission(s):3952
Problem Description
Background Professor Hopper is reearching the sexual behavior of a rare species of bugs.He asumes that the y feature two different and that the nly interact with bugs of the opopate geders.Induinent。because numbers were present on their backs.Problem Given a list of bug interactions,decide whethe the experiment supports his assion of two genders withのhomosexual bugs or if it contains.init.init。
 
Input
The first line of the input contains the number of scenaros.Each scenaro starts with one line giving the number of bugs(at least one,and up to 2000)and the number of interactionseach interaction is given in the form of two distinct bug numbers separated by a single space.Bugs are numbend consecutively starting from one.
 
Output
The output for everry scenario is a line containing“Scienaro龛:”、where i is the number of the scenar starting at 1、フォローアップby one line saying einther“No spicious bugs found!”if Professor Hopper's assimpone is definitely wrong.
 
Sample Input

   
   
   
   
2 3 3 1 2 2 3 1 3 4 2 1 2 3 4
 
Sample Output

   
   
   
   
Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found!
Hint
Huge input,scanf is recommended.
 
ソurce
TUD Prograamming Contect 2005,Darmstadt,Germany
 
Recommund
linle   |   We have carefully selected several simiar probles for you:  1558 1811 2473 3172 3234 
同性愛があるかどうかを判断する関係を与え、簡単に言うと二分図かどうかを判断します。
ここでは図を作る方法を新しく学び,離散化して図を作る。
問題解決の考え方:二分図の判断+bfsは、離散化しないとクリティカルREを配列します。
詳細はコードを参照してください
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int judge[2010];
int n,m;
int num,head[2010];

struct edge
{
    int st,ed,next;
} e[2010*1000];

void addedge(int x,int y)
{
    e[num].st=x;
    e[num].ed=y;
    e[num].next=head[x];
    head[x]=num++;
    e[num].st=y;
    e[num].ed=x;
    e[num].next=head[y];
    head[y]=num++;
}

bool bfs(int x)
{
    queue<int>q;
    q.push(x);
    judge[x]=0;
    //cout<<x<<endl;
    while (!q.empty())
    {
        int s=q.front();
        q.pop();
        for(int i=head[s]; i!=-1; i=e[i].next)
        {
            //cout<<judge[i]
            if (judge[e[i].ed]==-1)
            {
                judge[e[i].ed]=(judge[s]+1)%2;
                q.push(e[i].ed);
            }
            else
            {
                if (judge[e[i].ed]==judge[s])
                    return false;
            }
        }
    }
    return true;
}

int main()
{
    int flag;
    int tt=1;
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d",&n,&m);
        flag=1;
        //memset(Map,0,sizeof(Map));
        memset(judge,-1,sizeof(judge));
        int a,b;
        num=0;
        memset(head,-1,sizeof(head));
        for (int i=1; i<=m; i++)
        {
            scanf("%d%d",&a,&b);
            addedge(a,b);
        }
        for (int i=1; i<=n; i++)
        {
            if(judge[i]==-1)
            {
                if (!bfs(i))
                    flag=0;
            }
        }
        printf ("Scenario #%d:
",tt++); if (flag==1) printf ("No suspicious bugs found!
"); else printf ("Suspicious bugs found!
"); printf ("
"); } return 0; }