G - A Bug's Life

Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Appoint description:

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
3 4

Sample Output

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

Hint

Huge input,scanf is recommended.
 
本题大概提议是给出n个bug和m对关系,每对关系都是异性进行的,问你是否有同性恋的关系
解决方法主要是路径压缩,还有怎么处理两个大的集合和如何去合并集合,我们应该把同性之间划分到一个集合里面
由于并查集其实就是一个构成的树,所以我们尽量在合并的时候将高度低的树合并到高度高的树中,我们在这里可以定义数组,rank
rank【x】代表的就是第x个节点所在树的高度,我们为了减少时间复杂度,尽量将高度低的节点转移到高度高的节点上去,这里判断一下rank的高低,之后用father【】数组合并就可以了
之后我们在定义个other数组,这个数组记录的是以自己为节点,之前和我匹配过得异性的节点:::例如other【x=3】=5,其实代表的就是我本身是节点3,我之前和5发生过关系,
这样处理的用处主要是加入后面3,和4再次配对的话,我们明显可以看出来,那么4和5属于同性关系,那么我们应该讲4,和5合并,如果other【3】!=0那么我们Union(otehr【3】,4),其实
也就是Union了(4,5);
 
解决此题的方法还有通过递归回溯的方法,别人的题解里,自己去看吧
 
 #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int father[maxn];
int other[maxn];
int rank[maxn];
int m,n; void init(){
for(int i=;i<=n;i++){
father[i]=i;
rank[i]=;
other[i]=;
}
} int get_father(int x){
if(x!=father[x])
father[x]=get_father(father[x]);
return father[x];
} void Union(int x,int y){
int t1=get_father(x);
int t2=get_father(y);
if(rank[t1]>rank[t2])
father[t2]=t1;
else{
father[t1]=t2;
if(rank[t1]==rank[t2])
rank[t2]++;
}
} int main(){
int t;
scanf("%d",&t);
int cnt=;
while(t--){
bool flag=false;
scanf("%d%d",&n,&m);
init();
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
int t1=get_father(x);
int t2=get_father(y);
if(flag)
continue;
if(t1==t2){
flag=true;
continue;
}
if(other[x]){
Union(other[x],y);
}
else
other[x]=y;
if(other[y])
Union(other[y],x);
else
other[y]=x; } printf("Scenario #%d:\n",cnt++);
if(flag) printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
puts("");
}
return ;
}

POJ 2492 并查集扩展(判断同性恋问题)的更多相关文章

  1. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  2. poj 2492 并查集

    思路:当a,b的根节点find(a)与find(b)不同时,就直接将这两个数连接起来.由于每个树的根节点的kind值一定为0,所以,对于a,b的kind值相同,我们就讲其中一个根的kind值变为1,当 ...

  3. POJ 2492 并查集 A Bug's Life

    #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> ...

  4. poj 1984 并查集

    题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 /* POJ 1984 并查集 */ #include <stdio.h& ...

  5. POJ 1182 (经典食物链 /并查集扩展)

    (參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...

  6. POJ 3228 [并查集]

    题目链接:[http://poj.org/problem?id=3228] 题意:给出n个村庄,每个村庄有金矿和仓库,然后给出m条边连接着这个村子.问题是把所有的金矿都移动到仓库里所要经过的路径的最大 ...

  7. poj 1733 并查集+hashmap

    题意:题目:有一个长度 已知的01串,给出多个条件,[l,r]这个区间中1的个数是奇数还是偶数,问前几个是正确的,没有矛盾 链接:点我 解题思路:hash离散化+并查集 首先我们不考虑离散化:s[x] ...

  8. poj 3310(并查集判环,图的连通性,树上最长直径路径标记)

    题目链接:http://poj.org/problem?id=3310 思路:首先是判断图的连通性,以及是否有环存在,这里我们可以用并查集判断,然后就是找2次dfs找树上最长直径了,并且对树上最长直径 ...

  9. poj 1797(并查集)

    http://poj.org/problem?id=1797 题意:就是从第一个城市运货到第n个城市,最多可以一次运多少货. 输入的意思分别为从哪个城市到哪个城市,以及这条路最多可以运多少货物. 思路 ...

随机推荐

  1. Bootstrap3.0学习第十二轮(导航、标签、面包屑导航)

    详情请查看http://aehyok.com/Blog/Detail/18.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  2. 改善C#程序的建议3:在C#中选择正确的集合进行编码

    要选择正确的集合,我们首先要了解一些数据结构的知识.所谓数据结构,就是相互之间存在一种或多种特定关系的数据元素的集合.结合下图,我们看一下对集合的分类. 集合分类 在上图中,可以看到,集合总体上分为线 ...

  3. Mac上git的安装配置与使用简述

    Mac下git搭建及使用 之前就只是经常在GitHubs上下载代码,也没注意怎么上传项目.一开始对git都没什么了解花了几个小时去小补了下知识.如果有需要可以转去这里学习:[GIT使用简易指南] (h ...

  4. eclipse快捷键的使用及概述

    <eclipse快捷键的使用及概述> <Eclipse概述>       Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其本身而言,它只是一个框架和一组服 ...

  5. 修改ssh的访问端口号

    [root@redis143 ~]# vim /etc/ssh/sshd_config 修改其中的:Port 10056 重启sshd服务 同时如果有防火墙规则的话,注意修改防火墙规则,或者关闭防火墙 ...

  6. iOS边练边学--NSURLConnection发送HTTP请求以及NSString和NSData的相互转换

    HTTP请求的常见方法 GET 所有参数拼接在URL后面,并且参数之间用&隔开 比如http://520it.com?name=123&pwd=345 传递了2个参数给服务器 name ...

  7. .net架构设计读书笔记--第二章 设计体系结构

    第五节 探索领域架构 一.领域驱动设计的价值与意义 最初在java中使用,.net要晚些才引入.领域驱动设计出现之初的争议.一个向导,少走弯路   1. 我们真的需要DDD吗? DDD并不适用于每个软 ...

  8. CSS_复习

    //这个可以作为补白居中的替补方法<!doctype html> <html> <head> <meta charset="utf-8"& ...

  9. onclik的使用.

    //好笨啊,这个居然忘记了,在行间家onclick事件要加();,addEventListener只要使用函数名字就好了 <!doctype html> <html> < ...

  10. Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

    使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...