BZOJ 1934: [Shoi2007]Vote 善意的投票

二次联通门 : BZOJ 1934: [Shoi2007]Vote 善意的投票

 

 

 

 

/*
    BZOJ 1934: [Shoi2007]Vote 善意的投票

    最小割

*/
#include <cstdio>
#include <iostream>
#define rg register

inline void read (int &n) { 
    rg char c = getchar ();
    for (n = 0; !isdigit (c); c = getchar ());
    for (; isdigit (c); n = n * 10 + c - '0', c = getchar ());
}
#define Max 900
int S, T;
#define INF 1e9
namespace net {

    const int MaxE = 200005;
    int _n[MaxE], _v[MaxE], list[Max], _f[MaxE], EC = 1, d[Max], q[Max], tc[Max];
    
    inline void In (int u, int v, int f) { 
        _v[++ EC] = v, _n[EC] = list[u], list[u] = EC, _f[EC] = f;
        _v[++ EC] = u, _n[EC] = list[v], list[v] = EC, _f[EC] = 0;
    }

    bool Bfs () {
        int h = 1, t = 1; q[t] = S; rg int i, n;
        for (i = 0; i <= T; ++ i) d[i] = -1;
        for (d[S] = 0; h <= t; ++ h)
            for (n = q[h], i = list[n]; i; i = _n[i])
                if (_f[i] && d[_v[i]] < 0) {
                    d[_v[i]] = d[n] + 1, q[++ t] = _v[i];
                    if (_v[i] == T) return true; 
                }
        return false;
    }
    int Flowing (int n, int f) {
        if (n == T || f == 0) return f;
        int p, r = 0;
        for (rg int &i = tc[n]; i; i = _n[i])
            if (_f[i] && d[_v[i]] == d[n] + 1) {
                p = Flowing (_v[i], std :: min (_f[i], f));
                if (p > 0) { 
                    r += p, f -= p, _f[i] -= p, _f[i ^ 1] += p;
                    if (f == 0) return r;
                }
            }
        if (r != f) d[n] = -1; return r;
    }

    int Dinic () { 
        int res = 0;
        for (; Bfs (); res += Flowing (S, INF))
            for (rg int i = 0; i <= T; ++ i) tc[i] = list[i];
        return res;
    }
}

int main (int argc, char *argv[]) {
    int x, y, N, M; read (N), read (M); S = 0, T = N + 1; rg int i;
    for (i = 1; i <= N; ++ i) {
        read (x);
        if (x) net :: In (S, i, 1), net :: In (i, T, 0);
        else net :: In (S, i, 0), net :: In (i, T, 1);
    }
    for (i = 1; i <= M; ++ i) read (x), read (y), net :: In (x, y, 1), net :: In (y, x, 1);
    printf ("%d", net :: Dinic ());
    
    return 0;
}

 

posted @ 2018-01-19 20:37  ZlycerQan  阅读(...)  评论(...编辑  收藏