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. 【The final】软件工程实践总结

    软件工程就这么告一段落了,竟然有那么一丢丢的舍不得-- 一.为拖延找的种种借口     [首先声明]以下纯粹是个人吐槽,仅作记录以便日后自己可以回顾一下往昔罢了,可以直接忽略,跳到第二大点:我的拖延之 ...

  2. 关于obj和基本类通过函数参数传进去执行是否改变原来的值

    var obj = { p1 : 1, p2 : 2 }; (function(_/* 这个东东是地址的应用哦 */){ _.p1 = 3, _.p2 = 4 })(obj) var i = 2; ( ...

  3. 【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D The famous Korean internet co ...

  4. Spring JdbcTemplate 的使用与学习

    JDBCTemplate 是SPRING 框架自带的一种对sql 语句查询的封装 ,封装非常完善,虽然与Hibernate比起来有一点麻烦,但是学号JDBCTemplate可以让我们用Spirngmv ...

  5. VMWare File Format Learning && Use VHD File To Boot VMWare && CoreOS Docker Configuration And Running

    目录 . Virtual Machine Introduce . Vmware Image File Format . VHD File Format . Convert VHD File Into ...

  6. JSP 九个隐含JSP对象

    输入输出对象:request.response.out. 作用域通信对象:session.application.pageContext servlet对象:page.config 错误对象:exce ...

  7. appium-向右滑动定位

    上面5幅欢迎图,要向右滑动4次再点击[立即体验]才可以到首屏 #首页欢迎图滑动4次 for i in range(4): driver.swipe(1200, 200, 10, 200, 1500) ...

  8. iOS开发的那些坑

    最近重新拿起了iOS的开发,使用OC和Swift混编,碰到了一些比较棘手的问题,在这里记录下来,方便自己以后或他人不再入坑.这篇文章的内容包含: UITableViewCell的真实结构在iOS的环境 ...

  9. leach协议matlab仿真代码

    http://www.ilovematlab.cn/thread-177006-1-1.html LEACH協議clear;%清除內存變量 xm=100;%x軸範圍ym=100;%y軸範圍 sink. ...

  10. javascript判断文件大小

    <input type="file" id="fileName" name ="fileName" onchange="Ge ...