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. plsql连接其他服务器的oracle

    plsql除了连接本地的oracle还需要连接其他服务器上的oracle时 1.下载安装oracleClient:2.在oracleClient安装目录下:例如:D:/instantclient_11 ...

  2. git 学习(3)文件删除恢复

    git学习(3) 撤销编辑 如果我们在编辑版本a的时候,如果在没有add之前,发现需要重新编辑版本a怎么办呢,可以通过git reset --hard comm_id, commit_id是版本a的提 ...

  3. Abp Uow 设计

    初始化入口 在AbpKernelModule类中,通过UnitOfWorkRegistrar.Initialize(IocManager) 方法去初始化 /// <summary> /// ...

  4. 具备双向通行能力的架构对于移动APP属于刚性需求。 WebSocket连接 注册信令

    双向通信使用指南_用户指南(开放 API)_API 网关-阿里云 https://help.aliyun.com/document_detail/66031.html 流程描述 (1) 客户端在启动的 ...

  5. 唯品会的Service Mesh三年进化史 2018 年 Service Mesh 元年,被誉为是下一代微服务架构

    2018 年 Service Mesh 元年,被誉为是下一代微服务架构 https://www.sohu.com/a/225324586_465914 唯品会的Service Mesh三年进化史 - ...

  6. Centos locate 文件搜索命令(十一)

    locate命令 locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命令所搜索的后台数据库 updatedb 更新数据库 locate搜索 ...

  7. 003-shell 传递参数

    一.概述 可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 二.实例 以下实例我们向 ...

  8. 工作笔记-javascript-网络层封装

    /** * @Author Mona * @Date 2016-12-08 * @description 网络层封装 */ /** * 封装基本请求方式 */ window.BaseRequest = ...

  9. PEP8编码规范

    1.代码布局设计 1.1 缩进 -4个空格进行缩进 1.2 tab键-在python2中tab和空格是混用的,但是在python中基本上使用tab(pycharm开发工具会自动对代码缩进) 1.3 最 ...

  10. json & pickle数据序列化

    序列化:把内存中的数据对象变成字符串 info = { 'name':'tom', 'age':22 } f = open("test.txt","w") f. ...