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

时间:2022-06-12 23:13:30

Necklace

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

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.

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
给2*n个珠子, n<=9, n个阴n个阳。 然后将它们弄成一个环, 阴阳交替。现在给你m个关系, 每个关系给出a, b。 如果阳a和阴b挨着, 那么a就会变暗。 问你最小变暗几个阳。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
#define CT continue
#define SC scanf
const int N=1e5+10;
int f[50][50],a[50],match[50],used[50];
vector<int> G[50];
int n,m,u,v; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u)
{
used[u]=1;
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
int w=match[v];
if(w<0||(!used[w]&&dfs(w))){
match[u]=v;
match[v]=u;
return true;
}
}
return false;
} int bi_match()
{
MM(match,-1);
int res=0;
for(int i=1;i<=n;i++)
if(match[i]<0){
MM(used,0);
if(dfs(i)) res++;
}
return res;
} int main()
{
while(~SC("%d%d",&n,&m))
{
MM(f,0);
for(int i=1;i<=m;i++){
SC("%d%d",&u,&v);
f[u][v]=1;
}
if(n==0||m==0){
printf("0\n");
CT;
}
int ans=0;
for(int i=1;i<=n;i++) a[i]=i;
a[n+1]=a[1];
do{
for(int i=1;i<=2*n;i++) G[i].clear();
for(int u=1;u<=n;u++) {
for(int j=1;j<=n;j++){
int pre=a[j],lat=a[j+1];
if(!f[u][pre]&&!f[u][lat])
add_edge(u,j+n);
}
}
ans=max(ans,bi_match());
}while(next_permutation(a+2,a+n+1));
printf("%d\n",n-ans);
}
return 0;
}

  分析:全排列一下阴珠子形成一个环,然后对于形成的每个位置,如果该位置可以放下当前枚举的阳珠子,就连接一条边,那么能不变质的最大阳珠子个数,就是二分图的足最大匹配数。

易错点:

int v=G[u][i];
int w=match[v];
if(w<0||(!used[w]&&dfs(w))){
match[u]=v;
match[v]=u;
return true;
}
把这个地方写成了match[v]<0||!used[v]&&dfs(v);悲剧的wa了好几发,其实应该是w=match[v]来匹配的