Skip to content

Latest commit

 

History

History
60 lines (60 loc) · 1.69 KB

POJ2446.md

File metadata and controls

60 lines (60 loc) · 1.69 KB

二分图匹配 每个点和它相邻的可以组成一块的点互相连边,然后二分图匹配判断是否所有点都能匹配上

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
using namespace std;
const int MAXN = 2222;
const int dir[2][2] = {{1,0},{0,1}};
int n,m,k,vis[MAXN],match[MAXN];
vector<int> G[MAXN];
set<pair<int,int> > coor;
bool dfs(int u){
    vis[u] = true;
    for(int i = 0; i < (int)G[u].size(); i++){
        int v = G[u][i];
        if(match[v]==-1||(!vis[match[v]]&&dfs(match[v]))){
            match[v] = u;
            return true;
        }
    }
    return false;
}
bool hungary(){
    memset(match,255,sizeof(match));
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){
        if(coor.count(make_pair(i,j))) continue;
        memset(vis,0,sizeof(vis));
        if(!dfs((i-1)*m+j)) return false;
    }
    return true;
}
int main(){
    while(scanf("%d %d %d",&n,&m,&k)!=EOF){
        for(int i = 0; i < MAXN; i++) G[i].clear();
        coor.clear();
        for(int i = 1; i <= k; i++){
            int x, y;
            scanf("%d %d",&x,&y);
            coor.insert(make_pair(y,x));
        }
        for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++){
            if(coor.count(make_pair(i,j))) continue;
            for(int d = 0; d < 2; d++){
                int nx = i + dir[d][0];
                int ny = j + dir[d][1];
                if(nx<=n&&ny<=m&&!coor.count(make_pair(nx,ny))){
                    G[(nx-1)*m+ny].push_back(((i-1)*m+j));
                    G[(i-1)*m+j].push_back((nx-1)*m+ny);
                }
            }
        }
        if(hungary()) puts("YES");
        else puts("NO");
    }
    return 0;
}