BZOJ 1934 善意的投票(最小割)

把人分成两个集合,一个赞成睡觉,一个反对睡觉。好朋友连一条容量为1的双向边,s向赞成睡觉的连边,反对睡觉的向t连边。

那么这个图的一个割就对应着一个方案。如果割掉s和v的边,就代表v投意见与它自己相反的票,t和v的边同理。割掉u和v的边,就代表了这对好朋友之间意见不同。

这样求出一个割之后,好朋友之间意见不同的边都被割去了。

求出此图的最小割即为答案。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-9
# define MOD 1024523
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=305;
//Code begin...

struct Edge{int p, next, w;}edge[200005];
int head[N], cnt=2, s, t, vis[N];
queue<int>Q;

void add_edge(int u, int v, int w){
    edge[cnt].p=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;
    edge[cnt].p=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++;
}
int bfs(){
    int i, v;
    mem(vis,-1);
    vis[s]=0; Q.push(s);
    while (!Q.empty()) {
        v=Q.front(); Q.pop();
        for (i=head[v]; i; i=edge[i].next) {
            if (edge[i].w>0 && vis[edge[i].p]==-1) {
                vis[edge[i].p]=vis[v] + 1;
                Q.push(edge[i].p);
            }
        }
    }
    return vis[t]!=-1;
}
int dfs(int x, int low){
    int i, a, temp=low;
    if (x==t) return low;
    for (i=head[x]; i; i=edge[i].next) {
        if (edge[i].w>0 && vis[edge[i].p]==vis[x]+1){
            a=dfs(edge[i].p,min(edge[i].w,temp));
            temp-=a; edge[i].w-=a; edge[i^1].w += a;
            if (temp==0) break;
        }
    }
    if (temp==low) vis[x]=-1;
    return low-temp;
}
int main ()
{
    int n, m, flag, u, v;
    scanf("%d%d",&n,&m); s=0; t=n+1;
    FOR(i,1,n) {
        scanf("%d",&flag);
        if (flag) add_edge(s,i,1);
        else add_edge(i,t,1);
    }
    FOR(i,1,m) scanf("%d%d",&u,&v), add_edge(u,v,1), add_edge(v,u,1);
    int sum=0, tmp;
    while (bfs()) while (tmp=dfs(s,INF)) sum+=tmp;
    printf("%d\n",sum);
    return 0;
}
View Code

 

posted @ 2017-05-02 17:29  free-loop  阅读(173)  评论(0编辑  收藏  举报