[网络流] bzoj1934: [Shoi2007]Vote 善意的投票

版权声明:本文为QQQQqtt原创,但是我猜没有人转载oAo https://blog.csdn.net/qq_36038511/article/details/80300178

bzoj1934: [Shoi2007]Vote 善意的投票 https://www.lydsy.com/JudgeOnline/problem.php?id=1934

网络流
考虑最小割
用最小割做决策
想睡觉的连源点 不想睡觉的连汇点 容量为1
朋友之间连双向边 容量为1
然后直接跑最小割就ok
这个证明很简单画一个图跑一个样例就很好理解了
大水题真的……
都不好意思刷掉 刷掉了就少一道水题了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[400000];
int last[310],len;
int dep[310],list[310];
int st,ed;
void build(int x,int y,int c)
{
    len++;int k1=len;
    a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
    len++;int k2=len;
    a[len].x=y;a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
bool bfs()
{
    memset(dep,0,sizeof(dep));
    dep[st]=1;list[1]=st;
    int head=1,tail=1;
    while (head<=tail)
    {
        int x=list[head];
        for (int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if (dep[y]==0&&a[k].c>0)
            {
                dep[y]=dep[x]+1;
                list[++tail]=y;
            }
        }
        head++;
    }
    if (dep[ed]==0) return 0;
    else return 1;
}
int dfs(int x,int flow)
{
    if (x==ed) return flow;
    int tot=0;
    for (int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if (dep[y]==dep[x]+1&&a[k].c>0&&flow>tot)
        {
            int sum=dfs(y,min(a[k].c,flow-tot));
            tot+=sum;a[k].c-=tot;a[a[k].other].c+=tot;
        }
    }
    if (tot==0) dep[x]=0;
    return tot;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    st=n+1;ed=st+1;
    for (int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        if (x==1) build(st,i,1);
        else build(i,ed,1);
    }
    for (int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        build(x,y,1);
        build(y,x,1);
    }
    int ans=0;
    while (bfs())
    {
        ans+=dfs(st,999999999);
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36038511/article/details/80300178