hdu 5727 Necklace dfs+二分图匹配
Necklace/center>
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5727
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.Given four points ABCD, if ABCD is a tetrahedron, calculate the inscribed sphere of ABCD.
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
Hint
题意
你有2n个珠子,每种珠子都有阴阳两种,你需要弄成一个环形的且阴阳交替的链。
然后给你M条边a,b。表示如果a阳球靠近了b阴球,那么阳球A就会暗淡。
让你输出构造一条链,使得暗淡的阳珠子最少。
题解:
dfs枚举阴珠子的排列,然后再二分图匹配跑阳珠子。
如果i位置能放j阳球
那么建一条j->i,流量为1的边
然后跑就好了。
但是我写的太渣了,会TLE,就随手加了个只搜10000次的剪枝。
然后就过了,蛤蛤蛤。
代码
#include<bits/stdc++.h>
using namespace std;
int vis[12];
int Vis[12];
int mp[12][12];
int n,m;
int ans;
int kkk[12];
vector<int> q[12];
int match[12];
int Dfs(int x){
for(int i=0;i<q[x].size();i++)
{
if(Vis[q[x][i]]==0)
{
Vis[q[x][i]]=1;
if(match[q[x][i]]==-1||Dfs(match[q[x][i]]))
{
match[q[x][i]]=x;
return 1;
}
}
}
return 0;
}
int QAQ(){
for(int i=0;i<n;i++)q[i].clear(),match[i]=-1;
for(int i=0;i<n;i++){
for(int j=1;j<=n;j++){
if(!mp[j][kkk[i]]&&!mp[j][kkk[(i-1+n)%n]]){
q[i].push_back(j-1);
}
}
}
int tmp = 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)Vis[j]=0;
if(Dfs(i))tmp++;
}
return n-tmp;
}
int cnt = 0;
void dfs(int x){
cnt++;
if(cnt>100000)return;
if(x==n){
ans=min(ans,QAQ());
return;
}
for(int i=1;i<=n;i++){
if(!vis[i]){
vis[i]=1;
kkk[x]=i;
dfs(x+1);
kkk[x]=0;
vis[i]=0;
}
}
}
void solve(){
cnt=0;
ans=100;
memset(mp,0,sizeof(mp));
for(int i=1;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
mp[a][b]=1;
}
dfs(0);
cout<<ans<<endl;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
solve();
}
return 0;
}
hdu 5727 Necklace dfs+二分图匹配的更多相关文章
- HDU 5727 Necklace(二分图匹配)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...
- HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...
- hdu 5727 Necklace 二分图匹配
题目链接 给2*n个珠子, n<=9, n个阴n个阳. 然后将它们弄成一个环, 阴阳交替.现在给你m个关系, 每个关系给出a, b. 如果阳a和阴b挨着, 那么a就会变暗. 问你最小变暗几个阳. ...
- HDU 5727 Necklace ( 2016多校、二分图匹配 )
题目链接 题意 : 给出 2*N 颗珠子.有 N 颗是阴的.有 N 颗是阳的.现在要把阴阳珠子串成一个环状的项链.而且要求珠子的放置方式必须的阴阳相间的.然后给出你 M 个限制关系.格式为 ( A.B ...
- HDU 2819 Swap(二分图匹配)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
- HDU 5727 - Necklace
题意:( 0 <= n <= 9 ) 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环, 已知有些阴珠子和阳珠子不能放在相邻的位置,否则这颗阳珠子就会失去功效, ...
- hdu 5943(素数间隔+二分图匹配)
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- TTTTTTTTTTTTTTTT hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列
Necklace Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
随机推荐
- python eval和literal_eval
eval是python中一个相当智能的函数,把参数当成表达式,进行最大限度的解析, 比如: a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" b ...
- 安装jdk源码
step1:打开选择Window->Preference step2:选择Java->Installed JREs step3:选中你所安装的jre,点击Edit,进入Edit JRE,如 ...
- 股票交易(洛谷U6084)
题目背景 kkk最近迷上了炒股. 题目描述 kkk炒了N天股,第i天的股价为a[i]元.kkk希望股票每天都上涨1元钱,但是操盘手lzn并不想让kkk赚很多钱导致他亏本,于是a[i]相对a[i-1]就 ...
- Mysql分布式事务
关于Mysql分布式事务介绍,可参考:http://blog.csdn.net/luckyjiuyi/article/details/46955337 分为两个阶段:准备和执行阶段.有两个角色:事务的 ...
- JAVA作业02
一, 课堂练习 (一)构造方法 1,源代码 public class Test{ public static void main(String[] args){ Foo obj1=new F ...
- CLR via C#学习笔记----知识总概括
第1章 CLR的执行模型 托管模块的各个组成部分:PE32或PE32+头,CLR头,元数据,IL(中间语言)代码. 高级语言通常只公开了CLR的所有功能的一个子集.然而,IL汇编语言允许开发人员访问C ...
- java web开发环境配置
一:安装jdk http://www.java.com/zh_TW/ 二:下载tomcat,这里是zip版.http://tomcat.apache.org/download-70.cgi 三:在 ...
- 让ASP.NET MVC不使用jsonp也可以跨域访问
跨域问题仅仅发生在Javascript发起AJAX调用,或者Silverlight发起服务调用时,其根本原因是因为浏览器对于这两种请求,所给予的权限是较低的,通常只允许调用本域中的资源,除非目标服务器 ...
- uC/OS II原理分析及源码阅读(一)
uC/OS II(Micro Control Operation System Two)是一个可以基于ROM运行的.可裁减的.抢占式.实时多任务内核,具有高度可移植性,特别适合于微处理器和控制器,是和 ...
- outlook备份及恢复
outlook备份及恢复 Reference: http://wenku.baidu.com/link?url=2gtDkCSDoPdnfx3Ungd6on9wdhUTWgbO_vmmKLv1i4df ...