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+二分图匹配的更多相关文章

  1. HDU 5727 Necklace(二分图匹配)

    [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5727 [题目大意] 现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,已知有些阴珠子和阳 ...

  2. HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...

  3. hdu 5727 Necklace 二分图匹配

    题目链接 给2*n个珠子, n<=9, n个阴n个阳. 然后将它们弄成一个环, 阴阳交替.现在给你m个关系, 每个关系给出a, b. 如果阳a和阴b挨着, 那么a就会变暗. 问你最小变暗几个阳. ...

  4. HDU 5727 Necklace ( 2016多校、二分图匹配 )

    题目链接 题意 : 给出 2*N 颗珠子.有 N 颗是阴的.有 N 颗是阳的.现在要把阴阳珠子串成一个环状的项链.而且要求珠子的放置方式必须的阴阳相间的.然后给出你 M 个限制关系.格式为 ( A.B ...

  5. HDU 2819 Swap(二分图匹配)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...

  6. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  7. HDU 5727 - Necklace

    题意:( 0 <= n <= 9 )    现在有n颗阴珠子和n颗阳珠子,将它们阴阳相间圆排列构成一个环,    已知有些阴珠子和阳珠子不能放在相邻的位置,否则这颗阳珠子就会失去功效,   ...

  8. hdu 5943(素数间隔+二分图匹配)

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. TTTTTTTTTTTTTTTT hdu 5727 Necklace 阴阳珠 二分图匹配+暴力全排列

    Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

随机推荐

  1. Android笔记:获取屏幕信息

    像素密度(dpi) float xdpi = getResources().getDisplayMetrics().xdpi;float ydpi = getResources().getDispla ...

  2. Tomcat在Linux上的安装与配置

    以下使用的Linux版本为: Redhat Enterprise Linux 6.5 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JDK与Tomcat.    jdk下载地址 ...

  3. 四、优化及调试--网站优化--Yahoo军规上

    什么是Yahoo军规?即如何提高网站速度的知识. 具体如下: 1.尽量减少HTTP请求个数——须权衡 什么是http请求:从客户端到服务器端的请求消息.包括消息首行中,对资源的请求方法,资源的标识符及 ...

  4. 四、优化及调试--网站优化--SEO在网页制作中的应用

    SEO分类:白帽SEO.黑帽SEO 白帽SEO: 内容上的SEO: 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 前端SEO: 网站结构布局优化 扁平化结构(一 ...

  5. 什么是DMI,SMBIOS,符合SMBIOS规范的计算机的系统信息获取方法

    转自:http://www.cnblogs.com/gunl/archive/2011/08/08/2130719.html DMI是英文单词Desktop Management Interface的 ...

  6. 微信支付:“当前页面的URL未注册”

    最近在尝试着写微信支付方面的内容,今天下午的时候遇到了一个问题,也就是当前页面没有进行注册的问题. 在写好后台代码之后我在微信后台配置了测试授权目录和支付白名单,如下图所示,但是无论我如何进行测试总是 ...

  7. Newtonsoft.Json(Json.Net)学习笔记-高级使用(转)

    1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称 7.动态决定属性是否序列化 8.枚举值的自定义格式化问题 9.自定义类型转换 10.全 ...

  8. How many Fibs?【sudt 2321】【大数的加法及其比较】

    How many Fibs? Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Recall the definition of t ...

  9. JAVA中this用法小结

    Java关键字this只能用于方法方法体内.当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this.因此,this只能在类中的非静态方法中使用,静 ...

  10. Swipe to back not working滑动后退功能消失?

    如果你发现滑动后退功能突然失效了,很可能是因为你隐藏了NavigationBar 或者定制了 leftBarButtonItem(s) 这会导致 NavigationController 的 inte ...