【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)
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
Input
Output
Sample Input
1 0 0
1 2
1 3
3 2
Sample Output
HINT
在第一个例子中,所有小朋友都投赞成票就能得到最优解
Source
【BZOJ】1934: [Shoi2007]Vote 善意的投票(网络流/-二分图匹配)的更多相关文章
- BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- 最小投票BZOJ 1934([Shoi2007]Vote 善意的投票-最小割)
上班之余抽点时间出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下最小投票 1934: [Shoi2007]Vote 好心的投票 Time Limit: 1 Sec Memory L ...
- BZOJ 1934 [Shoi2007]Vote 善意的投票(最小割)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1934 [题目大意] 每个人对于投票都有自己原来的观点:1或者0, 他可以违背自己原来的 ...
- bzoj 1934: [Shoi2007]Vote 善意的投票
#include<cstdio> #include<iostream> #define M 100000 #include<cstring> using names ...
- ●BZOJ 1934 [Shoi2007]Vote 善意的投票
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1934 题解: 题目有点迷. S向为1的点连边,为0的点向T连边,在有关系的两个点之间连双向边 ...
- 【刷题】BZOJ 1934 [Shoi2007]Vote 善意的投票
Description 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可 ...
- bzoj 1934: [Shoi2007]Vote 善意的投票 (最小割)
原来是赞同的连源,原来是反对的连汇,然后是朋友的就连在一起,这样最小割就是割掉违背和谐的吧 type arr=record toward,next,cap:longint; end; const ma ...
- 1934: [Shoi2007]Vote 善意的投票
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1174 Solved: 723[Submit][S ...
- 1934: [Shoi2007]Vote 善意的投票 - BZOJ
Description幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以 ...
随机推荐
- HDOJ 2544
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- win7系统扩展双屏幕时,如何在两个屏幕下都显示任务栏
扩展屏幕下都显示任务栏!!! win7系统本身无法设置该功能(目前我是不知道) 但可以下载第三方软件来解决该问题. 第一步:Dual Monitor Taskbar 下载软件 下载链接:http:// ...
- java前三本基础知识总结
基础软件:1:JDK,JRE,JVM(一些参数和作用),GC(机制和算法),Class,Loader(机种作用,加载顺序) 2:环境搭建:JAVA_HOME,path,class 语言基础:引用类型: ...
- Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...
- ubuntu apt-get update 失败解决
在执行 sudo apt-get update 之后 会出现如下错误 这是要检测网络是否有问题 因为我之前只使用nfs挂载的时候,在虚拟机 编辑-> 虚拟网络编辑器里面->更改设置里面 ...
- python提取百万数据到csv文件
转自:http://www.2cto.com/kf/201311/258112.html 今天有需求,需要把系统所有用户注册的id和邮箱等信息导出来提供给他们,在mysql里面count了下,大概有3 ...
- 如何让你的scrapy爬虫不再被ban
前面用scrapy编写爬虫抓取了自己博客的内容并保存成json格式的数据(scrapy爬虫成长日记之创建工程-抽取数据-保存为json格式的数据)和写入数据库(scrapy爬虫成长日记之将抓取内容写入 ...
- 利用WinHEX,重构狂牛加密视频1.0.0.1【只适合RIFF(AVI)】
幸亏是视频部分没有进行加密 1.用 WinHEX 打开狂牛加密视频, 查找 [RIFF] 字符串 2.光标放在 RIFF的 [R]上面, 按 CTRL+SHIFT+END 3.把选择的块写入新文件 H ...
- 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 ...
- Java for LeetCode 151 Reverse Words in a String
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...