【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个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以 ...
 
随机推荐
- 解决android:theme="@android:style/Theme.NoDisplay" 加入这句话后程序不能运行
			
原因: 原来用的是ActionBarActivity,继承自 ActionBarActivity的类必须指定固定的集中Theme风格,而这些 Theme 风格是需要导入V7中的 appcompat L ...
 - JAVA 中BIO,NIO,AIO的理解
			
[转自]http://qindongliang.iteye.com/blog/2018539 ?????????????????????在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解 ...
 - http协议学习系列
			
深入理解HTTP协议(转) http://www.blogjava.net/zjusuyong/articles/304788.html http协议学习系列 1. 基础概念篇 1.1 介绍 H ...
 - Counting Bits
			
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
 - PYTHON实现HTTP摘要认证(DIGEST AUTHENTICATION)
			
参考: http://blog.csdn.net/kiwi_coder/article/details/28677651 http://blog.csdn.net/gl1987807/article/ ...
 - codeforces  B. Color the Fence  解题报告
			
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
 - HDU 1521 排列组合 指数型母函数
			
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
 - CodeForces - 427A (警察和罪犯 思维题)
			
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
 - mysql中的sql总结
			
>>>>>增加字段 ALTER TABLE pj_account ADD COLUMN test INT(11) NOT NULL DEFAULT 1 COMME ...
 - MySQL监控
			
http://blog.csdn.net/zreodown/article/details/8158469