只想到了二分图的做法
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;
const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;
#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))
#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;
int n,m,k,ans,tot=1,hd[(int)2e5+9],cur[(int)2e5+9],s,t,dep[(int)2e5+9];
vector<pii> v;
map<pii,int> exi;
pii dir[4]={{1,0},{0,1},{-1,0},{0,-1}};
map<pii,int> cov;
struct Edge{
int to,nxt,v;
}e[(int)1e7+9];
void add(int u,int v){
e[++tot]={v,hd[u],1},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}
bool bfs(int st,int en){
for(int i=0;i<=t;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}
int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}
int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,0x3f3f3f3f);
}
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
v.resize(k+1);
for(int i=1,x,y;i<=k;++i){
cin>>x>>y;
cov[{x,y}]=i,v[i]={x,y},exi[{x,y}]=1;
}
s=2*k+1,t=2*k+2;
for(int i=1;i<=k;++i){
auto it=v[i];
auto p=cov[it];
add(s,p),add(p+k,t);
for(auto [aa,bb]:dir){
aa+=it.first,bb+=it.second;
//cerr<<it.first<<"--"<<it.second<<":::"<<aa<<"--"<<bb<<"\n";
if(exi.count({aa,bb})){
auto pp=cov[{aa,bb}];
add(p,pp+k);
}
}
}
bool ok=(dinic(s,t)==k);
cout<<(ok?"YES\n":"NO\n");
return 0;
}