Mediocre String Problem Gym - 101981M

https://cn.vjudge.net/problem/Gym-101981M

思路:网络流;s到1-n容量1,s到s2是k;s2到1-n是1;1-n到1-m是1;m到t是1;

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))<eps)
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int maxn=1e5+9;
const int maxm=4e5+9;

struct E
{
    int to,nex,cap,flow;
}edge[maxm];
int head[maxn];
int tot;
int gap[maxn],dep[maxn],cur[maxn];
void init()
{
    tot=0;
    mem(head,-1);
}
void addedge(int u,int v,int w,int rw=0)
{
    edge[tot].to=v;
    edge[tot].cap=w;
    edge[tot].flow=0;
    edge[tot].nex=head[u];
    head[u]=tot++;

    edge[tot].to=u;
    edge[tot].cap=rw;
    edge[tot].flow=0;
    edge[tot].nex=head[v];
    head[v]=tot++;
}
int Q[maxn];
void bfs(int st,int ed)
{
    mem(dep,-1);
    mem(gap,0);
    gap[0]=1;
    int fr=0,re=0;
    dep[ed]=0;
    Q[re++]=ed;
    while(fr!=re)
    {
        int u=Q[fr++];
        for(int i=head[u];i!=-1;i=edge[i].nex)
        {
            int v=edge[i].to;
            if(dep[v]!=-1) continue;
            Q[re++]=v;
            dep[v]=dep[u]+1;
            gap[dep[v]]++;
        }
    }
}
int S[maxn];
int sap(int st,int ed,int N)
{
    bfs(st,ed);
    memcpy(cur,head,sizeof(head));
    int top=0;
    int u=st;
    int ans=0;
    while(dep[st]<N)
    {
        if(u==ed)
        {
            int Min=INF;
            int inser;
            for(int i=0;i<top;i++)
            {
                if(Min>edge[S[i]].cap-edge[S[i]].flow)
                {
                    Min=edge[S[i]].cap-edge[S[i]].flow;
                    inser=i;
                }
            }
            for(int i=0;i<top;i++)
            {
                edge[S[i]].flow+=Min;
                edge[S[i]^1].flow-=Min;
            }
            ans+=Min;
            top=inser;
            u=edge[S[top]^1].to;
            continue;
        }
        bool flag=false;
        int v;
        for(int i=cur[u];i!=-1;i=edge[i].nex)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=i;
                break;
            }
        }
        if(flag)
        {
            S[top++]=cur[u];
            u=v;
            continue;
        }
        int Min=N;
        for(int i=head[u];i!=-1;i=edge[i].nex)
        {
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=st) u=edge[S[--top]^1].to;
    }
    return ans;
}
int main()
{
    //FAST_IO;
    //freopen("input.txt","r",stdin);

    init();
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
    {
        int num;
        cin>>num;
        for(int j=0;j<num;j++)
        {
            int x;
            cin>>x;
            addedge(i,n+x,1);
        }
    }

    for(int i=1;i<=n;i++)
    {
        addedge(0,i,1);
        addedge(n+m+1,i,1);
    }

    addedge(0,n+m+1,k);

    for(int i=1;i<=m;i++)
    {
        addedge(n+i,n+m+2,1);
    }

    int ans=sap(0,n+m+2,n+m+3);
    cout<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值