Description
幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。 我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?
Solution
从源点向所有兹瓷的同学连边,从所有不兹瓷的同学向汇点连边,每一对朋友之间连双向边,流量都为 1 1 <script type="math/tex" id="MathJax-Element-12">1</script>,求最小割即可。
Code
/************************************************
* Au: Hany01
* Date: Jul 25th, 2018
* Prob: BZOJ1934&2768 善意的投票/冠军调查
* Email: hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 305, maxm = maxn * maxn << 1;
int n, m, S, T, e = 1, beg[maxn], f[maxm], v[maxm], nex[maxm], Ans, cur[maxn], vis[maxn], lev[maxn];
inline void add(int uu, int vv, int ff) { v[++ e] = vv, f[e] = ff, nex[e] = beg[uu], beg[uu] = e; }
inline int BFS(int S, int T) {
static queue<int> q;
Set(lev, 0), lev[S] = 1, q.push(S);
while (!q.empty()) {
int u = q.front(); q.pop();
for (register int i = beg[u]; i; i = nex[i])
if (f[i] && !lev[v[i]]) lev[v[i]] = lev[u] + 1, q.push(v[i]);
}
return lev[T];
}
int DFS(int u, int T, int flow)
{
if (u == T) return flow;
int res = flow, t;
for (register int& i = cur[u]; i; i = nex[i]) if (f[i] && lev[v[i]] == lev[u] + 1) {
f[i] -= (t = DFS(v[i], T, min(f[i], res))), f[i ^ 1] += t;
if (!(res -= t)) break;
}
return flow - res;
}
int main()
{
#ifdef hany01
freopen("bzoj1934.in", "r", stdin);
freopen("bzoj1934.out", "w", stdout);
#endif
static int uu, vv;
n = read(), m = read(), T = (S = n + 1) + 1;
For(i, 1, n)
if (!read()) add(S, i, 1), add(i, S, 0);
else add(i, T, 1), add(T, i, 0);
while (m --) uu = read(), vv = read(), add(uu, vv, 1), add(vv, uu, 1);
while (BFS(S, T)) Cpy(cur, beg), Ans += DFS(S, T, INF);
printf("%d\n", Ans);
return 0;
}
//西塞山前白鹭飞,桃花流水鳜鱼肥。
// -- 张志和《渔歌子·西塞山前白鹭飞》