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

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. Codeforces Round #462 (Div. 2) A Compatible Pair

    A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  2. Curl之解决中文乱码

    利用iconv命令 curl http://www.baidu.com | iconv -f gb2312 -t utf-8 iconv命令可以将一种已知的字符集文件转换成另一种已知的字符集文件.它的 ...

  3. Falsk

    flask: 1.配置文件的几种方式: 1.app.config['DEBUG'] =True 2.app.config.from_pyfile("setting.py") 3.a ...

  4. IOS开发---菜鸟学习之路--(十三)-利用MBProgressHUD进行异步获取数据

    本章将介绍如何利用MBProgressHUD实现异步处理数据. 其实我本来只是像实现一个加载数据时提示框的效果,然后问了学长知道了这个类,然后就使用了 接着就发现了一个“BUG” 再然后就发现原来MB ...

  5. 深入学习之mysql(四)聚合函数

    聚合函数:COUNT统计记录的条数.SUM求和函数.AVG求平均值.MAX求最大值.MIN求最小值   一.COUNT练习: 1.统计学校一共有多少个学生: mysql> SELECT COUN ...

  6. js验证密码强弱

    JS密码强度验证 <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  7. jquery版右下角弹窗效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. GDOI2018 爆零记,Challenge Impossibility

    蒟蒻的GDOI又双叒叕考挂啦...... Day 0 && Day -1 学校月考,貌似考的还不错? 然而考完试再坐船去中山实在是慢啊......晚上10点才到酒店 wifi差评... ...

  9. [URAL1519] Formula 1 [插头dp入门]

    题面: 传送门 思路: 插头dp基础教程 先理解一下题意:实际上就是要你求这个棋盘中的哈密顿回路个数,障碍不能走 看到这个数据范围,还有回路处理,就想到使用插头dp来做了 观察一下发现,这道题因为都是 ...

  10. [ZJOI2012][bzoj 2816] 网络 network [LCT]

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2816 思路: 第一个条件看完暂时还没什么想法 看完第二个,发现每一个颜色都是一个森林 进而想 ...