您的位置:首页 > 其它

POJ 2492 并查集扩展(判断同性恋问题)

2015-12-02 18:05 267 查看
G - A Bug's Life
Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2492

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=2005;
int father[maxn];
int other[maxn];
int rank[maxn];
int m,n;

void  init(){
for(int i=0;i<=n;i++){
father[i]=i;
rank[i]=0;
other[i]=0;
}
}

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=1;
while(t--){
bool flag=false;
scanf("%d%d",&n,&m);
init();
int x,y;
for(int i=1;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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: