hdu 5727 - Necklace ( 环排列 + 二分图匹配)

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(0N9),M(0MNN) , 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  Y with 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
 


题意:

有2n(0<=n<=9)个珠子,分成阴阳两极,每极各n个

用这2n个珠子做成一个项链,使得相邻两个珠子的极性是不一样的,因为有一些阳性的珠子会被一些阴性的珠子所削弱在它们它们相邻的情况下。

给你m(0<=m<=n*(n-1)/2)个关系[x,y]表示阳性珠子x会被阴性珠子y在相邻情况下所削弱。问你最少有多少个阳性被削弱。


题解:

我们可以先暴力枚举出所有阴性珠子的排列情况(因为是环,所以有(n-1)!种),然后在它们之间插入阳性的珠子,判断出阳性珠子插入在之间会不会被削弱。我们通过匈牙利算法算出最大匹配sum,然后算出n-sum,对于每种排列取最小值就得到了我们想要的答案,注意特判下n=0的情况。


代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));


int T;
int n,m;

int pos[12];
int vis[12];
int mp[12][12];
int ans=0;


int g[12][12];
int linker[12];
bool used[12];

bool dfs2(int u)
{
    for(int v=1;v<=n;v++)
        if(g[u][v] && !used[v])
    {
        used[v] = true;
        if(linker[v] == -1 || dfs2(linker[v]))
        {
            linker[v]=u;
            return  true;
        }
    }
    return false;
}

int hungary(int n)
{
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=1;u<=n;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs2(u)) res++;
    }
    return res;
}



void solve()
{
    memset(g,1,sizeof(g));

    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if( mp[ pos[i] ][j] || mp[ pos[i-1] ][j])
                g[i][j]=0;
        }
    }

    for(int j=1;j<=n;j++)
    {
        if(mp[pos[1]][j] || mp[pos[n]][j]) g[1][j]=0;
    }

    int re=hungary(n);
    ans= min(ans,n-re);
}


void dfs(int ps)
{
    if(ps>=n+1)
    {
        solve();
        return;
    }
    for(int i=2;i<=n;i++)
    {
        if(vis[i]) continue;

        vis[i]=1;

        pos[ps]=i;
        dfs(ps+1);

        vis[i]=0;
    }
}

int main()
{
    int cas=1;
    int a,b;

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

        ans=n;

        memset(vis,0,sizeof(vis));
        pos[1]=1;vis[1]=1;

        dfs(2);

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

    }
    return 0
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值