A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28703   Accepted: 9350

Description

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

Sample Output

Scenario #1:
Suspicious bugs found! Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany
题目大意:给出n条虫子。m个配对(男<->女)的情况。问数据有没有错误(出现同性配对)?
也就是说每一个给出的ab在不同的阵营,假设查询出现了在同一阵营就出现了错误。
c数组记录属于某一个阵营。d数组记录它所属的阵营的敌对阵营。
对每一组数据出现后。更新两个数组,注意:
假设a属于x,b属于y,那么应该讲d[y]归并到x中,y归并到d[x]中
 
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int c[2100] , d[2100] ;
int find1(int a)
{
if( c[a] != a )
{
c[a] = find1(c[a]) ;
d[a] = d[ c[a] ] ;
}
return c[a] ;
}
int main()
{
int t , tt , i , j , n , m , a , b , flag ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
scanf("%d %d", &n, &m);
for(i = 1 ; i <= n ; i++)
c[i] = i ;
memset(d,-1,sizeof(d));
flag = 0 ;
while(m--)
{
scanf("%d %d", &a, &b);
if( flag ) continue ;
int x , y ;
x = find1(a) ; y = find1(b) ;
if(x == y)
flag = 1 ;
else if( d[x] == -1 && d[y] == -1 )
{
d[x] = y ; d[y] = x ;
}
else if( d[x] != -1 )
{
c[y] = d[x] ;
if( d[y] != -1 )
{
int xx = find1(d[y]);
c[xx] = x ;
d[xx] = y ;
}
d[y] = x ;
}
else
{
c[x] = d[y] ;
if( d[x] != -1 )
{
int yy = find1(d[x]);
c[yy] = y ;
d[yy] = x ;
}
d[x] = y ;
}
}
printf("Scenario #%d:\n", tt);
if( flag )
printf("Suspicious bugs found!\n\n");
else
printf("No suspicious bugs found!\n\n");
}
return 0;
}

poj2492--A Bug&#39;s Life(并查集变形)的更多相关文章

  1. poj2492 A Bug's Life【并查集】

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

  2. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  3. hdu1829 A Bug&#39;s Life(并查集)

    开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...

  4. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

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

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

  6. A Bug's Life(加权并查集)

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

  7. POJ_2492 A Bug's Life 【并查集】

    一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...

  8. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

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

  9. HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!

    A Bug's Life                                                     Find them, Catch them 都是并查集构造的题,不久前 ...

随机推荐

  1. ORACLE查询闪回

    在Oracle中如果错误地提交了修改操作,然后想查看修改前的值,这时候可以使用查询闪回(query flashback). 查询闪回可以根据根据一个时间值或者系统变更号(SCN)进行. 执行闪回操作, ...

  2. 39.Qt XML

    1.使用QXmlStreamReader读取XML,可以读取内存中容纳不了的特大文件,或者读取在XML文档中定制的文档的内容. xml文件(in1.xml) <?xml version=&quo ...

  3. docker compose的使用--在线安装未完成

    Compose 是一个用户定义和运行多个容器的 Docker 应用程序.在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务 ...

  4. BZOJ 2287 DP+容斥

    思路: 先处理出来f[j]表示这i个物品都可用 填满容量j的方案数 容斥一发 处理出来g[j]=g[j-w[i]] 表示i不能用的时候 填满容量j的方案数 //By SiriusRen #includ ...

  5. tp5页面跳转,空控制器空方法

    namespace app\index\controller; use think\Controller; class Login extends Controller{ //显示html页面 pub ...

  6. SpringBoot(九) ElasticSearch 全文检索

    ElasticSearch ​ ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用 ...

  7. DataTables入门

    转载 https://blog.csdn.net/gfd54gd5f46/article/details/65938189

  8. Android琐碎知识点集合

    1.最近发现android studio更新之后用的v7包,每次创建Activity的时候自动继承的是AppCompatActivity,很不舒服,还是习惯Activity.没什么大的毛病,毕竟goo ...

  9. 【AnjularJS系列6 】 过滤器

    第六篇,过滤器 AngularJS 过滤器可用于转换数据: 过滤器 描述 currency 格式化数字为货币格式. filter 从数组项中选择一个子集. lowercase 格式化字符串为小写. o ...

  10. Unity 烘焙的2种方式

    游戏场景通常有许多光源,使用实时渲染会非常消耗性能,解决办法是烘焙,烘焙有2种方式. 1, 在3dmax等模型制作软件中对场景进行烘焙.将烘焙好的模型以及贴图导入到unity3d. 相对复杂但效果好 ...