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;
    }
    
    


     

  • 相关阅读:
    linux 相关( 随时更新)
    django admin 修改批量操作内容
    区块链
    关于读取excel 和 写excel
    git基本用法
    服务器与本地的控制工具unison
    爬虫框架 Scrapy
    爬虫高性能 asyncio库 twisted库 tornado库
    爬虫请求库之selenium模块
    爬虫请求库
  • 原文地址:https://www.cnblogs.com/vermouth/p/3710190.html
Copyright © 2011-2022 走看看