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就会变暗。 问你最小变暗几个阳。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <cmath>
  7. #include <queue>
  8. #include <vector>
  9. #define MM(a,b) memset(a,b,sizeof(a));
  10. #define inf 0x3f3f3f3f
  11. using namespace std;
  12. typedef long long ll;
  13. #define CT continue
  14. #define SC scanf
  15. const int N=1e5+10;
  16. int f[50][50],a[50],match[50],used[50];
  17. vector<int> G[50];
  18. int n,m,u,v;
  19.  
  20. void add_edge(int u,int v)
  21. {
  22. G[u].push_back(v);
  23. G[v].push_back(u);
  24. }
  25.  
  26. bool dfs(int u)
  27. {
  28. used[u]=1;
  29. for(int i=0;i<G[u].size();i++){
  30. int v=G[u][i];
  31. int w=match[v];
  32. if(w<0||(!used[w]&&dfs(w))){
  33. match[u]=v;
  34. match[v]=u;
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. int bi_match()
  42. {
  43. MM(match,-1);
  44. int res=0;
  45. for(int i=1;i<=n;i++)
  46. if(match[i]<0){
  47. MM(used,0);
  48. if(dfs(i)) res++;
  49. }
  50. return res;
  51. }
  52.  
  53. int main()
  54. {
  55. while(~SC("%d%d",&n,&m))
  56. {
  57. MM(f,0);
  58. for(int i=1;i<=m;i++){
  59. SC("%d%d",&u,&v);
  60. f[u][v]=1;
  61. }
  62. if(n==0||m==0){
  63. printf("0\n");
  64. CT;
  65. }
  66. int ans=0;
  67. for(int i=1;i<=n;i++) a[i]=i;
  68. a[n+1]=a[1];
  69. do{
  70. for(int i=1;i<=2*n;i++) G[i].clear();
  71. for(int u=1;u<=n;u++) {
  72. for(int j=1;j<=n;j++){
  73. int pre=a[j],lat=a[j+1];
  74. if(!f[u][pre]&&!f[u][lat])
  75. add_edge(u,j+n);
  76. }
  77. }
  78. ans=max(ans,bi_match());
  79. }while(next_permutation(a+2,a+n+1));
  80. printf("%d\n",n-ans);
  81. }
  82. return 0;
  83. }

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

易错点:

  1. int v=G[u][i];
  2. int w=match[v];
  3. if(w<0||(!used[w]&&dfs(w))){
  4. match[u]=v;
  5. match[v]=u;
  6. return true;
  7. }
    把这个地方写成了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. ASP.NET跨平台、分布式技术架构技术栈概览 (迄今为止最全的.NET技术栈)

    今天有个学技术的小兄弟问我,现在这么多的技术我要学哪个?我说你根据岗位来学,学好了哪一门都可以在社会上立足,如今已经早已不是我们当年学习IT时候那么单纯了,给他讲了很多,发现现在的技术栈变得层次复杂且 ...

  2. 20190804-Python基础 第二章 列表和元组

    容器,Python支持一种数据结构的基本概念(容器,基本上就是可包含其他对象的对象.) 两种主要的容器是:序列(如列表和元组)和映射(如字典) Ps: 列表与元组区别,列表可修改,元组不能. 对序列的 ...

  3. 【01字典树】hdu-5536 Chip Factory

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5536 [题意] 求一个式子,给出一组数,其中拿出ai,aj,ak三个数,使得Max{ (ai+aj ...

  4. varnishlog、Varnishstat详解

    Varnish将日志记录到共享内存片段,而不是记录到一个普通文件中.当记录到内存片段的最后处,会再从头开始记,覆写老数据.这比记录到文件要快的多,不需要磁盘空间.Varnishlog是一个用来查看Va ...

  5. image的路径写法格式

    if (MapGrid.Visibility == Visibility.Visible) {      this.MapGrid.Visibility = Visibility.Collapsed; ...

  6. Angular 变更检测

    angular 的钩子函数有 content 和 view , Docheck 子控件中有属性变化的时候,父组件的 Docheck  content   view  这3个会依次执行,即使这个属性不在 ...

  7. 梯度直方图(HOG,Histogram of Gradient)

    1.介绍 HOG(Histogram of Oriented Gradient)是2005年CVPR会议上,法国国家计算机科学及自动控制研究所的Dalal等人提出的一种解决人体目标检测的图像描述子,该 ...

  8. springboot 集成 dubbo(一)简介

    一.简介 1,springboot 是 一款快速开发的框架,减少了开发人员对配置文件的操作.采用一些注解来取代xml配置文件. 注解包含预先封装的注解和开发人员自定义注解.同时使用Maven.Grad ...

  9. wrbstrom使用

    使用webstrom时遇到Firefox浏览器打不开问题,是webstrom未找到你Firefox的安装路径下面为大家提供解决方法: 文件--->设置--->工具--->web浏览器 ...

  10. scrapy增量爬取

    ​开始接触爬虫的时候还是初学Python的那会,用的还是request.bs4.pandas,再后面接触scrapy做个一两个爬虫,觉得还是框架好,可惜都没有记录都忘记了,现在做推荐系统需要爬取一定的 ...