2446

1 /*
2 二分匹配的题目真的有意思
3 如何把问题转化为二分图模型,这才是问题的关键,其他都是浮云
4 */
5
6 // include file
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10 #include <cmath>
11 #include <cctype>
12 #include <ctime>
13
14 #include <iostream>
15 #include <sstream>
16 #include <fstream>
17 #include <iomanip>
18 #include <bitset>
19 #include <strstream>
20
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 #include <queue>
25 #include <set>
26 #include <list>
27 #include <functional>
28
29 using namespace std;
30
31 // typedef
32 typedef long long LL;
33 typedef unsigned long long ULL;
34
35 //
36 #define read freopen("in.txt","r",stdin)
37 #define write freopen("out.txt","w",stdout)
38 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
39 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
40 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
41 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
42 #define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
43 #define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
44 #define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
45
46 #define FF(i,a) for(int i=0;i<(a);i+++)
47 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
48 #define Z(a) (a<<1)
49 #define Y(a) (a>>1)
50
51 const double eps = 1e-6;
52 const double INFf = 1e10;
53 const int INFi = 1000000000;
54 const double Pi = acos(-1.0);
55
56 template<class T> inline T sqr(T a){return a*a;}
57 template<class T> inline T TMAX(T x,T y)
58 {
59 if(x>y) return x;
60 return y;
61 }
62 template<class T> inline T TMIN(T x,T y)
63 {
64 if(x<y) return x;
65 return y;
66 }
67 template<class T> inline void SWAP(T &x,T &y)
68 {
69 T t = x;
70 x = y;
71 y = t;
72 }
73 template<class T> inline T MMAX(T x,T y,T z)
74 {
75 return TMAX(TMAX(x,y),z);
76 }
77
78
79 // code begin
80
81 #define MAXN 2000
82 vector<int> G[MAXN];
83 int mt[MAXN];
84 bool used[MAXN];
85 int N,M,K;
86 struct node
87 {
88 int r;
89 int c;
90 int flag;
91 };
92 node mem[MAXN];
93 bool mp[40][40];
94 int TN;
95 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
96
97
98 bool hungarian_MM(int i)
99 {
100 FORj(0,G[i].size(),1)
101 {
102 if(!used[G[i][j]])
103 {
104 used[G[i][j]]=1;
105 if( mt[ G[i][j] ]==-1 || hungarian_MM( mt[G[i][j]] ))
106 {
107 mt[G[i][j]] = i;
108 return true;
109 }
110 }
111 }
112 return false;
113 }
114
115 int main()
116 {
117 read;
118 write;
119 int a,b;
120 while(scanf("%d %d %d",&N,&M,&K)!=-1)
121 {
122 memset(mp,0,sizeof(bool)*40*40);
123 FORi(0,K,1)
124 {
125 scanf("%d %d",&a,&b);
126 mp[b-1][a-1] = 1;
127 }
128 //
129 TN = N*M;
130
131 //
132 FORi(0,N,1)
133 {
134 FORj(0,M,1)
135 {
136 G[i*M+j].clear();
137 if(!mp[i][j])
138 FORk(0,4,1)
139 {
140 int rr = i+dir[k][0];
141 int cc = j+dir[k][1];
142
143 if( rr>=0 && rr<N && cc>=0 && cc<M && !mp[rr][cc] )
144 {
145 G[i*M+j].push_back(rr*M+cc);
146 }
147 }
148 }
149 }
150
151 memset(mt,-1,sizeof(int)*MAXN);
152 int ans = 0;
153 FORi(0,TN,1)
154 {
155 memset(used,0,sizeof(bool)*MAXN);
156 if( hungarian_MM(i))
157 {
158 ans++;
159 }
160 }
161 if( ans == N*M-K)
162 puts("YES");
163 else
164 puts("NO");
165 }
166 return 0;
167 }
posted @ 2011-03-07 16:37  AC2012  阅读(914)  评论(0编辑  收藏  举报