Necklace

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

题意:

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

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

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

题解:对阴珠全排列,插入阳珠这样可以枚举出两个珠子的位置,利用给的关系找出每种情况下珠子的互相作用关系,再利用二分图的最大匹配找到一个多大不消退。

#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
const int N=;
int vis[N],a[N];
int n,m,ret;
int mp[N][N],g[N][N],match[N],used[N];
bool dfs(int u){
for(int v=;v<=n;++v){
if(!g[u][v]||used[v]) continue;
used[v]=true;
if(match[v]==-||dfs(match[v])){
match[v]=u;
return true;
}
}
return false;
}
void solve(){
memset(match,-,sizeof(match));
memset(g,,sizeof(g));
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
if(mp[a[i]][j]||mp[a[i-]][j])
continue;
g[i][j]=true;  //把珠子之间的关系做处理,褪色的两个珠子标记为0,不褪色的珠子为1,利用二分图最大匹配找到最大不消退数。
}
}
for(int i=;i<=n;++i){
if(mp[a[]][i]||mp[a[n]][i])
continue;
g[][i]=true;
}
int ans=;
for(int i=;i<=n;++i){
memset(used,,sizeof(used));
if(dfs(i))++ans;
}
ret=min(ret,n-ans);
}
void get(int x)
{
if(ret==)return ;
if(x==n+) {
solve();return ;
}
for(int i=;i<=n;i++)
{
if(vis[i])
continue;
vis[i]=;
a[x]=i;
get(x+);
vis[i]=;
}
}
int main()
{
int v,u,i;
vis[]=,a[]=;
while(~scanf("%d%d",&n,&m))
{
if(n==)
{
printf("0\n");
continue;
}
memset(mp,,sizeof(mp));
for(i=;i<m;i++)
{
scanf("%d%d",&u,&v);
mp[v][u]=;
}
ret=INF;
get();// n个阴珠子的全排列(注意:环形序列的全排列)
printf("%d\n",ret);
}
return ;
}

题解:

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

hdu5727的更多相关文章

  1. HDU5727 Necklace(枚举 + 二分图最大匹配)

    题目大概说有n个yang珠子n个yin珠子,要交替串成一个环形项链,有些yang珠子和某个yin珠子相邻这个yang珠子会不高兴,问最少有几个yang珠子不高兴. 自然会想到直接用状压DP去解,转移很 ...

  2. [HDU5727]Necklace(二分图最大匹配,枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:有N个阴珠子和N个阳珠子,特定序号的阴阳珠子放在一起会让阳珠子暗淡.现在问排放成一个环,如 ...

  3. HDU5727 Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:n个珠子,每个珠子有阴阳两种属性,且阴的一定和阳的紧邻,排成一个环:m行,每行两个数,表示阳性x珠子 ...

  4. HDU5727 Necklace(二分图匹配)

    Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang ...

随机推荐

  1. 《Apologize》歌曲

    OneRepublic,中译名“共和时代”,是美国的一个流行摇滚乐队,曲风走流行.独立摇滚的路线.2006年天使专辑<Dreaming Out Loud>诞生,主打<Apologiz ...

  2. Network Security Services If you want to add support for SSL, S/MIME, or other Internet security standards to your application, you can use Network Security Services (NSS) to implement all your securi

    Network Security Services | MDN https://developer.mozilla.org/zh-CN/docs/NSS 网络安全服务 (NSS) 是一组旨在支持支持安 ...

  3. CSS cursor 属性

    cursor 1.定义和用法 cursor 属性规定要显示的光标的类型(形状). 该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状(不过 CSS2.1 没有定义由哪个边界确定这个范围). 2 ...

  4. Storm-源码分析-Topology Submit-Supervisor

    mk-supervisor (defserverfn mk-supervisor [conf shared-context ^ISupervisor isupervisor] (log-message ...

  5. ubuntu14下创建软件的快捷启动方式

    下载软件,使用softname/bin/softname.sh即可启动,但是很麻烦,每次都要打开terminal 为了方便,我们需要创建desktop文件指向这个启动软件的shell文件(以创建Pyc ...

  6. Ubuntu安装Python3 和卸载

    Python2中文的解决 在py文件第一行添加 #coding=utf-8 1 规范的应该这么写 #-*- coding:utf-8 -*- 1 安装python 系统默认安装Python2 安装Py ...

  7. zabbix使用之常用功能使用心得

    ZABBIX 使用 Written by: Jane.Hoo 1.zabbix监控概念介绍 项目(iterm)定义收集被监控的数据项,如收集被监控机内存使用情况 应用集(application)一些项 ...

  8. Zabbix3的离线安装

    背景与环境 由于实际情况需求,zabbix在局域网中进行部署,遇到许多问题,在此记录. 操作系统:CentOS 6.9(使用的最小安装) zabbix版本:zabbix-3.0.13(LTS) php ...

  9. 产品固件(系统)升级——curl/wget

    1.文件下载指令应用 支持断点续传 curl -C - -O "https://curl.haxx.se/download/archeology/curl-7.58.0.tar.gz&quo ...

  10. Spring框架第六篇之Spring与DAO

    一.Spring与JDBC模板 1.搭建环境 首先导入需要的jar包: 以上jar中多导入了DBCP和C3P0的jar包,因为这里需要演示怎么配置多种数据源,所以导入了这两个包,在实际开发中无需导入这 ...