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.
 
 
 
 
数据量大要用scanf

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<queue>
#include<iomanip>
#include<iostream>
using namespace std;
#define MAXN 2003
#define INF 0x3f3f3f3f
typedef long long LL;
/*带权并查集,
虫子只和不同性别交配,所以并查集距离为1,如果有不匹配,就标记
*/
int pre[MAXN],rank[MAXN],n,m;
int find(int x)
{
if(pre[x]==-)
return x;
int fx = pre[x];
pre[x] = find(pre[x]);
rank[x] = (rank[x]+rank[fx])%;
return pre[x];
}
bool mix(int x,int y)
{
int fx = find(x),fy=find(y);
if(fx==fy)
{
if(rank[x]!=(rank[y]+)%)
return false;
return true;
}
pre[fy] = fx;
rank[fy] = (rank[x]-rank[y]++)%;
return true;
}
void Init()
{
memset(rank,,sizeof(rank));
memset(pre,-,sizeof(pre));
}
int main()
{
int t;
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d%d",&n,&m);
Init();
int x,y;
bool f = false;
for(int j=;j<m;j++)
{
scanf("%d%d",&x,&y);
if(f) continue;
if(!mix(x,y))
f = true;
}
printf("Scenario #%d:\n",i);
if(f)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}

J - A Bug's Life 并查集的更多相关文章

  1. nyoj 209 + poj 2492 A Bug's Life (并查集)

    A Bug's Life 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Background  Professor Hopper is researching th ...

  2. 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条臭 ...

  3. hdu 1829 A Bug's Life(并查集)

                                                                                                    A Bu ...

  4. poj 2492A Bug's Life(并查集)

    /* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...

  5. POJ 2492 A Bug's Life 并查集的应用

    题意:有n只虫子,每次给出一对互为异性的虫子的编号,输出是否存在冲突. 思路:用并查集,每次输入一对虫子后就先判定一下.如果两者父亲相同,则说明关系已确定,再看性别是否相同,如果相同则有冲突.否则就将 ...

  6. 牛客网多校第4场 J Hash Function 【思维+并查集建边】

    题目链接:戳这里 学习博客:戳这里 题意: 有n个空位,给一个数x,如果x%n位数空的,就把x放上去,如果不是空的,就看(x+1)%n是不是空的. 现在给一个已经放过数的状态,求放数字的顺序.(要求字 ...

  7. poj2492_A Bug's Life_并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 34947   Accepted: 11459 D ...

  8. [poj2492]A Bug's Life(并查集+补集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 34678   Accepted: 11339 D ...

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

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

随机推荐

  1. [C++ STL] 各容器简单介绍

    什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...

  2. 51nod1381 硬币游戏

    1381 硬币游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有一个简单但是很有趣的游戏.在这个游戏中有一个硬币还有一张桌子,这张桌子上有很多平 ...

  3. CodeForces - 7D Palindrome Degree

    最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...

  4. 网站开发综合技术 第一部分HTML 1.3.2表单

    <form id="" name="" method="post/get" action="负责处理的服务端"&g ...

  5. asp.net ajax get post 中文乱码解决办法

    前台: var username = $("#UserName").val(); var tel = $("#tel").val(); var yzm = $( ...

  6. Sass 主要知识点小记

    Sass 主要知识点小记 以前写样式的时候,每个元素的颜色,背景色都需要重新写一遍,然后就想CSS难道没有变量么?最后就查到Sass.但当时没有静下心来好好的看一下,今天正好有时间,就在这里边看边整理 ...

  7. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  8. 简单工厂模式&工厂方法模式&抽象工厂模式的区别

    之前写过一篇关于工厂模式(Factory Pattern)的随笔,里面分析了简单工厂模式,但对于工厂方法和抽象工厂的分析较为简略.这里重新分析分析三者的区别,工厂模式是java设计模式中比较简单的一个 ...

  9. 【原】thinkphp实现存储session至redis

    Thinkphp\Library\Think\Session\Driver中新建redis缓存文件:Redis.class.php Thinkphp\Common\function.php 中 fun ...

  10. java中通用权限管理设计(转)

    原文地址:http://www.cnblogs.com/a7345678/archive/2008/09/25/1298838.html 转自博客园暗夜精灵-鬼才阁 实现业务系统中的用户权限管理 B/ ...