题目大意:给出一些虫子之间的关系,看是否有同性恋。
对于任意两个虫子,若有关系则在这两个节点之间连边,如果有同性恋,则肯定会有奇数步的回路,若没有,则不存在同性恋。(二分图的判定)
#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; }