3692

1 /*
2 最大独立集+顶点最小覆盖(最大匹配)=顶点数
3
4 问题的本质回归为 求二分图的最大匹配了
5
6 二分图的最大匹配,可以用hungarian algorithm或者最大流算法来求
7 */
8
9 // include file
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <cmath>
14 #include <cctype>
15 #include <ctime>
16
17 #include <iostream>
18 #include <sstream>
19 #include <fstream>
20 #include <iomanip>
21 #include <bitset>
22 #include <strstream>
23
24 #include <algorithm>
25 #include <string>
26 #include <vector>
27 #include <queue>
28 #include <set>
29 #include <list>
30 #include <functional>
31
32 using namespace std;
33
34 // typedef
35 typedef long long LL;
36 typedef unsigned long long ULL;
37
38 //
39 #define read freopen("in.txt","r",stdin)
40 #define write freopen("out.txt","w",stdout)
41 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
42 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
43 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
44 #define FORp(a,b,c) for(int p=(a);p<(b);p+=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 #define MAXN 210
81 int G,B,M;
82 //bool ga[MAXN][MAXN];
83 bool fg[MAXN][MAXN];
84 //vector<int> ga[MAXN];
85
86 int mt[MAXN];
87 bool used[MAXN];
88
89 bool hungarian_MM(int i) //是否能够找到增广路
90 {
91 //FORj(1,B+1,1)
92 FORj(0,ga[i].size(),1)
93 {
94 if( !used[ ga[i][j] ] )
95 {
96 used[ga[i][j]] = 1;
97 if( mt[ga[i][j]]==-1 || hungarian_MM(mt[ga[i][j]]))
98 {
99 mt[ga[i][j]] = i;
100 return true;
101 }
102 }
103 }
104 return false;
105 }
106
107 int main()
108 {
109 read;
110 write;
111 int a,b,ans,cas=1;
112 while(scanf("%d %d %d",&G,&B,&M)!=-1)
113 {
114 if(G+B+M==0) break;
115
116 //memset(ga,0,sizeof(bool)*MAXN*MAXN);
117 FORi(1,G+1,1)
118 ga[i].clear();
119 memset(fg,0,sizeof(bool)*MAXN*MAXN);
120 FORi(0,M,1)
121 {
122 scanf("%d %d",&a,&b);
123 //ga[a][b] = 1;
124 //ga[a].push_back(b);
125 fg[a][b] = 1;
126 }
127 FORi(1,G+1,1)
128 {
129 FORj(1,B+1,1)
130 {
131 if(!fg[i][j])
132 ga[i].push_back(j);
133 }
134 }
135
136 memset(mt,-1,sizeof(int)*MAXN);
137
138 ans = 0;
139 FORi(1,G+1,1)
140 {
141 memset(used,0,sizeof(bool)*MAXN);
142 if(hungarian_MM(i))
143 {
144 ans++;
145 }
146 }
147 printf("Case %d: ",cas++);
148 printf("%d\n",G+B-ans);
149 }
150 return 0;
151 }

posted @ 2011-03-04 14:05  AC2012  阅读(548)  评论(0编辑  收藏  举报