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]来匹配的

TTTTTTTTTTTTTTTT hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列的更多相关文章

  1. HDU 5727 Necklace 环排+二分图匹配

    这是从山东大学巨巨那里学来的做法 枚举下黑色球的排列总数是8!,然后八个白球可选的位置与左右两个黑球存不存在关系建图就行 这是原话,具体一点,每次生成环排,只有互不影响的才连边 最后:注重一点,n个数 ...

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

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

  3. hdu 5727 Necklace 二分图匹配

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

  4. HDU 5727 Necklace(二分图匹配)

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

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

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

  6. HDU 5727 - Necklace

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

  7. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  8. HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...

  9. HDU 5727 Necklace(全排列+二分图匹配)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 题意:现在有n个阳珠子和n个阴珠子,现在要把它们串成项链,要求是阴阳珠子间隔串,但是有些阴阳珠 ...

随机推荐

  1. selenium登录百度

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...

  2. layui upload 在JS动态加载内容后, 点击按钮无反应

    /** * 根据用户选择的不同规格选项 * 返回 不同的输入框选项 */ function ajaxGetSpecInput2(spec_arr) { var goods_id = $('#goods ...

  3. MySQL SELECT语法(二)SELECT...INTO语法

    源自MySQL 5.7 官方手册 SELECT...INTO Syntax 一.SELECT...INTO介绍 SELECT...INTO用来将查询结果存储在变量或者写入文件中. SELECT ... ...

  4. 作业1:java虚拟机内存模型图示

    看了很多篇文章,整理成一幅图,但仍然有许多不解的地方,以后再接着完善,哪位大神看到不正确的地方,请指出,谢谢.

  5. StoneTab标签页CAD插件 3.2.2

    //////////////////////////////////////////////////////////////////////////////////////////////////// ...

  6. mybatis基础小结

    1.JDBC是怎么访问数据库的?答:JDBC编程有6步,分别是1.加载sql驱动,2.使用DriverManager获取数据库连接,3.使用Connecttion来创建一个Statement对象 St ...

  7. SpringCloud 随笔

    目录 服务间通讯 统一配置中心 RabbitMQ Spring Cloud Stream 服务网关 Spring Cloud Zuul ++==(纯手打,代码可能有错!)==++ 服务间通讯 Rest ...

  8. vue打包后找不到资源路径问题

    问题描述: 使用webpack打包vue项目后,前后端联调无法找到资源 解决方案: 一. 改为相对路径,去除axios中地址的第一个“/” onProxyReq: function (proxyReq ...

  9. 【python】多进程、多线程、序列

    一.多进程 1.子进程永远返回0,而父进程返回子进程的ID.这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程 ...

  10. docker container 导入和导出

    目录 docker container 导入和导出 1.前言 2.docker container 的导出 3.docker container 的导入 4.镜像和容器 导出和导入的区别 docker ...