# [BZOJ1934]&&[Shoi2007] Vote 善意的投票

## 样例输入 SampleInput

9 6
1 1 1 1 1 0 1 0 0
7 9
4 3
1 8
2 7
6 3
3 2

## 样例输出 SampleOutput

3

BZOJ 1934

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf = 0x7fffffff / 27.11;
const int maxn = 305;
const int maxm = 3 * maxn * maxn;
int i, j, t, n, m, l, r, k, z, y, x;
struct edge
{
int to, fl, nx;
} e[maxm];
int cnt, num;
deque <int> q;
inline void ins(int u, int v, int f)
{
e[cnt] = (edge)
{
e[cnt] = (edge)
{
}
inline bool bfs(int s, int t, int tim)
{
int i, u, v;
while (!q.empty()) q.pop_front();
dis[s] = 0; used[s] = tim;
q.push_back(s);
while (!q.empty())
{
u = q.front(); q.pop_front();
for (i = head[u]; i != -1; i = e[i].nx)
{
v = e[i].to;
if (used[v] != tim && e[i].fl > 0)
{
used[v] = tim;
dis[v] = dis[u] + 1;
q.push_back(v);
}
}
}
return (used[t] == tim);
}
int dfs(int u, int t, int flow)
{
int v, d, f = flow;
if (u == t) return flow;
for (int &i = cur[u]; i != -1 && f > 0; i = e[i].nx)
{
v = e[i].to;
if (dis[v] == dis[u] + 1 && e[i].fl > 0)
{
d = dfs(v, t, min(f, e[i].fl));
e[i].fl -= d; e[i ^ 1].fl += d;
f -= d;
}
}
return flow - f;
}
inline void dinic(int s, int t)
{
int ans = 0;
while (bfs(s, t, ++num))
{
ans += dfs(s, t, inf);
}
printf("%d\n", ans);
}
int main()
{
cnt = num = 0;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++)
{
scanf("%d", &x);
if (x == 1) ins(0, i, 1);
else ins(i, n + 1, 1);
}
for (i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
ins(x, y, 1);
ins(y, x, 1);
}
dinic(0, n + 1);
return 0;
}``````