并查集三

来源:互联网 发布:深圳创意软件 编辑:程序博客网 时间:2024/06/10 07:07

并查集还有很多要学啊,第一篇文章写的并查集的初始概念然后运用在了图最少还需要修几条路,第二个题是判断图里面这些点有没有形成一个环(里面有并查集的压缩),这个题是


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

23 31 22 31 34 21 23 4
 

Sample Output

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


他判断的是这些bugs的交配

http://www.cnblogs.com/dongsheng/archive/2012/08/08/2627917.html

读了这个的文章感觉才慢慢有思路,在原有基础上要有

/*关于并查集,注意两个概念:按秩合并、路径压缩。1、按秩合并由于并查集一般是用比较高效的树形结构来表示的,按秩合并的目的就是防止产生退化的树(也就是类似链表的树),用一个数组记录各个元素的高度(也有的记录各个元素的孩子的数目,具体看哪种能给解题带来方便),然后在合并的时候把高度小的嫁接到高度大的上面,从而防止产生退化的树。2、路径压缩而另一个数组记录各个元素的祖先,这样就防止一步步地递归查找父亲从而损失的时间。因为并查集只要搞清楚各个元素所在的集合,而区分不同的集合我们用的是代表元素(也就是树根),所以对于每个元素我们只需保存其祖先,从而区分不同的集合。而我们这道题并没有使用纯正的并查集算法,而是对其进行了扩展,我们并没有使用“1、按秩合并”(当然你可以用,那样就需要再开一个数组)我们从“1、按秩合并”得到启示,保存“秩”的数组保存的是    元素相对于父节点的关系  ,我们岂不可以利用这种关系(即相对于父节点的不同秩值来区分不同的集合),从而可以把两个集合合并成一个集合。(注:此代码 relation=0 代表 和父节点同一性别)*/#include<stdio.h>int father[2005];int relation[2005];int find_father(int i){    int t;    if(father[i]==i)        return i;    //计算相对于新的父节点(即根)的秩,relation[t]是老的父节点相对于新的父节点(即根)的秩,relation[i]是i元素相对于老的父节点的秩,    //类似于物理里的相对运动,得到的r[i]就是相对于新的父节点(即根)的秩。而且这个递归调用不会超过两层    t=father[i];        father[i]=find_father(father[i]);    relation[i]=(relation[i]+relation[t]+1)%2;   //注意递归中把这棵树relation中的的值都更新一遍,这句的顺序 不能 和上一句 调换位置    // relation[a]的改变是伴随着father[a]的改变而更新的(有father改变就有relation改变),要是father改变了,而relation未改变,此时的relation就记录了一个错误的值,    //father未改变(即使实际的father已不是现在的值,但只要father未改变,relation的值就是“正确”的,认识到这点很重要。)    return father[i];}void merge(int a,int b){    int x,y;    x=find_father(a);    y=find_father(b);    father[x]=y;    relation[x]=(relation[b]-relation[a])%2;//relation[a]+relation[x]与relation[b]相对于新的父节点必须相差1个等级,因为他们不是gay}                                            //x下边的节点不用改,因为查找的时候会自动更新int main(){    int T,m,n,i,j,a,b,flag;    scanf("%d",&T);    for(i=1;i<=T;++i)    {        flag=0;        scanf("%d%d",&n,&m);        for(j=1;j<=n;++j)       //初始化        {            father[j]=j;            relation[j]=1;        }        for(j=1;j<=m;++j)        {            scanf("%d%d",&a,&b);            if(find_father(a)==find_father(b))            {            //    if(relation[a]!=(relation[b]+1)%2)                if(relation[a]==relation[b])            //说明是同性                    flag=1;            }            else                merge(a,b);        }        if(flag)            printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);        else            printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);    }    return 0;}


  ************************

#include<cstdio>  #include<cstring>  //using namespace std;  int f[2023],rank[2023];//rank[]表示0为和根节点是同性,1为和根节点是异性  void Init(int n)  {      for(int i=0;i<=n+1;i++)      {          f[i] = i;          rank[i] = 0;      }  }  int work(int x)  {      if(x==f[x])          return x;      int tem = f[x];      int ss = rank[tem];      f[x] = work(f[x]);      rank[x] = rank[x]^rank[tem];  //rank[tem]是代表x的父亲节点和最新的根节点的关系                                      //现在知道了rank[tem]和自己跟父亲的关系即右边的rank[x],      return f[x];                //要更新自己跟最新根节点的关系,可知这是异或的关系。                          //例如父亲节点跟最新节点是1即是异性,而自己跟父亲节点是0即是同性,                          //那自己跟最新节点就是1即是异性。。                          //还有三种情况可以自己列出来。。     }                                     int main()  {      int n;      scanf("%d",&n);      int bugnum,ed,x,y;      for(int i=1;i<=n;i++)      {          scanf("%d%d",&bugnum,&ed);          Init(bugnum);          int re = 0;          for(int j=0;j<ed;j++)          {              scanf("%d%d",&x,&y);              if(!re)              {                  int xx = work(x);                  int yy = work(y);                  if(xx==yy && rank[x]==rank[y])                      re = 1;                  else                  {                      f[xx] = yy;                      rank[xx] = !(rank[x]^rank[y]); //这句话是将两棵树的根节点合起来,那本来两棵树的                  }                       //根节点的rank[]值都是0,现在要更新xx,即xx成为了yy的子树              }                       //那一定要更新rank[xx]的值。本来rank[x]和rank[y]分别为相对xx跟yy          }                           //的性别,并且x跟y是新进入的节点,所以他们是异性。                                      //所以xx相对于yy的性别为!(rank[x]^rank[y])          if(re==1)              printf("Scenario #%d:\nSuspicious bugs found!\n\n",i);          else              printf("Scenario #%d:\nNo suspicious bugs found!\n\n",i);      }      return 0;  }  


0 0
原创粉丝点击