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 ...
随机推荐
- [Android Pro] Dangerous permissions and permission groups.
Permission Group Permissions CALENDAR READ_CALENDAR WRITE_CALENDAR CAMERA CAMERA CONTACTS READ_CONTA ...
- 创建Oracle10g ASM数据库
概述 在真正的海量数据库环境中,DBA可能会花费很多的时间来作磁盘管理,比如一个表空间将占满整个磁盘,DBA就需要再添加一块磁盘到操作系统中,然后再在新的磁盘上创建新的数据文件,如果是单个磁 ...
- 51nod1069(nim博弈)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1069 题意: 中文题诶~ 思路: 一开始我以为需要把上一堆到 ...
- 简单的mysql封装类
class mysql{ private $host; private $user; private $pwd; private $dbname; private $charset; private ...
- jq点击和鼠标移上效果以及一个等号"=" 二个等号"==" 三个等号"===" 的区别
<body> <div class="a" bs='1' style="width:100px; height:30px; border:1px sol ...
- ASP.NET MVC中ViewData、ViewBag和TempData
1.ViewData 1.1 ViewData继承了IDictionary<string, object>,因此在设置ViewData属性时,传入key必须要字符串型别,value可以是任 ...
- linux cpuInfo
转自:http://blog.csdn.net/lgstudyvc/article/details/7889364 /proc/cpuinfo文件分析 在Linux系统中,提供了proc文件系统显 ...
- Validform 学习笔记---基础知识整理
面对表单的验证,自己写大量的js毕竟不是一个明智的做法.不仅仅是代码很长而且不便于梳理.Validform就是一款开源的第三方验证js的控件,通过添加相应的js以及css能够有效的验证表单,维护起来也 ...
- HDU3359 Kind of a Blur(高斯消元)
建立方程后消元 #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring& ...
- go sample - hello world
入门级别的hello world package mainimport "fmt"func main() { fmt.Println("blibliblbibl!&quo ...