【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个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以 ...
随机推荐
- 又是一个二模02,不过day2
话说比较简单.除了第三题不会写平衡树啊你妹!!边做边写吧. 机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链接~机智的链 ...
- AWS AutoScaling
origin_from: http://blog.csdn.net/libing_thinking/article/details/48327189 AutoScaling 是 AWS 比较核心的一个 ...
- ubuntu安装到选择位置时闪退
转自:http://tieba.baidu.com/p/3020839207
- spring+quartz报错:Table 'BANKSTEELERP_OLD.QRTZ_TRIGGERS' doesn't exist
spring3.2.8 + quartz2.2.1配置到application.xml中 org.springframework.beans.factory.BeanCreationException ...
- 31.从尾到头输出链表[Print linked list from last to first]
[题目] 输入一个链表的头结点,从尾到头反过来输出每个结点的值. [分析] 这是一道很有意思的面试题.该题以及它的变体经常出现在各大公司的面试.笔试题中. [链表逆置] 看到这道题后,第一反应是从头到 ...
- Java for LeetCode 057 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Heap:Sunscreen(POJ 3614)
晒太阳 题目大意:一堆牛,为了避免晒太阳会灼烧自己,然后他们自己有自己的防晒指数(一个区间),防晒霜可以提高防晒因数SPF,大了不行小了不行,现在有一桶防晒霜,他们提供一定的SPF,但是最多可以提供k ...
- codeforces 485B Valuable Resources 解题报告
题目链接:http://codeforces.com/problemset/problem/485/B 题目意思:需要建造一个正方形的city,这座city的边需要满足平行于 x 轴和 y 轴,而且这 ...
- C++查询网站
1.C++外文api查询 http://www.cplusplus.com/ 2.https://isocpp.org/ 3.Juce学习 http://www.thinksaas.cn/favori ...
- PhpStorm 配置Xdebug
IDE => Xdebug => Apache(XAMPP) => Firefox + easist Xdebug 1>XAMPP停止apache服务;2>在安装目录下找 ...