http://www.lydsy.com/JudgeOnline/problem.php?id=1934

一开始我想到了这是求最小割,但是我认为这题二分图可做,将1的放在左边,0的放在右边,然后朋友连边,如果有冲突就相当于有1条x-y的边,求最小割也就是最大匹配即可。。可是不知道为什么就错了。

#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;
}

后来无奈看了题解,恩,也是差不多。

首先也是二分图,将s连到1的点,点为0连到t,容量均为1(1的点集我设x,0的点集我设为y)

然后连兄弟边,容量均为1(双向)

然后求最小割就是答案

为什么呢。。

因为我们设s就代表了1,t代表了0,而一个割C(s, t)就代表了一个解决冲突的方案,如果某个x集里的点a现在被割分到了t,那么它与s的边一定断掉(代表他自己违背自己),与x集或y集的其它分到s的点的边也一定断掉(代表了它解决冲突的方案)

所以最小割就代表了最优方案。

#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

幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了 照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的 人数。 我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?

Input

第一行只有两个整数n,m,保证有 2≤n≤300,1≤m≤n(n-1)/2。其中n代表总人数,m代表好朋友的对数。文件第二行有n个整数,第i个整数代表第i个小朋友的意愿,当它为1 时表示同意睡觉,当它为0时表示反对睡觉。接下来文件还有m行,每行有两个整数i,j。表示i,j是一对好朋友,我们保证任何两对i,j不会重复。

Output

只需要输出一个整数,即可能的最小冲突数。

Sample Input

3 3
1 0 0
1 2
1 3
3 2

Sample Output

1

HINT

在第一个例子中,所有小朋友都投赞成票就能得到最优解

Source

【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)的更多相关文章

  1. BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割

    1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...

  2. 最小投票BZOJ 1934([Shoi2007]Vote 善意的投票-最小割)

    上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下最小投票 1934: [Shoi2007]Vote 好心的投票 Time Limit: 1 Sec Memory L ...

  3. BZOJ 1934 [Shoi2007]Vote 善意的投票(最小割)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1934 [题目大意] 每个人对于投票都有自己原来的观点:1或者0, 他可以违背自己原来的 ...

  4. bzoj 1934: [Shoi2007]Vote 善意的投票

    #include<cstdio> #include<iostream> #define M 100000 #include<cstring> using names ...

  5. ●BZOJ 1934 [Shoi2007]Vote 善意的投票

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1934 题解: 题目有点迷. S向为1的点连边,为0的点向T连边,在有关系的两个点之间连双向边 ...

  6. 【刷题】BZOJ 1934 [Shoi2007]Vote 善意的投票

    Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...

  7. bzoj 1934: [Shoi2007]Vote 善意的投票 (最小割)

    原来是赞同的连源,原来是反对的连汇,然后是朋友的就连在一起,这样最小割就是割掉违背和谐的吧 type arr=record toward,next,cap:longint; end; const ma ...

  8. 1934: [Shoi2007]Vote 善意的投票

    1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1174  Solved: 723[Submit][S ...

  9. 1934: [Shoi2007]Vote 善意的投票 - BZOJ

    Description幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以 ...

随机推荐

  1. Thinkphp中eq,neq,gt,lt等表达式缩写

    eq 等于  equalneq 不等于gt 大于   greater thanegt 大于等于lt 小于     less thanelt 小于等于like LIKEbetween BETWEENno ...

  2. 73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)

    [本文链接] http://www.cnblogs.com/hellogiser/p/swap-odd-even-bits.html [分析] 假定一个数字是8位数,设为ABCDEFGH ABCDEF ...

  3. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  4. iOS 和 Android 触摸事件传递

    先看文章,写得很好 ios 触摸事件传递 http://www.cnblogs.com/Quains/p/3369132.html 另外一篇 http://blog.csdn.net/yongyinm ...

  5. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. UBUNTU中如何获得root权限

    在终端中输入: sudo passwd root Enter new UNIX password: (在这输入你的密码) Retype new UNIX password: (确定你输入的密码) pa ...

  7. cf584a(水题)

    题意是输出一个能被t整除的n位数... 思路很简单,输出t和n-1个0即可.当然,还需要特判一下t为1,n为10的情况.. 代码如下: #include <bits/stdc++.h> u ...

  8. linux下常见的文件夹含义

    1./bin :获得最小的系统可操作性所需要的命令2./boot :内核和加载内核所需的文件3./dev :终端.磁盘.调制解调器等的设备项4./etc :关键的启动文件和配置文件5./home :用 ...

  9. p235习题3

  10. Redis中常用命令

    连接操作相关的命令 quit:关闭连接(connection) auth:简单密码认证 持久化 save:将数据同步保存到磁盘 bgsave:将数据异步保存到磁盘 lastsave:返回上次成功将数据 ...