思路:枚举阴球的全排列,然后如果和阳球不冲突的话就在这个位置和阳球连一条边,求二分图最大匹配,最后n-最大匹配就是冲突的最小个数
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=120+5;
#define INF 999999
int ans = 0;
int vis[maxn],pos[maxn];
int n,m;
int mp[20][20];
struct Max_Match
{
int n;
vector<int> g[maxn];
// int g[maxn][maxn];
bool vis[maxn];
int left[maxn];
void init(int n)
{
this->n=n;
for(int i=1; i<=n; ++i) g[i].clear();
// memset(g,0,sizeof(g));
memset(left,-1,sizeof(left));
}
bool match(int u)
{
for(int i=0;i<g[u].size();++i)
{
int v=g[u][i];
if(!vis[v])
{
vis[v]=true;
if(left[v]==-1 || match(left[v]))
{
left[v]=u;
return true;
}
}
}
return false;
}
int solve()
{
int ans=0;
for(int i=1; i<=n; ++i)
{
memset(vis,0,sizeof(vis));
if(match(i)) ++ans;
}
return ans;
}
}MM;
void work()
{
MM.init(n*2);
for(int i = 2;i<=n;i++)
for(int j = 1;j<=n;j++)
{
if(mp[pos[i]][j] || mp[pos[i-1]][j])
continue;
MM.g[i].push_back(j);
}
for(int i = 1;i<=n;i++)
{
if(mp[pos[1]][i] || mp[pos[n]][i])
continue;
MM.g[1].push_back(i);
}
ans = min(ans,n-MM.solve());
}
int jianzhi = 0;
void shengchengquanpailie(int cnt)
{
// jianzhi++;
// if(jianzhi>100000)
// return;
if(!ans)
return;
if(cnt==n+1)
{
work();
return;
}
for(int i = 1;i<=n;i++)
{
if(vis[i])
continue;
pos[cnt]=i;
vis[i]=1;
shengchengquanpailie(cnt+1);
pos[cnt]=0;
vis[i]=0;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0)
{
printf("0\n");
continue;
}
memset(vis,0,sizeof(vis));
memset(pos,0,sizeof(pos));
memset(mp,0,sizeof(mp));
jianzhi = 0;
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
mp[v][u]=1;
}
ans = INF;
pos[1]=1;
vis[1]=1;
shengchengquanpailie(2);
printf("%d\n",ans);
}
return 0;
}
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