poj2492--A Bug's Life(并查集变形)
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 28703 | Accepted: 9350 |
Description
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
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
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
Source
#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's Life(并查集变形)的更多相关文章
- poj2492 A Bug's Life【并查集】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- hdu1829 A Bug's Life(并查集)
开两个并查集.然后合并的时候要合并两次.这样在合并之前推断是否冲突,假设不冲突就进行合并,否则不须要继续合并. #include<cstdio> #include<cstdlib&g ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- A Bug's Life(加权并查集)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- POJ_2492 A Bug's Life 【并查集】
一.题面 POJ2492 二.分析 并查集判断类别的题目感觉套路都差不多. 还是先判断在不在一个集合里,在一个集合里才能判断是否同类. 若不在一个集合里则需要将这两个点联系起来. 关于联系起来后关系的 ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!
A Bug's Life Find them, Catch them 都是并查集构造的题,不久前 ...
随机推荐
- Ruby学习笔记(二)——从管道读取数据
在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...
- 谷歌开源可视化工具Facets,将用于人+AI协作项目研究——无非就是一个用于特征工程探索的绘图工具集,pandas可以做的
见:http://www.infoq.com/cn/news/2017/07/goole-sight-facets-ai https://github.com/PAIR-code/facets/blo ...
- Java-MyBatis:MyBatis XML 文件
ylbtech-Java-MyBatis:MyBatis XML 文件 1.返回顶部 1. Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大, ...
- Laravel异常处理
Laravel异常处理 标签(空格分隔): php 自定义异常类 <?php namespace App\Exceptions; use Throwable; use Exception; cl ...
- Python中functools模块函数解析
Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...
- 蓝牙音箱BluetoothA2dp
package myapplication.com.mybuletooch; import android.support.v7.app.AppCompatActivity; import andro ...
- swift使用查阅资料备份4
Swift - RxSwift的使用详解6(观察者2: 自定义可绑定属性) http://www.hangge.com/blog/cache/detail_1946.html extension UI ...
- Object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...
- 将数据内容动态添加到HTML中
// 申明一个数组用来装遍历的元素 var li = []; //遍历元素并加载到标签中 for(var i = 0; i<navGroup.self_first_nav.length; i++ ...
- GDB Layout
layout:用于分割窗口,可以一边查看代码,一边测试.主要有以下几种用法:layout src:显示源代码窗口layout asm:显示汇编窗口layout regs:显示源代码/汇编和寄存器窗口l ...