[BZOJ1381]Knights

Description
在一个N*N的棋盘上,有些小方格不能放骑士,棋盘上有若干骑士,任一个骑士不在其它骑士的攻击范围内,请输出最多可以放多少个骑士. 骑士攻击的点如中国象棋中的马,可以攻击8个点.

Input
第一行给出N,M代表棋盘的大小及故障点的个数 下面M行,给出故障点的坐标
1<=n<=200, 0<=m

Output
最多可以放多少个

Sample Input
3 2
1 1
3 3

Sample Output
5


二分图最大独立点集,由于\(N\)不是特别大,因此我们可以暴力枚举连边。
值得注意的一点是,我们需要提前规定好是由白块匹配黑块还是由黑块匹配白块,否则匹配就失去了它的准确性

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())    if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())  x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+'0');
}
const int N=2e2;
const int dx[8]={-2,-2,-1,-1,1,1,2,2};
const int dy[8]={1,-1,2,-2,2,-2,1,-1};
int pre[N*N*4+10],now[N*N/2+10],child[N*N*4+10],path[N*N/2+10],col[2];
int num[N+10][N+10];
bool use[N*N/2+10],map[N+10][N+10];
int n,m,ans,tot;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
bool in_map(int x,int y){return x>0&&x<=n&&y>0&&y<=n&&!map[x][y];}
void connect(int x,int y){
    for (int i=0;i<8;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if (in_map(tx,ty))  join(num[x][y],num[tx][ty]);
    }
}
bool check(int x){
    for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
        if (use[son])   continue;
        use[son]=1;
        if (path[son]<0||check(path[son])){
            path[son]=x;
            return 1;
        }
    }
    return 0;
}
int main(){
    n=read(),m=read();
    memset(path,-1,sizeof(path));
    for (int i=1;i<=m;i++)       map[read()][read()]=1;
    for (int i=1;i<=n;i++)   for (int j=1;j<=n;j++)   if (!map[i][j]) num[i][j]=++col[(i+j)&1];
    for (int i=1;i<=n;i++)   for (int j=1;j<=n;j++)   if (!map[i][j]&&(i+j)&1)    connect(i,j);
    for (int i=1;i<=col[1];i++){
        memset(use,0,sizeof(use));
        if (check(i))   ans++;
    }
    printf("%d\n",col[0]+col[1]-ans);
    return 0;
}

posted @ 2018-02-06 23:40  Wolfycz  阅读(197)  评论(0编辑  收藏  举报