HDU 1829 A Bug's Life

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829


用二分图做的,,,,,,刚学图,,以为这道题有向图跟无向图都没影响,,,贡献了N次WA。。调试了好久才找到原因,,

贡献一组测试数据:

89 11
42 2
43 5
23 54
12 55
76 3
33 89
89 32
54 4
34 32
88 67
56 54

NO...

#include <iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#define MAX_V 2005
using namespace std;

vector <int> g[MAX_V];
int color[MAX_V];           //二分图染色

bool dfs(int v,int c)
{
    int i;
    color[v]=c;         //把节点v染成c色
    for(i=0;i<g[v].size();i++)      //g[v].size()为当前节点的边数
    {
        if(color[g[v][i]]==c)
            return false;
        if(color[g[v][i]]==0 && !dfs( g[v][i],-c) )
            return false;
    }
    return true;
}

void solve(int v)
{
    int i;
    for(i=1; i<=v; i++)
        if(color[i]==0&&!g[i].empty())
        {
            if(!dfs(i,1))
            {
                printf("Suspicious bugs found!
");
                return;
            }
        }
    printf("No suspicious bugs found!
");
}
int main()
{
    int T;
    int i;
    int s,t;
    int v,e;
    int Case;
    scanf("%d",&T);
    for(Case=1; Case<=T; Case++)
    {
        memset(color,0,sizeof(color));
        for(i=0;i<MAX_V;i++)
            g[i].clear();
        scanf("%d%d",&v,&e);
        for(i=0; i<e; i++)
        {
            scanf("%d%d",&s,&t);            //无向图
            g[s].push_back(t);
            g[t].push_back(s);
        }

        printf("Scenario #%d:
", Case);
        solve(v);
        printf("
");

    }

    return 0;
}



原文地址:https://www.cnblogs.com/frankM/p/4399493.html