题意:
n颗阳宝石,n颗阴宝石组成一个项链,m对x,y,表示阳宝石x和阴宝石y挨在一起,阳宝石会变暗,求最少的变暗个数。
分析:
n颗阳宝石,n颗阴宝石组成一个项链,m对x,y,表示阳宝石x和阴宝石y挨在一起,阳宝石会变暗,求最少的变暗个数。
分析:
先枚举所有阴宝石排列情况(环,(n-1)!种),然后插入阳宝石,判断阳宝石插入在之间会不会变暗,不会则加边,然后二分图匹配。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<cctype>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<iomanip>
#include<sstream>
#include<limits>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const ll INF = 1e18;
const int maxn = 2e5+10;
const ll MOD = 1000000007;
const double EPS = 1e-10;
const double Pi = acos(-1.0);
const int MAXN = 3000;//点
const int MAXM = 50000; //边
struct Edge
{
int to,next;
}edge[MAXM];
int head[MAXN],tot;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[tot].to = v;edge[tot].next = head[u];
head[u] = tot++;
}
int linker[MAXN];
bool used[MAXN];
int uN;
bool dfs(int u)
{
for(int i = head[u]; i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(!used[v])
{
used[v] = true;
if(linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = 0;u < uN;u++) //编号0-uN-1
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
}
bool mp[100][100];
int a[10];
int main(){
#ifdef LOCAL
freopen("C:\\Users\\lanjiaming\\Desktop\\acm\\in.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
//ios_base::sync_with_stdio(0);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mp,true,sizeof(mp));
for(int i = 0; i < m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x][y] = false;
}
if(n==0) {printf("0\n"); continue;}
for(int i = 1; i <= n; i++) a[i] = i;
int ans = n;
do{
init();
uN = n;
for(int i = 1; i <= n; i++)
{
int pre = i-1;
if (pre == 0) pre = n;
for(int j = 1; j <= n; j++)
if (mp[j][a[i]] && mp[j][a[pre]])
addedge(i-1,j-1+n);
}
ans = min(ans,n-hungary());
}while(ans&&next_permutation(a+1,a+n));
printf("%d\n",ans);
}
return 0;
}