Necklace

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

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 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
给2*n个珠子, n<=9, n个阴n个阳。 然后将它们弄成一个环, 阴阳交替。现在给你m个关系, 每个关系给出a, b。 如果阳a和阴b挨着, 那么a就会变暗。 问你最小变暗几个阳。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int f[50][50],a[50],match[50],used[50];
vector<int> G[50];
int n,m,u,v; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
int w=match[v];
if(w<0||(!used[w]&&dfs(w))){
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bi_match()
{
MM(match,-1);
int res=0;
for(int i=1;i<=n;i++)
if(match[i]<0){
MM(used,0);
if(dfs(i)) res++;
}
return res;
} int main()
{
while(~SC("%d%d",&n,&m))
{
MM(f,0);
for(int i=1;i<=m;i++){
SC("%d%d",&u,&v);
f[u][v]=1;
}
if(n==0||m==0){
printf("0\n");
CT;
}
int ans=0;
for(int i=1;i<=n;i++) a[i]=i;
a[n+1]=a[1];
do{
for(int i=1;i<=2*n;i++) G[i].clear();
for(int u=1;u<=n;u++) {
for(int j=1;j<=n;j++){
int pre=a[j],lat=a[j+1];
if(!f[u][pre]&&!f[u][lat])
add_edge(u,j+n);
}
}
ans=max(ans,bi_match());
}while(next_permutation(a+2,a+n+1));
printf("%d\n",n-ans);
}
return 0;
}

  分析:全排列一下阴珠子形成一个环,然后对于形成的每个位置,如果该位置可以放下当前枚举的阳珠子,就连接一条边,那么能不变质的最大阳珠子个数,就是二分图的足最大匹配数。

易错点:

int v=G[u][i];
int w=match[v];
if(w<0||(!used[w]&&dfs(w))){
match[u]=v;
match[v]=u;
return true;
}
把这个地方写成了match[v]<0||!used[v]&&dfs(v);悲剧的wa了好几发,其实应该是w=match[v]来匹配的

最新文章

  1. AngularJs 动态加载模块和依赖
  2. 2016-2017-2 《Java程序设计》教学进程
  3. c#反射-动态加载dll简单例子
  4. 【c#】对象转json字符串/字符串转Json对象
  5. 【AS3】Flash与后台数据交换四种方法整理
  6. jquery on()方法绑定多个选择器,多个事件
  7. dom div重合提示
  8. 洛谷 P3374 【模板】树状数组 1
  9. mysql 查询
  10. MES系统的有用存储过程
  11. (java)从零开始之--异常处理(以文件拷贝为例)
  12. AJAX远程跨域获取数据
  13. Nuget 学习三
  14. Storm常见模式——批处理
  15. 微信小程序页面跳转的问题(app.json中设置tarBar后wx.redirectTo和wx.navigateTo均不能实现跳转到指定的页面)
  16. C++中const的实现机制深入分析
  17. 有趣的 box-decoration-break
  18. 网络流24(san)题题解汇总
  19. WebService,ESB笔记
  20. Emmet Cheat Sheet(Sublime编辑)

热门文章

  1. Zookeeper快速开始
  2. window 杀固定端口的进程
  3. log4j application.properties 配置文件
  4. 怎样在页面关闭时发起HTTP请求
  5. 出现 HTTP 错误 500.19 错误代码 0x800700b7
  6. Docker本地镜像发布到阿里云和从阿里云拉取镜像
  7. JavaScript获取数组索引
  8. LINUX 使用grep命令查看某个指定时间段的日志
  9. JPA的入门案例
  10. 虚拟机不能桥接联网 vmnet0上的网桥当前未运行