题目链接:HDU 5727
题面:
Necklace
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1576 Accepted Submission(s): 499
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 Y with Yin energy.
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
Author
HIT
Source
题意:
给定n个阳珠子,n个阴珠子。部分阳珠子和阴珠子有排斥关系,阳珠子若和这些阴珠子放在一起会变暗,问怎样环形排列阳珠子,可以使得阳珠子变暗的次数最少。
解题:
可以先全排列阴珠子,因为是环形,可以固定一种,随后全排列其他,复杂度为(8!),随后找出每个阳珠子和位置的互斥关系,建立一张关系图,用匈牙利算法求出最大匹配数即可。
代码:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define sz 100005
using namespace std;
bool vis[10],edge[10][10],rel[10][10];
int yin[10],links[10];
int n,m,a,b,ans;
//搜寻合法位置
int dfs(int x)
{
for(int i=1;i<=n;i++)
{
//如果位置x可以放置阳珠子i
if(edge[x][i])
{
//如果i珠子还没被访问到
if(!vis[i])
{
vis[i]=1;
//如果i珠子还未使用,或者可以找到替代
if(links[i]==-1||dfs(links[i]))
{
links[i]=x;
return 1;
}
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
//没珠子特判
if(n==0)
{
for(int i=0;i<m;i++)
scanf("%d%d",&a,&b);
printf("0\n");
}
else
{
ans=1000000;
memset(rel,0,sizeof(rel));
//建立矛盾关系
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
rel[a][b]=1;
}
//生成阴珠子的全排列,枚举阳珠子,因为阳珠子碰到阴珠子会褪色
//故建立阳珠子和位置的合法关系,利用二分图匹配得到最大的不干扰位置
for(int i=1;i<=n;i++)
yin[i]=i;
do
{
//edge[i][j]为1表示位置i可以放置阳珠子j
memset(edge,0,sizeof(edge));
//links表明二分图的连接关系
memset(links,-1,sizeof(links));
for(int i=1;i<n;i++)
for(int j=1;j<=n;j++)
//位置i左右两侧都不受阴珠子影响
if((!rel[j][yin[i]])&&(!rel[j][yin[i+1]]))
edge[i][j]=1;
//某位置特判
for(int i=1;i<=n;i++)
{
if((!rel[i][yin[n]])&&(!rel[i][yin[1]]))
edge[n][i]=1;
}
int cnt=0;
//计算可以放置阳珠子的最多位置
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
cnt+=dfs(i);
}
ans=min(ans,n-cnt);
}
//判断阴珠子全部全排列
while(next_permutation(yin+2,yin+n+1)&&ans);
printf("%d\n",ans);
}
}
return 0;
}