A Bug's Life POJ - 2492【拓扑】【BFS染色】

先把我的错误代码发出来,也是想了解到为什么这样会错,如果有大神恰巧路过可否在评论讲解一番:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int N,M;
int mp[2005];
bool flag=true;
int main()
{
    int T;
    scanf("%d",&T);
    for(int Case=1; Case<=T; Case++)
    {
        scanf("%d%d",&N,&M);
        memset(mp, 0, sizeof(mp));
        flag=true;
        while(M--)
        {
            int e1,e2;
            scanf("%d%d",&e1,&e2);
            if(!flag) continue;
            if(mp[e1])
            {
                if(!mp[e2]) mp[e2]=3-mp[e1];
                else
                {
                    if(3-mp[e1]!=mp[e2]) flag=false;
                }
            }
            else
            {
                if(!mp[e2])
                {
                    mp[e1]=1;
                    mp[e2]=2;
                }
                else
                {
                    mp[e1]=3-mp[e2];
                }
            }
        }
        printf("Scenario #%d:\n",Case);
        if(flag) printf("No suspicious bugs found!\n");
        else printf("Suspicious bugs found!\n");
    }
    return 0;
}

  感谢大佬的讲解,上述算法却是有些漏洞,因为在面对两对未知匹配的情况下会出现不确定性:举个例子吧:1->2; 3->4; 2->3这样三条路径,由于我的处理方式边输入边处理,所以在1->2和3->4这里会出现不确定性导致2与3的关系具有不确定关系。

改完了!!!发一下题和我的思维!

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

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

 

题目大意:就是对于一个图判断它是否是拓扑结构。

思路

  先对于每个点相互关系建图,可以看到这样的图不是一份图就是多份,对于每个树,我们从树上的其中一个点开始往后BFS找,下一个顶点与这个顶点不同但是每个隔了一个的点(又相互没有其他衔接情况下)要相等,所以我想到了(0、1、2)的做法,“0”代表这个点没有访问过(初始化情况下);“1”代表男的;“2”代表女的;1与1或者2与2之间不能有关系(有关系就是BUG)!

BFS:

void bfs(int u)
{
    mp[u]=1;
    while(!Q.empty()) Q.pop();
    Q.push(u);
    while(!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        int len=(int)vt[temp].size();
        for(int i=0; i<len; i++)
        {
            int v=vt[temp][i];
            if(mp[v]==0)
            {
                mp[v]=3-mp[temp];
                Q.push(v);
            }
            else
            {
                if(mp[v]==mp[temp])
                {
                    flag=false;
                    return;
                }
            }
        }
    }
}

  完整代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int N,M;
vector<int> vt[2005];
int mp[2005];
bool flag=true;     //flag在此
queue<int> Q;
void bfs(int u)
{
    mp[u]=1;
    while(!Q.empty()) Q.pop();
    Q.push(u);
    while(!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        int len=(int)vt[temp].size();
        for(int i=0; i<len; i++)
        {
            int v=vt[temp][i];
            if(mp[v]==0)
            {
                mp[v]=3-mp[temp];
                Q.push(v);
            }
            else
            {
                if(mp[v]==mp[temp])
                {
                    flag=false;
                    return;
                }
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int Case=1; Case<=T; Case++)
    {
        scanf("%d%d",&N,&M);
        flag=true;
        memset(mp, 0, sizeof(mp));
        for(int i=1; i<=N; i++)
        {
            vt[i].clear();
        }
        while(M--)
        {
            int e1,e2;
            scanf("%d%d",&e1,&e2);
            vt[e1].push_back(e2);
            vt[e2].push_back(e1);
        }
        for(int i=1; i<=N; i++)
        {
            if(mp[i]==0) bfs(i);
            if(!flag) break;
        }
        printf("Scenario #%d:\n",Case);
        if(flag) printf("No suspicious bugs found!\n");
        else printf("Suspicious bugs found!\n");
        if(Case<T) cout<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值