D - A Bug's Life 二分图 并查集

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

两种方法

1:二分图的判定 ,如果说其构成的图为二分图   那么没有问题,否则 有问题

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N= +;
vector<int >ve[N];
int mark[N];
bool flag=false ;
bool bfs(int x){
queue<int >que;
que.push(x);
mark[x]=;
while(que.size()){
int x1=que.front();
que.pop();
for(int i=;i<ve[x1].size();i++){
int dx=ve[x1][i];
if(mark[x1]==){
if(mark[dx]==) return true;
else if(mark[dx]==){
mark[dx]=;
que.push(dx);
}
}
else {
if(mark[dx]==) return true;
else if(mark[dx]==){
mark[dx]=;
que.push(dx);
}
}
}
}
return false;
}
int main(){
int t,k=;
cin>>t;
while(t--){
flag =false ;
int n,m;
k++;
scanf("%d%d",&n,&m);
memset(mark,,sizeof(mark));
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
}
for(int i=;i<=n;i++){
if(mark[i]==){
if(bfs(i)){
flag=true;
break;
}
}
}
printf("Scenario #%d:\n",k);
if(flag) puts("Suspicious bugs found!");
else puts("No suspicious bugs found!");
puts("");
}
return ;
}

2并查集

链接”:::https://www.cnblogs.com/geloutingyu/p/6117262.html

A Bug's Life POJ 2492的更多相关文章

  1. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  2. (并查集)A Bug's Life -- POJ -- 2492

    链接: http://poj.org/problem?id=2492 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  3. A Bug’s Life POJ - 2492(种类并查集)

    题目链接 每次给出两个昆虫的关系(异性关系),然后发现这些条件中是否有悖论 就比如说第一组数据 1 2 2 3 1 3 1和2是异性,2和3是异性,然后说1和3是异性就显然不对了. 我们同样可以思考一 ...

  4. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  5. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  7. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  8. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  9. (简单) POJ 2492 A Bug's Life,二分染色。

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

随机推荐

  1. hdu1548 奇怪的电梯 dfs dijkstra bfs都可以,在此奉上dfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/5706/ 简单的规定深度进行搜索,代码如下: #include<bits/stdc++.h> usin ...

  2. JSP(三)----EL表达式

    ##  EL表达式 1.概念:Expression  alnguage  表达式语言 2.作用:替换和简化JSP页面中java代码的编写 3.语法:${表达式} 4.注意: *  jsp默认支持EL表 ...

  3. PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境配置

    0x01 Xdebug安装 参考:https://xdebug.org/docs/install cd xdebug-/ phpize sudo ./configure --enable-xdebug ...

  4. windows找不到文件gpedit.msc处理方法

    新建一个txt,输入 @echo offpushd "%~dp0"dir /b C:\Windows\servicing\Packages\Microsoft-Windows-Gr ...

  5. 理解MapReduce计算构架

    用Python编写WordCount程序任务 程序 WordCount 输入 一个包含大量单词的文本文件 输出 文件中每个单词及其出现次数(频数),并按照单词字母顺序排序,每个单词和其频数占一行,单词 ...

  6. python—nnlog日志

      #when='S'每秒产生一个[D天默认 H M S]# backCount='5'## level是设置打印级别默认是debug级别(下面是四个级别可以指定打印) import nnlog lo ...

  7. IOS部分APP使用burpsuite抓不到包原因

    曾经在ios12的时候,iphone通过安装burpsuite的ca证书并开启授权,还可以抓到包,升级到ios13后部分app又回到以前连上代理就断网的情况. 分析:ios(13)+burpsuite ...

  8. 5个最佳WordPress通知栏插件

    作者:品博客 链接:https://blog.pingbook.top/328/ 来源:品博客 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. WordPress通知栏可有效地将 ...

  9. 面试刷题37:微服务是什么?springcloud,springboot是什么?

    面试中被问到为什么要使用微服务架构?springcloud的核心组件有哪些? 拿我们国家的兵种来说,如何把战争这个单体架构微服务化,就是根据适用的场景,拆分出不同的兵种(微服务) 然后每个兵种之间通过 ...

  10. Android AIDL[Android Interface Definition Language]跨进程通信

    全称与中文名IPC:Inter-Process Communication(进程间通信)Ashmem:Anonymous Shared Memory(匿名共享内存)Binder:Binder(进程间通 ...