Necklace

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2423    Accepted Submission(s): 766

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
/*
hdu 5727 二分图+环排列 problem:
要用n个阳石和n个阴石来串一个项链(环状),规定阳石旁边只能是阴石,阴石旁只能是阳石,现在有m对特殊阴阳石,
这些阴阳石相邻会使得阳石出故障(照样可以用),问串这个项链,至少有几个故障的阳石。 solve:
最开始一看题就感觉应该是二分匹配,发现往环的阴石中添加阳石每个位置要考虑左右两边的情况
所以可以枚举阴石的所有情况.然后对每个空位和所有阳石之间建图。即这里可以放阳石就置为1,然后跑个最大匹配得出cnt
那么n-cnt就是当前情况最少的故障数 枚举这个是用的系统自带的next_permutation,但是一直超时.后来看别人题解才发现环排列只需要(n-1)!.所以可以固定一个
位置的值,枚举剩下的即可 hhh-2016-08-16 11:07:51
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 11;
const int inf = 10000;
struct node
{
int to,next;
} edge[maxn*maxn];
int tot;
int tmap[maxn][maxn];
int head[maxn]; void add(int u,int v)
{
edge[tot].to = v,edge[tot].next = head[u];
head[u] = tot++;
}
int link[maxn],vis[maxn];
bool dfs (int u)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (!vis[v])
{
vis[v] = 1;
if (link[v] == -1 || dfs (link[v]))
{
link[v] = u;
return 1;
}
}
}
return 0;
}
int n,m;
int ans = inf; int cal()
{
int res = 0;
memset(link,-1,sizeof(link));
for(int i = 1; i <= n; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
res ++;
}
return n-res;
}
int po[maxn*2]; int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{ memset(tmap,0,sizeof(tmap));
int a,b;
for(int i =1; i <= m; i++)
{
scanf("%d%d",&a,&b);
tmap[a][b] = 1;
}
if(!n || !m)
{
printf("0\n");
continue;
}
for(int i = 1; i <= n; i++)po[i] = i;
ans = inf;
do
{
tot = 0;
memset(head,-1,sizeof(head));
for(int i =1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
int pre = i-1,next = i;
if(!pre) pre = n;
if(!tmap[j][po[pre]] && !tmap[j][po[next]])
add(j,i);
}
}
ans = min(ans,cal());
if(!ans)
break;
}
while(next_permutation(po+2,po+n+1));
printf("%d\n",ans);
}
return 0;
}

  

hdu 5727 二分图+环排列的更多相关文章

  1. HDU 5727 Necklace 环排+二分图匹配

    这是从山东大学巨巨那里学来的做法 枚举下黑色球的排列总数是8!,然后八个白球可选的位置与左右两个黑球存不存在关系建图就行 这是原话,具体一点,每次生成环排,只有互不影响的才连边 最后:注重一点,n个数 ...

  2. HDU 6432(不连续环排列 ~)

    题意是说在长度为 n 的环排列中,按照一定的方向(顺时针或逆时针),后一个数不能仅比前一个数大 1 , n 的下一个数不能是 1 ,问这种长度为 n 且本质不同(本质不同指环上数字的相对位置不同,如 ...

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

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

  4. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  5. HDU 5727 Necklace(全排列+二分图匹配)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 题意:现在有n个阳珠子和n个阴珠子,现在要把它们串成项链,要求是阴阳珠子间隔串,但是有些阴阳珠 ...

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

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

  7. hdu 5727 Necklace 二分图匹配

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

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

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

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

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

随机推荐

  1. c语言博客第二次作业

    一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 { double x,y; scanf("%lf",&x); if(x>=0) { y=pow(x,0. ...

  2. POST请求的提交

    var http = require("http"); var querystring = require("querystring"); //创建服务器 va ...

  3. 利用Node的chokidar 监听文件改变的文件。

    最近维护一个项目.每次改完东西,都要上传到服务器.然后有时候就忘记一些东西,于是就想有没有可以方法能监听文件的改变.然后我再利用程序把更改的文件一键上传到服务器. 于是就找到了nodejs 的chok ...

  4. mysql5.5中datetime默认值不能为NOW或者CURRENT_TIMESTAMP,用触发器解决

    mysql5.6及以上的版本datatime默认值可以为CURRENT_TIMESTAMP或者NOW 那我们要用的是mysql5.5及以下版本呢? 请看代码 delimiter // DROP TRI ...

  5. JAVA_SE基础——63.String类的常用方法

    获取方法int length()  获取字符串的长度char charAt(int index) 获取特定位置的字符 (角标越界)int indexOf(String str) 查找子串第一次出现的索 ...

  6. 利用封装、继承对Java代码进行优化

    注:本文实例分别可以在oldcastle(未优化的代码)和newcastle(优化后的代码)中查看,网址见文末 城堡游戏: 城堡中有多个房间,用户通过输入north, south, east, wes ...

  7. 儿童节,我们从零开始——Python入门资源推荐

    原创 2017-06-01 玄魂工作室 玄魂工作室 今天是六一儿童节,首先祝所有的小朋友身体健康,能永远生活在一个没有战争,没有压迫的世界里,永远快乐. 上一篇文章,很多人都对Python的各种书籍感 ...

  8. api-gateway实践(16)【租户模块:修改api定义】通过mq通知【开发者模块:更新开发者集市】

    一.订阅关系 二.接收消息 dev模块接收更新本地集市

  9. 图数据库orientDB(1-2)例子

    http://gog.orientdb.com/index.html#/infotab 小朱25岁,出生在教师家庭并且有个姐姐小田,他现在奋斗在帝都.  那么SQL是这样滴!!! CREATE VER ...

  10. 新概念英语(1-32)A fine day

    新概念英语(1-33)A fine day Where is the Jones family? It is a fine day today. There are some clouds in th ...