Storage of Bipartite Graphs Similar to Adjacent Matrix

Example: poj1274
This problem only gives 10,000 KB of memory, according to the previous vector storage explosion (but I did not believe in evil at first, when the result was crazy punishment), so associate for a moment, found that the dichotomy is also a graph, that can use the expanded adjacency matrix to save. So I searched it, and it was true that:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#define M 507
#define inf 0x3f3f3f
using namespace std;
int cap[M][M],pre[M],c[M],flow[M][M];
int n,m;
 
int EK(int s,int t){
    memset(flow,0,sizeof(flow));
    int sum=0;
    queue<int>q;
    while(1){
        memset(c,0,sizeof(c));
        c[s]=inf;
        q.push(s);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=0;i<=t;i++)
                if(!c[i]&&cap[u][i]>flow[u][i]){
                    pre[i]=u;
                    q.push(i);
                    c[i]=min(c[u],cap[u][i]-flow[u][i]);
                }
        }
        if(!c[t])break;
        for(int i=t;i!=s;i=pre[i]){
            flow[pre[i]][i]+=c[t];
            flow[i][pre[i]]-=c[t];
        }
        sum+=c[t];
    }
    return sum;
}
 
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(cap,0,sizeof(cap));
        memset(pre,0,sizeof(pre));
        int s=0;
        int t=n+m+1;
        int num,a;
        for(int i=1;i<=n;i++){
            scanf("%d",&num);
            cap[s][i]=1;
            while(num--){
                scanf("%d",&a);
                cap[i][a+n]=1;
            }
            for(int i=1;i<=m;i++)
                cap[i+n][t]=1;
        }
        int max=EK(s,t);
        printf("%d\n",max);
    }
    return 0;
}

The judgment process is not much different from the ordinary dinic algorithm. (I can't understand it now anyway)

Posted by tecate1 on Mon, 07 Oct 2019 08:31:48 -0700