好久没写过博客了,把以前的博客补一下。

Necklace

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

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
 
Author
HIT
 
Source

具体怎么写的忘记了

代码:

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <algorithm>
  8. #include <string>
  9. #include <queue>
  10. #include <ctime>
  11. #include <vector>
  12. using namespace std;
  13. typedef long long ll;
  14. const int INF=0x3f3f3f3f;
  15. const int N=;
  16. int vis[N],a[N];
  17. int n,m,ret;
  18. int mp[N][N],g[N][N],match[N],used[N];
  19. bool dfs(int u){
  20. for(int v=;v<=n;++v){
  21. if(!g[u][v]||used[v])continue;
  22. used[v]=true;
  23. if(match[v]==-||dfs(match[v])){
  24. match[v]=u;
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. void solve(){
  31. memset(match,-,sizeof(match));
  32. memset(g,,sizeof(g));
  33. for(int i=;i<=n;++i){
  34. for(int j=;j<=n;++j){
  35. if(mp[a[i]][j]||mp[a[i-]][j])
  36. continue;
  37. g[i][j]=true;//把珠子之间的关系做处理,褪色的两个珠子标记为0,不褪色的珠子为1,利用二分图最大匹配找到最大不消退数。
  38. }
  39. }
  40. for(int i=;i<=n;++i){
  41. if(mp[a[]][i]||mp[a[n]][i])
  42. continue;
  43. g[][i]=true;
  44. }
  45. for(int i=;i<=n;++i){
  46. if(mp[a[]][i]||mp[a[n]][i])
  47. continue;
  48. g[][i]=true;
  49. }
  50. int ans=;
  51. for(int i=;i<=n;++i){
  52. memset(used,,sizeof(used));
  53. if(dfs(i))++ans;
  54. }
  55. ret=min(ret,n-ans);
  56. }
  57. void get(int x){
  58. if(ret==)return;
  59. if(x==n+){
  60. solve();return;
  61. }
  62. for(int i=;i<=n;i++){
  63. if(vis[i])continue;
  64. vis[i]=;
  65. a[x]=i;
  66. get(x+);
  67. vis[i]=;
  68. }
  69. }
  70. int main(){
  71. int v,u,i;
  72. vis[]=;a[]=;
  73. while(~scanf("%d%d",&n,&m)){
  74. if(n==){
  75. printf("0\n");
  76. continue;
  77. }
  78. memset(mp,,sizeof(mp));
  79. for(i=;i<m;i++){
  80. scanf("%d%d",&u,&v);
  81. mp[v][u]=;
  82. }
  83. ret=INF;
  84. get();//n个阴珠子的全排列(注意:环形序列的全排列)
  85. printf("%d\n",ret);
  86. }
  87. return ;
  88. }

HDU 5727.Necklace-二分图匹配匈牙利的更多相关文章

  1. hdu 5727 Necklace 二分图匹配

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

  2. HDU - 2819 Swap (二分图匹配-匈牙利算法)

    题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...

  3. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  5. [ZJOI2009]假期的宿舍 二分图匹配匈牙利

    [ZJOI2009]假期的宿舍 二分图匹配匈牙利 一个人对应一张床,每个人对床可能不止一种选择,可以猜出是二分图匹配. 床只能由本校的学生提供,而需要床的有住校并且本校和外校两种人.最后统计二分图匹配 ...

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

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

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

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

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

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

  9. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

随机推荐

  1. poj 3292 H-素数问题 扩展艾氏筛选法

    题意:形似4n+1的被称作H-素数,两个H-素数相乘得到H-合成数.求h范围内的H-合成数个数 思路: h-素数                                            ...

  2. VS2010Datatable查看器查看超时(Microsoft.VisualStudio.DebuggerVisualizers)

    这个问题由来已久,却一直没有找到原因.大家都知道,VisualStudio的DebuggerVisualizers是一个非常方便的插件,可以帮助我们调试时查看Datatable视图,前阵子突然发现在查 ...

  3. 80C51单片机指令的取指、执行时序

    80C51单片机指令的取指.执行时序 现按4类指令介绍CPU时序.因为CPU工作的过程就是取指令与执行指令的过程,所以CPU必须先取出指令,然后才能执行指令. 1.双字节单周期指令 由于双字节单周期指 ...

  4. 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3150  Solved: 2013[Submit] ...

  5. 66、fastJson 解析json数据时,如果key值不同怎么处理?

    在某些场景,你可能需要定制序列化输出,比如说,希望序列化采用之后采用"ID",而不是"id",你可以使用@JSONField这个Annotation. publ ...

  6. Python-S9-Day122-Python爬虫

    11 初识爬虫 11 初识爬虫 12 今日作业 11.1 初识黄页: 11.2 互联网就是一张大的蜘蛛网: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种 ...

  7. vim中插入递增数

    假设生成0-9的递增数 1.插入数字1,yy复制,9p 2.输入命令 let i= | g//s//\=i/ | let i=i+1 3.结果:

  8. python函数之五马分析

    Python 函数 函数是组织好的,可重复使用的,用来实现单一或相关联功能的代码段. 函数能提高应用的模块性和代码的重复利用率.Python提供了许多内建函数,比如print().也可以自己创建函数, ...

  9. Linux下librtmp使用及编程实战

    最近想做rtmp的推流.直播的小项目,不想直接使用FFmpeg进行推流,FFmpeg进行推流特别简单,因为它已经将编码以及librtmp都集成好了,没啥意思.FFmpeg推流的例子,在雷神的博客里可以 ...

  10. c++面试须知

    这些都是从zhihu上看到的. 指针,多态(虚函数表.内存layout),作用域,内存的管理 算法与数据结构,数据结构上由掌握哈希.优先级队列,算法上有字符串处理,简单的DFS.BFS.动态规划 系统 ...