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(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

3 4
1 1
1 2
1 3
2 1

（阴球1,2,3对阳球1都会产生“失落”的影响，所以没有一条边连向阳球1；而阴球1会使阳球2“失落”，所以也没有连边。）

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int num[]={,,};
do
{
cout<<num[]<<" "<<num[]<<" "<<num[]<<endl;
}while(next_permutation(num,num+));
}

AC代码：

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define MAX 11
#define INF 0x3f3f3f3f
using namespace std;
int n,m,Yin[MAX];
bool unable[MAX][MAX]; struct Edge{
int u,v;
};
vector<Edge> E;
vector<int> G[*MAX];
int matching[*MAX];
int vis[*MAX];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
{
E.push_back((Edge){u,v});
E.push_back((Edge){v,u});
int _size=E.size();
G[u].push_back(E.size()-);
G[v].push_back(E.size()-);
}
bool dfs(int u)
{
for(int i=,_size=G[u].size();i<_size;i++)
{
int v=E[G[u][i]].v;
if (!vis[v])
{
vis[v]=;
if(!matching[v] || dfs(matching[v]))
{
matching[v]=u;
matching[u]=v;
return true;
}
}
}
return false;
}
int hungarian()
{
int ret=;
memset(matching,,sizeof(matching));
for(int i=;i<=n;i++)
{
if(!matching[i])
{
memset(vis,,sizeof(vis));
if(dfs(i)) ret++;
}
}
return ret;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(unable,,sizeof(unable));
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
unable[a][b]=;
} if(n== || m==)
{
printf("0\n");
continue;
} for(int i=;i<=n;i++) Yin[i]=i;
int ans=INF;
do
{
init(,*n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int a,b;
if(i==n) a=Yin[n], b=Yin[];
else a=Yin[i], b=Yin[i+];
if(unable[j][a]||unable[j][b]) continue;
}
}
ans=min(ans,n-hungarian());
}while(next_permutation(Yin+,Yin+n+)); printf("%d\n",ans);
}
}

#include<bits/stdc++.h>
#define MAX 11
#define INF 0x3f3f3f3f
using namespace std;
int n,m,Yin[MAX];
bool unable[MAX][MAX]; /*******************************************
Hopcroft-Karp算法：

*******************************************/
struct Hopcroft_Karp{
int edge[MAX][MAX],Mx[MAX],My[MAX],Nx,Ny;
int dx[MAX],dy[MAX],dis;
bool vis[MAX];
void init(int uN,int vN)
{
Nx=uN, Ny=vN;
for(int i=;i<=uN;i++) for(int j=;j<=vN;j++) edge[i][j]=;
}
bool searchP()
{
queue<int> Q;
dis=INF;
memset(dx,-,sizeof(dx));
memset(dy,-,sizeof(dy));
for(int i=;i<=Nx;i++)
{
if(Mx[i]==-)
{
Q.push(i);
dx[i]=;
}
}
while(!Q.empty())
{
int u=Q.front();Q.pop();
if(dx[u]>dis) break;
for(int v=;v<=Ny;v++)
{
if(edge[u][v] && dy[v]==-)
{
dy[v]=dx[u]+;
if(My[v]==-) dis=dy[v];
else
{
dx[My[v]]=dy[v]+;
Q.push(My[v]);
}
}
}
}
return dis!=INF;
}
bool dfs(int u)
{
for(int v=;v<=Ny;v++)
{
if(!vis[v] && edge[u][v] && dy[v]==dx[u]+)
{
vis[v]=;
if(My[v]!=- && dy[v]==dis) continue;
if(My[v]==- || dfs(My[v]))
{
My[v]=u;
Mx[u]=v;
return true;
}
}
}
return false;
}
int max_match()
{
int ret=;
memset(Mx,-,sizeof(Mx));
memset(My,-,sizeof(My));
while(searchP())
{
memset(vis,,sizeof(vis));
for(int i=;i<=Nx;i++) if(Mx[i]==-) ret+=dfs(i);
}
return ret;
}
}HK; int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(unable,,sizeof(unable));
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
unable[a][b]=;
} if(n== || m==)
{
printf("0\n");
continue;
} for(int i=;i<=n;i++) Yin[i]=i;
int ans=INF;
do
{
HK.init(n,n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int a,b;
if(i==n) a=Yin[n], b=Yin[];
else a=Yin[i], b=Yin[i+];
if(unable[j][a]||unable[j][b]) continue;
}
}
ans=min(ans,n-HK.max_match());
}while(next_permutation(Yin+,Yin+n+)); printf("%d\n",ans);
}
}

## HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]的更多相关文章

1. hdu 5727 Necklace dfs+二分图匹配

Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

2. HDU 5727 Necklace（二分图匹配）

[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...

3. hdu2389二分图之Hopcroft Karp算法

You're giving a party in the garden of your villa by the sea. The party is a huge success, and every ...

4. HDU5090--Game with Pearls 二分图匹配 （匈牙利算法）

题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

5. hdu 5727 Necklace 二分图匹配

题目链接 给2*n个珠子, n<=9, n个阴n个阳. 然后将它们弄成一个环, 阴阳交替.现在给你m个关系, 每个关系给出a, b. 如果阳a和阴b挨着, 那么a就会变暗. 问你最小变暗几个阳. ...

6. HDU 5727 Necklace ( 2016多校、二分图匹配 )

题目链接 题意 : 给出 2*N 颗珠子.有 N 颗是阴的.有 N 颗是阳的.现在要把阴阳珠子串成一个环状的项链.而且要求珠子的放置方式必须的阴阳相间的.然后给出你 M 个限制关系.格式为 ( A.B ...

7. HDU 1083 网络流之二分图匹配

http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

8. HDU 5727 - Necklace

题意:( 0 <= n <= 9 )    现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,    已知有些阴珠子和阳珠子不能放在相邻的位置,否则这颗阳珠子就会失去功效,   ...

9. HDU 2819 Swap（二分图匹配）

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...

## 随机推荐

1. 论文阅读（Xiang Bai——【CVPR2016】Multi-Oriented Text Detection with Fully Convolutional Networks）

Xiang Bai--[CVPR2016]Multi-Oriented Text Detection with Fully Convolutional Networks 目录 作者和相关链接 方法概括 ...

ReadingNotes@02-12-2013 */--> pre { background-color: #2f4f4f;line-height: 1.6; FONT: 10.5pt Cons ...

3. mysql 发生系统错误 1067

最近要搞一个免安装版的mysql,原来的配置在d盘的my.ini如下 [client]port=3306default-character-set=utf8 [mysqld]port=3306char ...

4. 笔记-windbg及时调试

当程序在测试或者老化的时候很有用,只要程序有异常抛出,就能启用windbg调试,这样就能及时的保存现场. 程序崩溃时,windows系统会调用系统默认调试器,其设置在注册表 HKEY_LOCAL_MA ...

5. weka实际操作--构建分类、回归模型

weka提供了几种处理数据的方式,其中分类和回归是平时用到最多的,也是非常容易理解的,分类就是在已有的数据基础上学习出一个分类函数或者构造出一个分类模型.这个函数或模型能够把数据集中地映射到某个给定的 ...

6. Streaming Principal Component Analysis in Noisy Settings

论文背景: 面对来袭的数据,连续样本不一定是不相关的,甚至不是同分布的. 当前,大部分在线PCA都只关注准确性,而忽视时效性! 噪声?数据缺失,观测有偏,重大异常? 论文内容: Section 2 O ...

7. Project 03- STM32F4xx PID controller

Project 03- STM32F4xx PID controller CMSIS files from ARM provides ARM Math functions. There are als ...

8. bzoj 3674 可持久化并查集加强版——可持久化并查集

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3674 用主席树维护 fa[ ]  和 siz[ ] .改 fa[ ] 和改 siz[ ] 都 ...

9. html页面设置一个跟随鼠标移动的DIV（jQuery实现）

说明业务:鼠标放到某个标签上,显示一个div,并跟随鼠标移动 html页面(直接放body里面): <a href="#" id="'+data[i].refund ...

10. sql server取某个时间段内所有日期或者所有月份

取所有月份: declare @begin datetime,@end datetime set @begin='2015-2-6' set @end='2015-12-2' declare @mon ...