(简单) POJ 2492 A Bug's Life,二分染色。
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.
判断奇环,二分染色bfs,并查集都可做。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 20时49分42秒
// File Name : 2492.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int map1[MaxN][MaxN];
int flag=;
int vis[MaxN];
int N;
int que[MaxN],first,last; bool bfs(int u)
{
int v; first=last=;
que[last++]=u;
vis[u]=; while(last-first)
{
u=que[first++]; for(v=;v<=N;++v)
if(map1[u][v]==flag && v!=u)
{
if(vis[v]==vis[u])
return ; if(!vis[v])
{
vis[v]=-vis[u];
que[last++]=v;
}
}
} return ;
} bool getans()
{
memset(vis,,sizeof(vis));
first=last=; for(int i=;i<=N;++i)
if(!vis[i])
if(bfs(i))
return ; return ;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T,cas=;
int Q;
int a,b; scanf("%d",&T); while(T--)
{
scanf("%d %d",&N,&Q);
++flag; while(Q--)
{
scanf("%d %d",&a,&b);
map1[a][b]=map1[b][a]=flag;
} printf("Scenario #%d:\n",cas++); if(getans())
puts("Suspicious bugs found!\n");
else
puts("No suspicious bugs found!\n");
} return ;
}
(简单) POJ 2492 A Bug's Life,二分染色。的更多相关文章
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
- poj 2492 a bug's life 简单带权并查集
题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. #include<ios ...
- 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条臭 ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- POJ 2492 A Bug's Life(带权并查集)
题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
- POJ 2492 A Bug's Life
传送门:A Bug's Life Description Background Professor Hopper is researching the sexual behavior of a rar ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
随机推荐
- acm课程练习2--1001
题目描述 Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and ...
- L10,not for jazz
expressions: It is called a clavichord这被称为古钢琴 a friend of my father's我父亲的朋友 words: musical,adj,音乐的 ...
- 第15章 I/O(输入/输出)
在变量.数组和对象中存储的数据是暂时存在的,程序结束后它们就会丢失.为了能够永久地保存创建的数据,需要将其保存在磁盘文件中,这样就可以在其它程序中使用它们.Java的I/O技术可以将数据保存到文本文件 ...
- 格式化一个文件的大小(size),或者说是格式化一个app的大小(size)
long number = 6243161; Formatter.formatFileSize(context, number): 需要导包,import android.text.format.Fo ...
- js 中ajax请求时设置 http请求头中的x-requestd-with= ajax
今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...
- src和href的区别
<html><!--头部标签--><!--src和href的区别src:引用,该资源是页面不可缺少的一部分,如(img标签 video标签 radio标签);href:引 ...
- 在 Android 中调用二进制可执行程序(native executable )
前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下. Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件.只不过Android 限制了直接的方式只 ...
- gdb 远程调试android进程 -转
什么是gdb 它是gnu组织开发的一个强大的unix程序调试工具,我们可以用它来调试Android上的C.C++代码. 它主要可以做4件事情: 随心所欲地启动你的程序. 设置断点,程序执行到断点处会停 ...
- ARM系统中函数调用过程中的参数传递-转
在 嵌入式软件编程中,经常会用到函数调用,之前在学习如何在C语言中嵌入汇编时有了解到C语言之前的参数调用是使用寄存器R0传递第一个参数,R1传递到第 二个..一直到R3传递第四个参数.但是实际上有时可 ...
- 10、end关键字和Fibonacci series: 斐波纳契数列
# Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 #复合赋值表达式,a,b同时赋值0和1 while b < 10: print(b ...