zoukankan      html  css  js  c++  java
  • POJ 2438 Children’s Dining (哈密顿图模板题之巧妙建反图 )

    题目链接

    Description

    Usually children in kindergarten like to quarrel with each other. This situation annoys the child-care women. For instant, when diner time comes, a fierce conflict may break out when a certain couple of children sitting side by
    side who are hostile with each other. Although there aren't too many children dining at the same round table, but the relationship of "enemy" or "friend" may be very complex. The child-care women do come across a big problem. Now it is time for you to help
    them to figure out a proper arrangement of sitting, with which no two "enemy" children is adjacent.
    Now we assume that there are 2 * n children who sit around a big table, and that none has more than n - 1 "enemies".

    Input

    The input is consisted of several test blocks. For each block, the first line contains two integers n and m (1 <= n <= 200, 0 <= m <= n (n - 1)). We use positive integers from 1 to 2 * n to label the children dining round table.
    Then m lines followed. Each contains positive integers i and j ( i is not equal to j, 1 <= i, j <= 2 * n), which indicate that child i and child j consider each other as "enemy". In a input block, a same relationship isn't given more than once, which means
    that if "i j" has been given, "j i" will not be given.
    There will be a blank line between input blocks. And m = n = 0 indicates the end of input and this case shouldn't be processed.

    Output

    For each test block, if the proper arrangement exist, you should print a line with a proper one; otherwise, print a line with "No solution!".

    Sample Input

    1 0

    2 2
    1 2
    3 4

    3 6
    1 2
    1 3
    2 4
    3 5
    4 6
    5 6

    4 12
    1 2
    1 3
    1 4
    2 5
    2 6
    3 7
    3 8
    4 8
    4 7
    5 6
    5 7
    6 8

    0 0

    Sample Output

    1 2
    4 2 3 1
    1 6 3 2 5 4
    1 6 7 2 3 4 5 8

    分析:
    本题给出的是小朋友之间的敌对关系,有敌对关系的不能挨着。所以建图的时候要初始化g[i][j]为1,如果相邻的话,就改为0,这样就建好图了。因为一共有2n个小朋友,但是每个人最多有n-1个敌人,所以每个小朋友可选择的人数肯定大于n+1,这就说明在建完图后,每个点的度数大于n+1,因此任意两个点的度数之和大于2n,满足哈密顿图的充分条件,所以一定存在哈密顿图。最后直接套上哈密顿图模板就能解出来了。

    需要注意的一点就是m的值可能是为0 的,所有的小朋友彼此之家都没有敌对关系。

    算法讲解
    我们了解欧拉图谈论的实际上是图上关于边的可行遍性的问题,而哈密吨图的要求与点有关,图G的一个回路,若它通过图的每一个节点有且仅有一次,就是哈密顿回路。存在哈密顿回路的图就是哈密顿图。、

    欧拉回路就是从图上的一点出发,经过所有的边必须且只能一次,最终回到原点的路径。

    哈密顿回路就是从一点出发,经过所有的点必须且只能一次,最终回到起点的路径,图中有的边可以不经过,但是不会有边被经过两次。

    满足哈密顿图的两个判定条件:
    一:设图G是具有n个顶点的无向连通图,如果G中任意两个不同顶点的度数之和大于等于n,则G具有哈密顿回路,即G是哈密顿图。这是判断哈密顿图的充分条件,即不满足定理条件时也有可能存在哈密顿回路,图G也可能时哈密顿图,但是满足条件一定是哈密顿图。

    二:如果图G<V,E>时哈密顿图,则对于V的任意一个非空子集,若以|S|表示S中元素的数目,G-S表示删除了S中的点以及这些所关联的边后得到的子图,则W(G-S)<=|S|成立。其中W(G-S)是G-S中联通分支数。这个判定方法上哈密顿图的必要条件,即如果不满足条件一丁不是哈密顿图,但满足条件还不能说这个图是哈密顿图。

    构造哈密顿图的算法过程:

    1.任意找两个相邻的节点S和T,在其基础上扩展出一条尽量长的没有重复节点的路径。也就是说,如果节点S与节点v相邻,而且节点v不在路径S-->T上,则可以把该路径变成v-->S-->T,然后把v变成新的S。从S和T分别向两头扩展,直到无法扩展为止,即所有与S或T相邻的节点都在S-->T上(这里的S和T是不断变化的)。

    2.若S和T相邻,则路径S-->T形成一个回路。

    3.若S和T不相邻,可以构造出一个回路。设路径S-->T上有k+2个节点,依次为S,v1,v2,···,vk和T。可以证明存在节点vi(i属于[1,k)),满足vi与T相邻,且v(i+1)与S相邻。找到上述节点vi,把原路径变成S-->vi-->T-->v(i+1),即形成一个回路。

    4.现在有了一条没有重复节点的路径,如果其长度为n,则哈密顿回路就找到了。如果回路的长度小于n,由于整个图是连通的,所以在还回路上,一定存在一点与回路以外的点相邻。那么就把该回路从这一点处断开,就变成一条路径,同时还可以将与之相连的点加入路径。在按照步骤1的方法尽量扩展路径,则一定有新的节点被加进来。接着回到步骤2。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    using namespace std;
    int n,m;
    int tu[409][409];
    int vis[409];
    int ans[409];
    void init()
    {
        for(int i=0; i<=n; i++)
            for(int j=0; j<=n; j++)
            {
                if(i==j)
                    tu[i][j]=0;
                else
                    tu[i][j]=1;
            }
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
    }
    
    void reverse(int ans[409],int s,int t)//反转函数,s和t分别表示反转开始和结束的界限
    {
        int temp;
        while(s<t)
        {
            temp=ans[s];
            ans[s]=ans[t];
            ans[t]=temp;
            s++;
            t--;
        }
    }
    void Hamilton()//哈密顿图模板
    {
        int s=1,t;//初始化取s为1号点
        int ansi=2;
        int i,j,w,temp;
        for(i=1; i<=n; i++)
        {
            if(tu[s][i]==1)
                break;//只要任意找到一个和起始点相邻的点就行
        }
        t=i;//这个点作为当前的结束点
        vis[s]=vis[t]=1;
        ans[0]=s;
        ans[1]=t;//存储当前的起点和终点
        while(true)
        {
            while(true)//从t点开始向外扩展
            {
                for(i=1; i<=n; i++)
                {
                    if(tu[t][i]==1&&vis[i]==0)//找到任意的一个就可以向外扩
                    {
                        ans[ansi++]=i;
                        vis[i]=1;
                        t=i;//将t的值改变后接着往下找
                        break;
                    }
                }
                if(i>n) break;//相当于从t没法向外扩展了
            }
            //将新的到的序列倒置,s和t互换,这样接着从t开始向外扩展,相当于将原来的s向外扩展
            w=ansi-1;
            i=0;
            reverse(ans,i,w);
            //将s和t互换
            temp=s;
            s=t;
            t=temp;
    
            //从新的t开始向外扩展,相当于原来的s向外扩展,代码与上面的原理一样
            while(true)
            {
                for(i=1; i<=n; i++)
                {
                    if(tu[t][i]==1&&vis[i]==0)
                    {
                        ans[ansi++]=i;
                        vis[i]=1;
                        t=i;
                        break;
                    }
                }
                if(i>n)break;
            }
    
            //如果s和t不相邻的话,进行调整
            if(tu[s][t]==0)
            {
                //取序列中的一点i,值得ans[i]与t相邻,ans[i+1]与s相邻
                for(i=1; i<ansi-2; i++)
                {
                    if(tu[ans[i]][t]==1&&tu[s][ans[i+1]]==1)
                        break;
                }
                //将从ans[i+1]到t的部分倒置
                w=ansi-1;
                i++;
                t=ans[i];
                reverse(ans,i,w);
            }
            //如果s和t相邻
            if(ansi==n) return ;//当前序列中包含n个元素,算法结束
            //当前序列元素个数小于n,寻找ans[i],使得ans[i]与ans外的一点相邻
            for(j=1; j<=n; j++)
            {
                if(vis[j]) continue;
                for(i=1; i<ansi-2; i++)
                {
                    if(tu[ans[i]][j])
                        break;
                }
                if(tu[ans[i]][j])
                    break;
            }
            s=ans[i-1];
            t=j;
            //将ans中的s到ans[i-1]部分的ans倒置
            reverse(ans,0,i-1);
            //将ans中的ans[i+1]到t部分的ans倒置
            reverse(ans,i,ansi-1);
            //将j加入到尾部
            ans[ansi++]=j;
            vis[j]=1;
    
        }
    }
    int main()
    {
        int u,v;
        while(~scanf("%d%d",&n,&m),n|m)
        {
            n=n*2;
            init();
            for(int i=0; i<m; i++)
            {
                scanf("%d%d",&u,&v);
                tu[u][v]=tu[v][u]=0;//建立反图,表示这两个小朋友不能坐在一起,用图理解的话就是这两个点不相邻
            }
            Hamilton();
            printf("%d",ans[0]);
            for(int i=1; i<n; i++)
                printf(" %d",ans[i]);
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    按位与、或、非、异或总结
    Linux 挂载命令
    Linux 文件系统常用命令
    Linux 系统命令sudo权限
    Linux 文件系统属性chattr权限
    Linux 文件特殊权限-Sticky BIT
    Linux 文件特殊权限-SetGID
    Linux 文件特殊权限-SetUID
    Linux 递归acl权限和默认acl权限
    Linux 最大有效权限与删除ACL
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7701047.html
Copyright © 2011-2022 走看看