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

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

具体怎么写的忘记了

代码:

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <ctime>
#include <vector>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=;
int vis[N],a[N];
int n,m,ret;
int mp[N][N],g[N][N],match[N],used[N];
bool dfs(int u){
for(int v=;v<=n;++v){
if(!g[u][v]||used[v])continue;
used[v]=true;
if(match[v]==-||dfs(match[v])){
match[v]=u;
return true;
}
}
return false;
}
void solve(){
memset(match,-,sizeof(match));
memset(g,,sizeof(g));
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
if(mp[a[i]][j]||mp[a[i-]][j])
continue;
g[i][j]=true;//把珠子之间的关系做处理,褪色的两个珠子标记为0,不褪色的珠子为1,利用二分图最大匹配找到最大不消退数。
}
}
for(int i=;i<=n;++i){
if(mp[a[]][i]||mp[a[n]][i])
continue;
g[][i]=true;
}
for(int i=;i<=n;++i){
if(mp[a[]][i]||mp[a[n]][i])
continue;
g[][i]=true;
}
int ans=;
for(int i=;i<=n;++i){
memset(used,,sizeof(used));
if(dfs(i))++ans;
}
ret=min(ret,n-ans);
}
void get(int x){
if(ret==)return;
if(x==n+){
solve();return;
}
for(int i=;i<=n;i++){
if(vis[i])continue;
vis[i]=;
a[x]=i;
get(x+);
vis[i]=;
}
}
int main(){
int v,u,i;
vis[]=;a[]=;
while(~scanf("%d%d",&n,&m)){
if(n==){
printf("0\n");
continue;
}
memset(mp,,sizeof(mp));
for(i=;i<m;i++){
scanf("%d%d",&u,&v);
mp[v][u]=;
}
ret=INF;
get();//n个阴珠子的全排列(注意:环形序列的全排列)
printf("%d\n",ret);
}
return ;
}

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. 动态规划:HDU2844-Coins(多重背包的二进制优化)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. BFS:HDU2612-Find a way(双向BFS)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. HDU 4005 The war 双连通分量 缩点

    题意: 有一个边带权的无向图,敌人可以任意在图中加一条边,然后你可以选择删除任意一条边使得图不连通,费用为被删除的边的权值. 求敌人在最优的情况下,使图不连通的最小费用. 分析: 首先求出边双连通分量 ...

  4. SetUnhandledExceptionFilter

    SetUnhandledExceptionFilter 设置未捕获异常处理 通常windows程序长时间运行,会发生各种问题,例如访问异常,内存溢出,堆栈破坏等. 这时候通常希望程序自己能增加处理,而 ...

  5. 设计模式之第15章-适配器模式(Java实现)

    设计模式之第15章-适配器模式(Java实现) “呔,来着何人,报上名来.”“这是谁啊,我怎么没见过”,“就是啊,我也没印象.”“我当然是适配器了,要不然还能是谁.”适配器模式碎碎念:我不就是昨天把你 ...

  6. 设计模式之第10章-桥接模式(Java实现)

    设计模式之第10章-桥接模式(Java实现) “一入软件深似海,从此早睡是路人.黑夜给了我黑色的眼睛,我却用他去寻找八阿哥.”“怎么了,又来那么多的感慨啊.”“还能有什么啊,老板是说让换个APP做,这 ...

  7. Ecplise实战常用操作快捷键(更新至2018年10月8日 13:46:40)

    ctrl+鼠标左键    进入/查看这个类或者方法, ctrl + t        快速类型层次结构(出现部分方法) ctrl + o                             快速大 ...

  8. Pipenv 学习笔记

    个人笔记,胡言乱语.并不是什么教学向文章.. 前言 在学习了 Python.Java 后,会发现 Java 有很成熟的项目构建工具,以前是使用 xml 的 Maven,现在又出现了使用 groovy ...

  9. selenium webdriver——控制浏览器

    from selenium import webdriver import time def controlBrowser(): #启动浏览器 driver = webdriver.Firefox() ...

  10. 【转】Twitter-Snowflake,64位自增ID算法详解

    Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统 ...