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 ...
随机推荐
- Qt Creator 中关于调试器的设置
Qt Creator3.4.2 Based on Qt 5.5.0 (MSVC 2013, 32 bit) 在安装了VS2013的电脑上能够自动识别VS编译器,却不能识别调试器 需要下载一个wdk,安 ...
- innodb之超时参数配置
可参考:http://www.penglixun.com/tech/database/mysql_timeout.html 下面内容摘取自上面这个链接. connection_timeout,只是设置 ...
- C语言文件操作相关函数
在实际应用中,我们往往需要对文件进行操作,下面我将介绍C语言的一些关于操作文件的函数. 一.计算机文件 计算机文件是以计算机硬盘为载体存储在计算机上的信息集合,是存储在某种长期储存设备上的一段数据流. ...
- EasyUi – 6.easyui常见问题
1.进度条 2.JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 3. 三张表的连接查询现在到datagrid里 4.日期组合框DateBox设置readonly ...
- Kl 证明 凸函数
回到随机变量传输问题,假设传输中我们不知道具体 分布情况(unknown),我们用一个已知的分布 ,来模拟它,那么在这种情况下如果我们利用 尽可能高效的编码,那么我们平均需要多少额外的信息量来描述x呢 ...
- Linux命令中特殊符号
转自:http://blog.chinaunix.net/uid-16946891-id-5088144.html 在shell中常用的特殊符号罗列如下:# ; ;; . , / \ 'strin ...
- 解析PHP处理换行符的问题 \r\n
一首先说说 \r 与\n的区别回车”(Carriage Return)和“换行”(Line Feed)这两个概念的来历和区别.在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model ...
- Spring之ResourceLoader加载资源
Resource与ResourceLoader对比 1.Resource接口定义了应用访问底层资源的能力. 通过FileSystemResource以文件系统绝对路径的方式进行访问: 通过ClassP ...
- 从官方ROM中提取原生APK
背景:由于自己手机总出现android.process.acore问题,最后发现是被自己精简掉了日历相关应用,故寻找提取原生apk. 注:解决方案主要是在机锋论坛上看到的. 环境要求:需要电脑安卓ja ...
- 阿里云(ECS)Centos服务器LNMP环境搭建
阿里云( ECS ) Centos7 服务器 LNMP 环境搭建 前言 第一次接触阿里云是大四的时候,当时在校外公司做兼职,关于智能家居项目的,话说当时俺就只有一个月左右的 php 后台开发经验(还是 ...