A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492
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
Output
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 5e4+;
const int mod = 1e9+; struct node
{
int pre;
int relation; //0:同 1:异
}p[maxn]; int find(int x)
{
int temp;
if(x == p[x].pre)
return x;
temp = p[x].pre;
p[x].pre = find(temp);
p[x].relation = (p[x].relation + p[temp].relation + ) % ;
return p[x].pre;
}
void combine(int x,int y)
{
int xx = find(x);
int yy = find(y);
if(xx != yy)
{
p[xx].pre = yy;
p[xx].relation = (p[y].relation - p[x].relation) % ;
}
}
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n,m;
int flag = ;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
p[i].pre = i;
p[i].relation = ;
}
for(int i=;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(find(a) == find(b))
{
if(p[a].relation == p[b].relation)
flag = ;
}
else
combine(a,b);
}
if(flag)
printf("Scenario #%d:\nSuspicious bugs found!\n\n",ca++);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n\n",ca++);
}
}
A Bug's Life POJ - 2492 (带权并查集)的更多相关文章
- poj 1182 (带权并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 71361 Accepted: 21131 Description ...
- A Bug’s Life POJ - 2492(种类并查集)
题目链接 每次给出两个昆虫的关系(异性关系),然后发现这些条件中是否有悖论 就比如说第一组数据 1 2 2 3 1 3 1和2是异性,2和3是异性,然后说1和3是异性就显然不对了. 我们同样可以思考一 ...
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- POJ 1703 带权并查集
直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...
- Navigation Nightmare POJ - 1984 带权并查集
#include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj 2492 A Bug's Life【带权并查集】
就是给一个无向图判是否有奇环 用带权并查集来做,边权1表示连接的两个节点异性,否则同性,在%2意义下进行加法运算即可,最后判相同的时候也要%2,因为可能有负数 #include<iostream ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
随机推荐
- 汇编语言版本的HelloWorld
平台 macOS 工具 nasm clang 文件 main.asm extern _printf ; 这里调用系统的一个系统调用函数, _printf, 使用extern告诉链接器该label在其他 ...
- C#与重构(入门)
C#与代码重构(入门) 重构(Refactoring)就是通过调整程序代码改善软件的质量.性能,使其程序的设计模式和架构更趋合理,提高软件的扩展性和维护性. 单从概念少来理解重构可能很抽象,那么通过下 ...
- 12.Visual Studio 2013中的默认快捷键
这篇大致是IDE的使用技巧,常用的也就那么几个. 我自己用的最多的是注释.取消注释.格式调整.运行测试.开始调试.断开调试.重新开始调试.删除行ctrl+L.保存.全部保存.打开资源管理器.搜索等几个 ...
- 分享一个WPF下日历控件(Calendar)的样式
WPF日历控件的一个样式 WPF自带的日历控件样式可能会比较丑,要修改其样式看起来挺复杂的,实际上很简单,用Blend打开,修改三个模板,基本就能改变全部面貌,也很容易 先上图 样式如下: <S ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第八天(非原创)
文章大纲 一.课程介绍二.Solr基本介绍三.ssm整合Solr四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的 ...
- spring boot 启动报错No active profile set, falling back to default profiles
报错No active profile set, falling back to default profiles pom.xml加上下面两个依赖 <dependency> <gro ...
- Bootstrap插件-carousel(轮播图)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JAVA 与 sqlite3 连接
SQLite SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经 ...
- Java Web中web.xml文件简单介绍
参考博客: https://www.cnblogs.com/Y-oung/p/8401549.html 1.XML 声明和根元素 <?xml version="1.0" en ...
- android通过fiddler代理,抓取网络请求
安装fiddler过程省略 1, 2, 3, 4,手机需要跟电脑处于同一局域网,设置网络代理为电脑在局域网内的ip,端口为3步设置的port 5,电脑就可以通过fiddler监控手机的所有网络请求了( ...