HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

Posted tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]相关的知识,希望对你有一定的参考价值。

Problem Description
SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid making the necklace too Yin or too Yang, he must place these magic gems Yin after Yang and Yang after Yin, which means two adjacent gems must have different kind of energy. But he finds that some gems with Yang energy will become somber adjacent with some of the Yin gems and impact the value of the neckless. After trying multiple times, he finds out M rules of the gems. He wants to have a most valuable neckless which means the somber gems must be as less as possible. So he wonders how many gems with Yang energy will become somber if he make the necklace in the best way.

Input
Multiple test cases.
For each test case, the first line contains two integers N(0≤N≤9),M(0≤M≤N∗N), descripted as above.
Then M lines followed, every line contains two integers X,Y, indicates that magic gem X with Yang energy will become somber adjacent with the magic gem Ywith Yin energy.

Output
One line per case, an integer indicates that how many gem will become somber at least.

Sample Input
2 1
1 1
3 4
1 1
1 2
1 3
2 1

Sample Output
1 1

题意:
现有2*N个宝珠,N个阴球,N个阳球;给出M个关系,代表当Xi阴球和Yi阳球连接在一起时,Yi阳球会变得很“失落”;
要求求出最小多少个阳球会变成“失落”的。
 
题解:
假如我们现在有如下的匹配:,就不难得到对应的项链穿法:(如果规定了阴球的排列顺序为1->2->3的话,那么项链的穿法就固定了)
 
那么,如果我们按照题目所给的样例2:
3 4
1 1
1 2
1 3
2 1
做出如下的图:
(阴球1,2,3对阳球1都会产生“失落”的影响,所以没有一条边连向阳球1;而阴球1会使阳球2“失落”,所以也没有连边。)
我们求出最大匹配,就能得到一个相应的项链穿法:
          
显然,由于一些边的缺少,导致了阳球的失配,所以此时 3 - 最大匹配数 = “失落的”球数
 
当然,这是在固定的阴球顺序下得到的,我们要求得所有可能性中的最小,就需要对阴球进行全排列;
不妨使用STL中的next_permutation函数对阴球进行全排列,下面是next_permutation函数的用法实例:
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int num[3]={1,2,3};
    do
    {
        cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
    }while(next_permutation(num,num+3));
}
View Code

这样,整一道题目的解法就已经相当完整了;

还有一个需要注意的小细节,那就是:

因为项链成环,所以 1 -> 2 -> 3( -> 1 ) 和 2 -> 3 -> 1( -> 2 )其实是相同的项链,不需要重复计算,故我们需要定项链的一个“头”;

对应到全排列时,即保证任意的阴球顺序,都是1号阴球排在第一位,这样就不会产生重复计算了;

 

AC代码:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<vector>
  4 #include<algorithm>
  5 #define MAX 11
  6 #define INF 0x3f3f3f3f
  7 using namespace std;
  8 int n,m,Yin[MAX];
  9 bool unable[MAX][MAX];
 10 
 11 struct Edge{
 12     int u,v;
 13 };
 14 vector<Edge> E;
 15 vector<int> G[2*MAX];
 16 int matching[2*MAX];
 17 int vis[2*MAX];
 18 void init(int l,int r)
 19 {
 20     E.clear();
 21     for(int i=l;i<=r;i++) G[i].clear();
 22 }
 23 void add_edge(int u,int v)
 24 {
 25     E.push_back((Edge){u,v});
 26     E.push_back((Edge){v,u});
 27     int _size=E.size();
 28     G[u].push_back(E.size()-2);
 29     G[v].push_back(E.size()-1);
 30 }
 31 bool dfs(int u)
 32 {
 33     for(int i=0,_size=G[u].size();i<_size;i++)
 34     {
 35         int v=E[G[u][i]].v;
 36         if (!vis[v])
 37         {
 38             vis[v]=1;
 39             if(!matching[v] || dfs(matching[v]))
 40             {
 41                 matching[v]=u;
 42                 matching[u]=v;
 43                 return true;
 44             }
 45         }
 46     }
 47     return false;
 48 }
 49 int hungarian()
 50 {
 51     int ret=0;
 52     memset(matching,0,sizeof(matching));
 53     for(int i=1;i<=n;i++)
 54     {
 55         if(!matching[i])
 56         {
 57             memset(vis,0,sizeof(vis));
 58             if(dfs(i)) ret++;
 59         }
 60     }
 61     return ret;
 62 }
 63 
 64 int main()
 65 {
 66     while(scanf("%d%d",&n,&m)!=EOF)
 67     {
 68         memset(unable,0,sizeof(unable));
 69         for(int i=1,a,b;i<=m;i++)
 70         {
 71             scanf("%d%d",&a,&b);
 72             unable[a][b]=1;
 73         }
 74         
 75         if(n==0 || m==0)
 76         {
 77             printf("0\\n");
 78             continue;
 79         }
 80         
 81         for(int i=1;i<=n;i++) Yin[i]=i;
 82         int ans=INF;
 83         do
 84         {
 85             init(1,2*n);
 86             for(int i=1;i<=n;i++)
 87             {
 88                 for(int j=1;j<=n;j++)
 89                 {
 90                     int a,b;
 91                     if(i==n) a=Yin[n], b=Yin[1];
 92                     else a=Yin[i], b=Yin[i+1];
 93                     if(unable[j][a]||unable[j][b]) continue;
 94                     add_edge(i,n+j);
 95                 }
 96             }
 97             ans=min(ans,n-hungarian());
 98         }while(next_permutation(Yin+2,Yin+n+1));
 99 
100         printf("%d\\n",ans);
101     }
102 }

 

