AC代码(二分图匹配问题)
Select Code
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#define maxn 20010
using namespace std;
int vis[maxn], link[maxn], n;
vector<int>edge[maxn];
struct node
{
int x, y;
} a[maxn];
int pan(int i, int j)
{
if((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)==1)
return 1;
return 0;
}
int dfs(int u)
{
int i, k;
for(i = 0; i<edge[u].size(); i++)
{
k = edge[u][i];
if(!vis[k])
{
vis[k] = 1;
if(link[k]==-1||dfs(link[k]))
{
link[k] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int i, j;
while( scanf("%d",&n)!=EOF)
{
for(i = 1; i<=n; i++)
{
scanf("%d %d",&a[i].x, &a[i].y);
edge[i].clear();
}
for(i = 1; i<=n; i++)
{
for(j = 1; j<=n; j++)
{
if(pan(i, j))
{
edge[i].push_back(j);
edge[j].push_back(i);
}
}
}
memset(link, -1, sizeof(link));
int ans = 0;
for(i = 1; i<=n; i++)
{
if((a[i].x+a[i].y)%2)
{
memset(vis, 0, sizeof(vis));
if(dfs(i))
ans++;
}
}
printf("%d\n",n-ans);
}
return 0;
}