2016 Multi-University Training Contest 1 Necklace 环排+二分匹配

时间:2022-10-29 20:07:05

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5727

题意:由2*N颗宝石构成的环(阴阳宝石均为N颗且标号均从1~N) 之后给定M组 a,b;表示阳宝石a若和阴宝石b相邻会使得阳宝石变暗,问所构成的环中阳宝石变暗的最少数量?

其中(1<=N<=9, 1<= M <= N*N)

思路:确定一个阴宝石(我代码中让1不动),其余的环排。

将空隙变成一个点,先将所夹空隙的两点与阳宝石每点建图之后跑匈牙利即可,这跑出来的是最大匹配数(即最多不变暗的阳宝石数);

稍加剪枝还是容易过的;8! = 40320

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
#define MSi(a) memset(a,0x3f,sizeof(a))
#define inf 0x3f3f3f3f
#define A first
#define B second
#define MK make_pair
#define esp 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
#define bitnum(a) __builtin_popcount(a)
#define clear0 (0xFFFFFFFE)
#define mod 1000000007
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
template<typename T>
void read1(T &m)
{
T x = ,f = ;char ch = getchar();
while(ch <'' || ch >''){ if(ch == '-') f = -;ch=getchar(); }
while(ch >= '' && ch <= ''){ x = x* + ch - '';ch = getchar(); }
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
inline ll gcd(ll a,ll b){ return b == ? a: gcd(b,a%b); }
template<class T1, class T2> inline void gmin(T1& a,T2 b) { if(a > b) a = b; }
template<class T1, class T2> inline void gmax(T1& a,T2 b) { if(a < b) a = b; }
int S[][], f[], n;
int vis[], girl[], line[][];
int dfs(int p)
{
rep1(i,,n){
if(line[p][i] && !vis[i]){
vis[i] = ;
if(!girl[i] || dfs(girl[i])){
girl[i] = p;
return ;
}
}
}
return ;
} void init()
{
MS0(girl);MS0(line);
rep1(i,,n) rep1(j,,n) if(!S[i][f[j]] && !S[i][f[j+]]) line[i][j] = ;
} int solve()
{
init(); //建边
int ans = ;
rep1(i,,n){
MS0(vis);
if(dfs(i)) ans++;
}
return n-ans;
}
int main()
{
//freopen("data.txt","r",stdin);
//freopen("out.txt","w",stdout);
int m, a, b;
while(scanf("%d%d",&n, &m) == ){
MS0(S);
rep1(i,,m){
read2(a,b);
S[a][b] = ;
}
rep1(i,,n) f[i] = i; f[n+] = ;
int ans = inf;
do{
gmin(ans, solve());
}while(next_permutation(f+,f+n+) && ans);
printf("%d\n",ans);
}
return ;
}