HDU5727 Necklace (全排列+二分匹配)

题意:

n颗阳宝石,n颗阴宝石组成一个项链,m对x,y,表示阳宝石x和阴宝石y挨在一起,阳宝石会变暗,求最少的变暗个数。

分析:

先枚举所有阴宝石排列情况(环,(n-1)!种),然后插入阳宝石,判断阳宝石插入在之间会不会变暗,不会则加边,然后二分图匹配。






#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<sstream>
#include<limits>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const ll INF = 1e18;
const int maxn = 2e5+10;
const ll MOD = 1000000007;
const double EPS = 1e-10;
const double Pi = acos(-1.0);

const int MAXN = 3000;//点
const int MAXM = 50000; //边
struct Edge
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];
    head[u] = tot++;
}
int linker[MAXN];
bool used[MAXN];
int uN;
bool dfs(int u)
{
    for(int i = head[u]; i != -1;i = edge[i].next)
    {
        int v = edge[i].to;
        if(!used[v])
        {
            used[v] = true;
            if(linker[v] == -1 || dfs(linker[v]))
            {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int res = 0;
    memset(linker,-1,sizeof(linker));
    for(int u = 0;u < uN;u++)      //编号0-uN-1
    {
        memset(used,false,sizeof(used));
        if(dfs(u))res++;
    }
    return res;
}
bool mp[100][100];
int a[10];
int main(){
#ifdef LOCAL
	freopen("C:\\Users\\lanjiaming\\Desktop\\acm\\in.txt","r",stdin);
	//freopen("output.txt","w",stdout);
#endif
//ios_base::sync_with_stdio(0);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(mp,true,sizeof(mp));
        for(int i = 0; i < m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x][y] = false;
        }
        if(n==0) {printf("0\n"); continue;}
        for(int i = 1; i <= n; i++) a[i] = i;
        int ans = n;
        do{
            init();
            uN = n;
            for(int i = 1; i <= n; i++)
            {
                int pre = i-1;
                if (pre == 0) pre = n;
                for(int j = 1; j <= n; j++)
                    if (mp[j][a[i]] && mp[j][a[pre]])
                        addedge(i-1,j-1+n);
            }
             ans = min(ans,n-hungary());
        }while(ans&&next_permutation(a+1,a+n));
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值