zoukankan      html  css  js  c++  java
  • hdu 1829 A Bug's Life

    题目大意:给出一些虫子之间的关系,看是否有同性恋。

    对于任意两个虫子,若有关系则在这两个节点之间连边,如果有同性恋,则肯定会有奇数步的回路,若没有,则不存在同性恋。(二分图的判定)

    #include <stdio.h>
    #include <queue>
    using namespace std;
    #define maxn 2010
    int color[maxn];
    queue < int > Que;
    vector < int > g[maxn];
    bool bfs(int s)           //bfs染色
    {
         while (!Que.empty()) Que.pop();
         Que.push(s);
         color[s] = 0;
         int num = 1;
    	 int i;
         while (!Que.empty())
    	 {
    		int pre = Que.front();
            Que.pop();
            for (i = 0; i < g[pre].size(); i ++)
    		{
    			int k =g[pre][i];
                if (color[pre] == color[k])
    				return false; 
                if (color[k] == -1)
    			{
    				num ++;
    			    color[k] = color[pre]^1;
                    Que.push(k);             
                }
             }      
         }
         return true;
    }
    int main()
    {
    	int t;
    	int i,j;
    	int x,y;
    	scanf("%d",&t);
    	int sum=0;
    	int n,m;
    	int flag;
    	while(t--)
    	{
    		flag=0;
    		sum++;
    		memset(color,-1,sizeof(color));
    		scanf("%d%d",&n,&m);
    		for(i=1;i<=n;i++) g[i].clear();
    		for(i=1;i<=m;i++)
    		{
    			scanf("%d%d",&x,&y);
    			g[x].push_back(y);
    			g[y].push_back(x);
    		}
    		for(i=1;i<=n;i++)
    			if(color[i]==-1&&!bfs(i))
    			{
    				flag=1;
    				break;
    			}
    		printf("Scenario #%d:
    ",sum);
    		if(flag) printf("Suspicious bugs found!
    ");
    		else printf("No suspicious bugs found!
    ");
    		printf("
    ");
    	}
    	return 0;
    }
    
    


     

  • 相关阅读:
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    HAService 刨坑
    RocketMQ服务器监控误区
    Send [1] times, still failed
    RECONSUME_LATER
    RocketMQ 自定义文件路径
    RocketMQ 运维指令
    Thrift 学习记录
    服务网格(Service Mesh)学习记录
    Linux 安装 Apache
  • 原文地址:https://www.cnblogs.com/vermouth/p/3710190.html
Copyright © 2011-2022 走看看