Maximum flow: The way to build the map is metaphysics.
The Dinic is O (n2m). #include <cstdio> #include <cstring> #include <cctype> #include <algorithm>using namespace std; #define REP (I,s,t) for (int. i=s;i<=t;i++) #define DWN (i,s,t) for (int i=s;i>=t;i--) #define CLR (x,c) memset (X,c, sizeof (x)) int read () {int X=0;char c=getchar (); while (!isdigit (c)) C=getchar (); while (IsDigit (c)) x=x*10+c-' 0 ', C=getchar (); return x;} const int NMAX=305;CONST int MAXN=200000;CONST int inf=0x7f7f7f7f;struct edge{int to,cap;edge *next,*rev;}; Edge es[maxn],*pt=es,*head[nmax];void Add (int u,int v,int d) {pt->to=v;pt->cap=d;pt->next=head[u];head[u]= pt++; pt->to=u;pt->cap=0;pt->next=head[v];head[v]=pt++; Head[u]->rev=head[v];head[v]->rev=head[u];} Edge *cur[nmax],*p[nmax];int cnt[nmax],h[nmax];int maxflow (int s,int t,int n) {clr (cnt,0); cnt[0]=n;clr (h,0); int Flow=0,a=inf,x=s;edge *e; while (H[s]<n) {for (E=cur[x];e;e=e->next) if (e->cap>0&&h[x]==h[e->to]+1) break; if (e) {a=min (a,e->cap);p [e->to]=cur[x]=e;x=e->to; if (x==t) {while (x!=s) p[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to; Flow+=a;a=inf; }}else{if (!--Cnt[h[x]]) break; H[x]=n; for (E=head[x];e;e=e->next) if (e->cap>0&&h[x]>h[e->to]+1) h[x]=h[e->to]+1,cur[x]=e; cnt[h[x]]++; if (x!=s) x=p[x]->rev->to; }} return flow;} int main () {int n=read (), M=read (), u,v,s=0,t=n+1; Rep (i,1,n) {u=read (); (!u)? Add (i,t,1): Add (s,i,1); } Rep (I,1,m) U=read (), V=read (), add (u,v,1), add (v,u,1); printf ("%d\n", Maxflow (s,t,t+1)); return 0;}
1934: [Shoi2007]vote Goodwill Poll time limit:1 Sec Memory limit:64 MB
submit:1888 solved:1169
[Submit] [Status] [Discuss] Description Kindergarten There are N children planning to vote to decide whether to sleep or nap. To them, the problem is not very important, so they decided to carry forward the spirit of humility. Although everyone has their own ideas, but in order to take care of their friends of the idea, they can also vote and their own will be contrary to the vote. We define the number of conflicts in a poll as the total number of conflicts between good friends plus the number of people who have clashed with their own intentions. Our question is, how should each child vote in order to minimize the number of conflicts? The first line of input is only two integers n,m, guaranteed to have 2≤n≤300,1≤m≤n (n-1)/2. where n represents the total number of people, M is the logarithm of good friends. The second line of the file has n integers, and the first integer represents the will of the child I, and when it is 1, it expresses its consent to sleep, while it is 0 when it stands against sleep. The next file also has m lines, each with two integer i,j. Indicates that I,j is a good friend, and we guarantee that no two pairs of i,j will repeat. Output requires only one integer, which is the smallest possible number of collisions. Sample Input3 3
1 0 0
1 2
1 3
3 2
Sample Output1HINT
In the first example, all the children voted for the best solution.
Source
Day2
[Submit] [Status] [Discuss]
bzoj1934: [Shoi2007]vote vote of goodwill