91分能想办法卡过吗qwq

P3355 骑士共存问题

mattinas @ 2021-05-13 13:31:51

#include<bits/stdc++.h>
using namespace std;
#define maxn 40010
bool cant[maxn];
int N,M,K;
int last[maxn];
int dx[8]={-2,2,-2,2,-1,1,-1,1};
int dy[8]={1,1,-1,-1,2,2,-2,-2};
int num(int x,int y)
{
    return (x-1)*M+y;
}
int len=0;
int v[maxn];
int f[maxn];
struct node{int x,y,next;}e[maxn*8];
void ins(int x,int y)
{
    len++;
    e[len]={x,y,last[x]};
    last[x]=len;
}
bool dfs(int x,int yyy)
{
    for(int i=last[x];i!=-1;i=e[i].next)
    {
        int y=e[i].y;
        if(v[y]!=yyy)
        {
            v[y]=yyy;
            if(f[y]==0||dfs(f[y],yyy))
            {
                f[y]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    scanf("%d%d",&N,&M);
    int x,y,cnt=0;
    memset(last,-1,sizeof(last));
    K=M;M=N;
    for(int i=1;i<=K;i++)
    {
        scanf("%d%d",&x,&y);
        if(cant[num(x,y)])continue;
        cant[num(x,y)]=true;
        cnt++;
    }
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            if(!cant[num(i,j)])
            {
                int u=num(i,j);
                for(int k=0;k<8;k++)
                {
                    x=dx[k]+i;
                    y=dy[k]+j;
                    if(x>=1&&x<=N&&y>=1&&y<=M&&!cant[num(x,y)])
                        ins(u,num(x,y));
                }
            }
        }
    }
    int ans=0;
    memset(v,0,sizeof(v));
    for(int i=1;i<=N;i++)
    {
        for(int j=1;j<=M;j++)
        {
            x=num(i,j);
            if(!cant[x]&&dfs(x,x))ans++;
        }
    }
    printf("%d",N*M-cnt-ans/2);
    return 0;
} 

by mattinas @ 2021-05-13 13:37:40

本地跑4s大概是没希望了


by Stinger @ 2021-05-13 13:50:37

@mattinas 匈牙利算了吧,换成dinic


by mattinas @ 2021-05-13 13:51:46

@zhangqs 嗯嗯谢谢大佬


by Citnaris @ 2021-08-31 19:48:22

匈牙利算法改成邻接表就能过的样子(


|