[Bzoj] 1934: [shoi2007] Vote Voting in good faith (network stream/-bipartite graph matching)

Source: Internet
Author: User
Tags cmath

 

At first, I thought this was the minimum cut, but I thought that the bipartite graph could be used to put 1 on the left, 0 on the right, and then a friend connected to the edge, if a conflict exists, it is equivalent to an x-y edge. You can find the minimum cut, that is, the maximum match .. But I don't know why.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=310, M=N*N/2, oo=~0u>>1;int ihead[N], cnt=1, n, m, ly[N], x[N], cx, y[N], cy, vis[N];struct ED { int from, to, cap, next; } e[M];inline void add(const int &u, const int &v) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;}const bool ifind(const int &x) {vis[x]=1; int y;for(int i=ihead[x]; i; i=e[i].next) if(!vis[y=e[i].to]) {vis[y]=1;if(!ly[y] || ifind(ly[y])) {ly[y]=x;return true;}}return false;}int main() {read(n); read(m);int t, ans=0;for1(i, 1, n) {read(t);if(t) x[++cx]=i;else y[++cy]=i;}rep(i, m) add(getint(), getint());for1(i, 1, cx) {CC(vis, 0);if(ifind(x[i])) ++ans;}print(ans);return 0;}

 

Later, I had no choice but to understand the problem. Well, it was similar.

First, it is also a bipartite graph. It connects s to the point of 1, and the point is 0 to T. The capacity is 1 (I set the point set of 1 to X, and the point set of 0 to Y)

Then connect to the sibling side. The capacity is 1 (bidirectional)

Then the minimum cut is the answer.

Why ..

Because s represents 1, t represents 0, and a cut C (S, T) represents a solution to the conflict, if point A in a certain X set is cut to T, its side with S must be broken (representing that he violates himself ), the edge of the vertex to which X or Y is divided into S must also be broken (representing the solution for resolving the conflict)

Therefore, the minimum cut represents the optimal solution.

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=320, M=N*N*2, oo=~0u>>1;int ihead[N], cnt=1, cur[N], gap[N], d[N], p[N], n, m;struct ED { int from, to, cap, next; } e[M];inline void add(const int &u, const int &v, const int &w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=w;e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].from=v; e[cnt].cap=0;}int isap(const int &s, const int &t, const int &n) {for1(i, 0, n) cur[i]=ihead[i];int ret=0, i, f, u=s;gap[0]=n;while(d[s]<n) {for(i=cur[u]; i; i=e[i].next) if(e[i].cap && d[u]==d[e[i].to]+1) break;if(i) {p[e[i].to]=cur[u]=i; u=e[i].to;if(u==t) {for(f=oo; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;ret+=f;}}else {if(! (--gap[d[u]]) ) break;d[u]=n; cur[u]=ihead[u];for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].to]+1) d[u]=d[e[i].to]+1;++gap[d[u]];if(u!=s) u=e[p[u]].from;}}return ret;}int main() {read(n); read(m);int s=n+10, t=s+1, tp;for1(i, 1, n) {read(tp);if(tp) add(s, i, 1);else add(i, t, 1);}for1(i, 1, m) {int t1=getint(), t2=getint();add(t1, t2, 1);add(t2, t1, 1);}print(isap(s, t, t+1));return 0;}

 

 

 

 

 

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

 

[Bzoj] 1934: [shoi2007] Vote Voting in good faith (network stream/-bipartite graph matching)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.