1934: [shoi2007] Vote goodwill voting time limit: 1 sec memory limit: 64 MB
Submit: 1076 solved: 660
[Submit] [Status] Description there are n children in kindergarten who plan to vote to decide whether to take a nap. For them, this issue is not very important, so they decided to carry forward the spirit of humility. Although everyone has their own opinions, in order to take care of the ideas of their friends, they can also vote for the opposite of their own intentions. We define the number of conflicts in a vote as the total number of conflicts between good friends plus the number of people who conflict with their original intentions. Our question is, how should every child vote to minimize the number of conflicts? The first line of input contains only two integers, N and M, which must be 2 ≤ n ≤ 300,1 ≤ m ≤ n (n-1)/2. N represents the total number of friends, and m represents the logarithm of good friends. There are n integers in the second row of the file. The I integer represents the intention of the I-th child. When it is 1, it indicates that the child agrees to sleep. When it is 0, it indicates that the child is opposed to sleep. The following file contains m rows, each of which has two integers I, j. Indicates that J is a good friend. We guarantee that J will not repeat any two pairs of I. Output only needs to output an integer, that is, the possible minimum number of conflicts. Sample input3 3
1 0 0
1 2
1 3
3 2
Sample output1hint
In the first example, all the children vote in favor to obtain the optimal solution.
Source
Day2
Question: Why is this question so powerful! Worship hzwer: Build source S, sink t
Then S-> xpy with the first vote, xpy with the first vote against T
All traffic is 1
Then add a bidirectional edge for a friend relationship (A, B), and the traffic is still 1
The final cut is the number of conflict
(1) number of conflicts not greater than N:
Obviously, even if there is a friend relationship between all xpy, xpy can reach "agree" or "deny" by changing (or not changing) the original decision ", therefore, the number of conflicts between friends is 0, and the number of conflicts not previously determined is not greater than N.
(2) The edges between the "agree" set and the "deny" set are all friends.
(3) A conflict is a code cut between consent and disagreement:
1 const inf=maxlongint; 2 type node=record 3 from,go,next,v:longint; 4 end; 5 var tot,i,j,n,m,maxflow,l,r,s,t,x,y:longint; 6 h,head,q,cur:array[0..1000] of longint; 7 e:array[0..200000] of node; 8 function min(x,y:longint):longint; 9 begin 10 if x<y then exit(x) else exit(y); 11 end; 12 procedure ins(x,y,z:longint); 13 begin 14 inc(tot); 15 e[tot].from:=x;e[tot].go:=y;e[tot].v:=z;e[tot].next:=head[x];head[x]:=tot; 16 end; 17 procedure insert(x,y,z:longint); 18 begin 19 ins(x,y,z);ins(y,x,0); 20 end; 21 function bfs:boolean; 22 var i,x,y:longint; 23 begin 24 fillchar(h,sizeof(h),0); 25 l:=0;r:=1;q[1]:=s;h[s]:=1; 26 while l<r do 27 begin 28 inc(l); 29 x:=q[l]; 30 i:=head[x]; 31 while i<>0 do 32 begin 33 y:=e[i].go; 34 if (e[i].v<>0) and (h[y]=0) then 35 begin 36 h[y]:=h[x]+1; 37 inc(r);q[r]:=y; 38 end; 39 i:=e[i].next; 40 end; 41 end; 42 exit (h[t]<>0); 43 end; 44 function dfs(x,f:longint):longint; 45 var i,y,used,tmp:longint; 46 begin 47 if x=t then exit(f); 48 used:=0; 49 i:=cur[x]; 50 while i<>0 do 51 begin 52 y:=e[i].go; 53 if (h[y]=h[x]+1) and (e[i].v<>0) then 54 begin 55 tmp:=dfs(y,min(e[i].v,f-used)); 56 dec(e[i].v,tmp);if e[i].v<>0 then cur[x]:=i; 57 inc(e[i xor 1].v,tmp); 58 inc(used,tmp); 59 if used=f then exit(f); 60 end; 61 i:=e[i].next; 62 end; 63 if used=0 then h[x]:=-1; 64 exit(used); 65 end; 66 procedure dinic; 67 begin 68 while bfs do 69 begin 70 for i:=s to t do cur[i]:=head[i]; 71 inc(maxflow,dfs(s,inf)); 72 end; 73 end; 74 procedure init; 75 begin 76 tot:=1; 77 readln(n,m); 78 s:=0;t:=n+1; 79 for i:=1 to n do 80 begin 81 read(x); 82 if x=0 then insert(s,i,1) 83 else insert(i,t,1); 84 end; 85 readln; 86 for i:=1 to m do begin readln(x,y);insert(x,y,1);insert(y,x,1);end; 87 end; 88 procedure main; 89 begin 90 maxflow:=0; 91 dinic; 92 writeln(maxflow); 93 end; 94 begin 95 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 96 reset(input);rewrite(output); 97 init; 98 main; 99 close(input);close(output);100 end.
View code