bzoj1934

 1 var
 2   ans,u,v,x,tot,kk,i,j,k,l,n,m,ee,s,t:longint;
 3   d,last,next,long,other,e,head,num:array[0..200000]of longint;
 4 
 5 function min(aa,bb:longint):longint;
 6   begin
 7   if aa>bb then exit(bb)
 8   else exit(aa);
 9   end;
10 
11 procedure add(u,v,c:longint);
12   begin
13   inc(kk);
14   other[kk]:=kk+1;
15   e[kk]:=v;
16   next[kk]:=head[u];
17   head[u]:=kk;
18   long[kk]:=c;
19   inc(kk);
20   e[kk]:=u;
21   next[kk]:=head[v];
22   head[v]:=kk;
23   long[kk]:=0;
24   other[kk]:=kk-1;
25   end;
26 
27 function dfs(x,flow:longint):longint;
28   var j,p:longint;
29   begin
30   if x=t then exit(flow);
31   dfs:=0; j:=last[x];
32   while j<>0 do
33     begin
34     if (long[j]>0)and(d[x]=d[e[j]]+1) then
35       begin
36       last[x]:=j;
37       p:=dfs(e[j],min(flow-dfs,long[j]));
38       dec(long[j],p); inc(long[other[j]],p);
39       dfs:=dfs+p;
40       if dfs=flow then exit;
41       end;
42     j:=next[j];
43     end;
44   if d[s]>tot then exit;
45   dec(num[d[x]]);
46   if num[d[x]]=0 then d[s]:=tot;
47   inc(d[x]);
48   last[x]:=head[x];
49   inc(num[d[x]]);
50   end;
51 
52 begin
53   readln(n,m);
54   s:=0;t:=n+1;
55   for i:=1 to n do
56     begin
57     read(x);
58     if x=1 then
59       add(s,i,1)
60     else
61       add(i,t,1);
62     end;
63   for i:=1 to m do
64     begin
65     readln(u,v);
66     add(u,v,1);
67     add(v,u,1);
68     end;
69   tot:=t+1;
70   num[0]:=tot;
71   for i:=s to t do
72     last[i]:=head[i];
73   while d[s]<=tot do
74     ans:=ans+dfs(s,maxlongint);
75   writeln(ans);
76   end.

 



posted @ 2013-03-01 15:55  lbz007  阅读(103)  评论(0编辑  收藏
Live2D