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. ord在python是什么意思?

    >>> help(ord)Help on built-in function ord in module builtins:ord(...) #这是一个函数 ord(c) -> ...

  2. 创建带缩进的XML

    from xml.etree import ElementTree as ET from xml.dom import minidom root = ET.Element('}) son=ET.Sub ...

  3. Python 3.* print 出现SyntaxError: invalid syntax

    很简单,不知道为啥,据说是3.0以后的print都改为了print(); 例如 a=1 print a 上边出错 输入 a=1 print(a) 就正确了

  4. bzoj千题计划252:bzoj1095: [ZJOI2007]Hide 捉迷藏

    http://www.lydsy.com/JudgeOnline/problem.php?id=1095 点分树+堆 请去看 http://www.cnblogs.com/TheRoadToTheGo ...

  5. redux的知识点

    Redux: Redux 是针对 JavaScript应用的可预测状态容器 就是用来管理数据的.stroe 保存数据action领导 下达命令reducer员工 执行命令 下载命令:  npm ins ...

  6. JS 转换数据类型

    JavaScript 是一种动态数据类型语言,变量是没有类型的,可以随机赋予任意值,若变量要转换数据类型,有两种办法:隐式转换和显式转换. 隐式转换可转换为字符串(将一个值加上字符串) 数字(在值的前 ...

  7. 使用cxf创建webservice 出现timeOut的问题,设置spring超时时间

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. hadoop2.6.0实践:003 检查hadoop是否可用

    start-dfs.sh start-yarn.sh 1.检查hdfs hdfs dfs -ls / http://localhost:50070 2.运行例子程序 hdfs dfs -ls / hd ...

  9. 用Jmeter实现SQLServer数据库的增删查改

    1.添加线程组 Jmeter性能测试,最重要的就是线程组了,线程组相当于用户活动 2.添加JDBC Connection Configuration Database URL:jdbc:sqlserv ...

  10. __new__ 单例

    a.实例化类 实例化一个类时 1. 创建一个对象,调用__new__方法,如果没有会调用父类的__new__方法 2. 调用__init__方法 3. 返回对象的引用 class Dog(object ...