然后是使用了HK算法来求二分图最大匹配的AC代码:

#include<bits/stdc++.h>
#define MAX 11
#define INF 0x3f3f3f3f
using namespace std;
int n,m,Yin[MAX];
bool unable[MAX][MAX];

/*******************************************
Hopcroft-Karp算法:
初始化:edge[][]邻接矩阵,Nx和Ny为左右点个数
左右点编号为1~N,间复杂度为 O( (V^0.5) * E )
*******************************************/
struct Hopcroft_Karp{
    int edge[MAX][MAX],Mx[MAX],My[MAX],Nx,Ny;
    int dx[MAX],dy[MAX],dis;
    bool vis[MAX];
    void init(int uN,int vN)
    {
        Nx=uN, Ny=vN;
        for(int i=1;i<=uN;i++) for(int j=1;j<=vN;j++) edge[i][j]=0;
    }
    void addedge(int u,int v){edge[u][v]=1;}
    bool searchP()
    {
        queue<int> Q;
        dis=INF;
        memset(dx,-1,sizeof(dx));
        memset(dy,-1,sizeof(dy));
        for(int i=1;i<=Nx;i++)
        {
            if(Mx[i]==-1)
            {
                Q.push(i);
                dx[i]=0;
            }
        }
        while(!Q.empty())
        {
            int u=Q.front();Q.pop();
            if(dx[u]>dis) break;
            for(int v=1;v<=Ny;v++)
            {
                if(edge[u][v] && dy[v]==-1)
                {
                    dy[v]=dx[u]+1;
                    if(My[v]==-1) dis=dy[v];
                    else
                    {
                        dx[My[v]]=dy[v]+1;
                        Q.push(My[v]);
                    }
                }
            }
        }
        return dis!=INF;
    }
    bool dfs(int u)
    {
        for(int v=1;v<=Ny;v++)
        {
            if(!vis[v] && edge[u][v] && dy[v]==dx[u]+1)
            {
                vis[v]=1;
                if(My[v]!=-1 && dy[v]==dis) continue;
                if(My[v]==-1 || dfs(My[v]))
                {
                    My[v]=u;
                    Mx[u]=v;
                    return true;
                }
            }
        }
        return false;
    }
    int max_match()
    {
        int ret=0;
        memset(Mx,-1,sizeof(Mx));
        memset(My,-1,sizeof(My));
        while(searchP())
        {
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=Nx;i++) if(Mx[i]==-1) ret+=dfs(i);
        }
        return ret;
    }
}HK;

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(unable,0,sizeof(unable));
        for(int i=1,a,b;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            unable[a][b]=1;
        }

        if(n==0 || m==0)
        {
            printf("0\\n");
            continue;
        }

        for(int i=1;i<=n;i++) Yin[i]=i;
        int ans=INF;
        do
        {
            HK.init(n,n);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    int a,b;
                    if(i==n) a=Yin[n], b=Yin[1];
                    else a=Yin[i], b=Yin[i+1];
                    if(unable[j][a]||unable[j][b]) continue;
                    HK.addedge(i,j);
                }
            }
            ans=min(ans,n-HK.max_match());
        }while(next_permutation(Yin+2,Yin+n+1));

        printf("%d\\n",ans);
    }
}

 

以上是关于HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5727 Necklace(全排列+二分图匹配)

HDU 5727 Necklace ( 2016多校二分图匹配 )

hdu 527 Necklace

HDU 3091 Necklace

HDU 1817Necklace of Beads(置换+Polya计数)

HDU5730 Shell Necklace