3281

1 // include file
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cmath>
6 #include <cctype>
7 #include <ctime>
8
9 #include <iostream>
10 #include <sstream>
11 #include <fstream>
12 #include <iomanip>
13 #include <bitset>
14
15 #include <algorithm>
16 #include <string>
17 #include <vector>
18 #include <queue>
19 #include <set>
20 #include <list>
21 #include <functional>
22
23 using namespace std;
24
25 // typedef
26 typedef long long LL;
27 typedef unsigned long long ULL;
28 typedef __int64 Bint;
29
30 //
31 #define read freopen("in.txt","r",stdin)
32 #define write freopen("out.txt","w",stdout)
33 #define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
34 #define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
35 #define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
36 #define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
37 #define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
38 #define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
39 #define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)
40
41 #define FF(i,a) for(int i=0;i<(a);i++)
42 #define FFD(i,a) for(int i=(a)-1;i>=0;i--)
43
44 #define Z(a) (a<<1)
45 #define Y(a) (a>>1)
46
47 const double eps = 1e-6;
48 const double INFf = 1e10;
49 const int INFi = 1000000000;
50 const double Pi = acos(-1.0);
51
52 template<class T> inline T sqr(T a){return a*a;}
53 template<class T> inline T TMAX(T x,T y)
54 {
55 if(x>y) return x;
56 return y;
57 }
58 template<class T> inline T TMIN(T x,T y)
59 {
60 if(x<y) return x;
61 return y;
62 }
63 template<class T> inline void SWAP(T &x,T &y)
64 {
65 T t = x;
66 x = y;
67 y = t;
68 }
69 template<class T> inline T MMAX(T x,T y,T z)
70 {
71 return TMAX(TMAX(x,y),z);
72 }
73
74
75 // code begin
76
77 #define MAXN 404
78
79 class MaxFlow_ISAP
80 {
81 private:
82 int N;
83 int source,sink;
84 bool G[MAXN][MAXN];
85
86 int que[MAXN];
87 int dis[MAXN]; //距离
88 int lay[MAXN]; //层个数
89 int pos[MAXN]; //当前弧
90 int fat[MAXN]; //
91
92 public:
93
94 MaxFlow_ISAP(){};
95 MaxFlow_ISAP(int vnum,int so,int sk)
96 {
97 N = vnum;
98 source = so;
99 sink = sk;
100 }
101 void Init()
102 {
103 memset(G,0,sizeof(G));
104 }
105 void Set(int Nt,int sourcet,int sinkt)
106 {
107 N = Nt;
108 source = sourcet;
109 sink = sinkt;
110 }
111 void Add_edge(int a,int b,int c)
112 {
113 G[a][b] = c;
114 }
115 void Print()
116 {
117 FORi(1,N+1,1)
118 {
119 FORj(1,N+1,1)
120 {
121 printf("%d ",G[i][j]);
122 }
123 printf("\n");
124 }
125 printf("\n");
126 }
127 public:
128 void BFS()
129 {
130 int head(0),tail(0);
131 fill(dis,dis+N+1,N);
132 fill(lay,lay+N+1,0);
133 FORi(1,N+1,1)
134 {
135 lay[dis[i]]++;
136 }
137
138 //
139 lay[dis[sink]]--;
140 dis[sink] = 0;
141 lay[dis[sink]]++;
142
143 que[++tail] = sink;
144 while(head!=tail)
145 {
146 int cur = que[++head];
147 FORi(1,N+1,1)
148 {
149 if(G[i][cur]==0 || dis[i]<N) continue;
150 lay[dis[i]]--;
151 dis[i] = dis[cur]+1;
152 lay[dis[i]]++;
153 que[++tail] = i;
154 }
155 }
156 }
157
158 int Augment()
159 {
160 int mind = INFi;
161 int cur = sink;
162 while(cur!=source)
163 {
164 if(G[fat[cur]][cur]<mind)
165 mind = G[fat[cur]][cur];
166 cur = fat[cur];
167 }
168
169 cur = sink;
170 while(cur!=source)
171 {
172 G[fat[cur]][cur] -= mind;
173 G[cur][fat[cur]] += mind;
174 cur = fat[cur];
175 }
176
177 return mind;
178 }
179
180 int Relabel(int &cur)
181 {
182 int tmp,mind(N-1);
183
184 FORi(1,N+1,1)
185 {
186 if(G[cur][i]>0 && dis[i]<mind)
187 mind = dis[i];
188 }
189
190 tmp = dis[cur];
191
192 lay[dis[cur]]--;
193 dis[cur] = 1+mind;
194 lay[dis[cur]]++;
195
196 if(cur!=source)
197 {
198 cur = fat[cur];
199 }
200
201 return lay[tmp];
202 }
203
204 int Maxflow()
205 {
206 int flow(0);
207
208 BFS();
209
210 fill(pos,pos+N+1,1);
211
212 fat[source] = -1;
213 int st = source;
214 while(dis[source]<N)
215 {
216 int ds;
217 for(ds=pos[st];ds<=N;ds++)
218 {
219 if(G[st][ds]>0&&dis[st]==dis[ds]+1)
220 {
221 break;
222 }
223 }
224
225 if(ds<=N)
226 {
227 pos[st] = ds;
228 fat[ds] = st;
229 st = ds;
230 if(st==sink)
231 {
232 flow += Augment();
233 st = source;
234 }
235 }
236 else
237 {
238 pos[st] = 1;
239 if( Relabel(st)==0)
240 break;
241 }
242 }
243 return flow;
244 }
245 };
246 int N,F,D;
247 int main()
248 {
249 read;
250 write;
251 int fs,ds,source,sink,a;
252 MaxFlow_ISAP g;
253 while(scanf("%d %d %d",&N,&F,&D)!=-1)
254 {
255 g.Init();
256 source = F+D+N*2+1;
257 sink = F+D+2*N+2;
258 g.Set(F+D+N*2+2,source,sink);
259
260 FORi(1,N+1,1)
261 {
262 scanf("%d %d",&fs,&ds);
263 FORj(0,fs,1)
264 {
265 scanf("%d",&a);
266 g.Add_edge(a,F+i,1);
267 }
268 FORj(0,ds,1)
269 {
270 scanf("%d",&a);
271 g.Add_edge(F+N+i,a+F+2*N,1);
272 }
273 }
274
275 FORi(1,F+1,1)
276 {
277 g.Add_edge(source,i,1);
278 }
279 FORi(1,D+1,1)
280 {
281 g.Add_edge(i+F+2*N,sink,1);
282 }
283 FORi(1,N+1,1)
284 {
285 g.Add_edge(F+i,F+N+i,1);
286 }
287
288 printf("%d\n",g.Maxflow());
289 }
290 return 0;
291 }
posted @ 2011-03-16 21:41  AC2012  阅读(341)  评论(0编辑  收藏  举报