HDU 5727

Necklace

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2555    Accepted Submission(s): 806


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
 

Author
HIT
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5932  5931  5930  5928  5925 

 



题意,给n个阴阳柱子组成阴阳相间的环,给出m个匹配。m对阴阳柱子在一起会变色。问你最少有多少阳珠子会变色。

枚举阴柱子的全排列。因为是环 所以枚举到(n-1)!就行了。最后跑二分匹配判断最多多少能匹配,变色的不建成二分图。


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int N=10;
int n,m,u,v,ans,pos[N],link[N];
bool s[N][N],vis[N];
vector<int>eg[N];
bool dfs(int u)
{
    for(int i=0; i<eg[u].size(); i++)
    {
        int v=eg[u][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void solve()
{
    memset(s,0,sizeof(s));
    while(m--)
    {
        scanf("%d%d",&u,&v);
        s[u][v]=1;
    }
    ans=inf;
    for(int i=1; i<=n; i++)
        pos[i]=i;
    do
    {
        memset(link,-1,sizeof(link));
        for(int i=1; i<=n; i++)
        {
            eg[i].clear();
            for(int j=1; j<=n; j++)
            {
                u=pos[i];
                if(i==1) v=pos[n];
                else v=pos[i-1];
                if(s[j][u]||s[j][v]) continue;
                eg[i].push_back(j);
            }
        }
        int now=0;
        for(int i=1; i<=n; i++)
        {
            memset(vis,0,sizeof(vis));
            now+=dfs(i);
        }
        ans=min(ans,n-now);
    }
    while(next_permutation(pos+2,pos+n+1)&&ans);  //一个柱子固定剩下(n-1)!的排列,因为是环!!
    printf("%d\n",ans);
}
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        if(!n)
        {
            puts("0");
            continue;
        }
        solve();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值