Cod sursa(job #2642787)

Utilizator OvidRata Ovidiu Ovid Data 17 august 2020 11:43:09
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("cuplaj.in"); ofstream fout("cuplaj.out");
#define cin fin
#define cout fout

int n, m, k, e, src, t;
vector<int> g[10010];
vector<vector<int>> r, c;
bool v[40010];
int flow=0;

int dfs(int s, int add){
    if(s==t){flow+=add; return add;}
    v[s]=true;

    for(int i:g[s]){
        if( (!v[i]) && (r[s][i]<c[s][i]) ){
             int pl=dfs(i, min(add, c[s][i]-r[s][i]));
             r[s][i]+=pl; r[i][s]-=pl;
             if(pl>0){
                    v[s]=false;
                return pl;
             }
        }
    }
    v[s]=false;
    return 0;
}




int32_t main(){
INIT
cin>>n>>m>>e;
r.assign(n+m+5, vector<int>(n+m+5, 0) );
c.assign(n+m+5, vector<int>(n+m+5, 1) );
src=n+m+1, t=n+m+2;
for(int i=1; i<=n; i++){
    g[src].pb(i);
}
for(int i=1; i<=m; i++){
    g[n+i].pb(t);
}

for(int i=1; i<=e; i++){
        int u, v; cin>>u>>v;
        g[u].pb(n+v); g[n+v].pb(u);  c[v+n][u]=0;
}
while(true){
int f0=flow;
dfs(src, 1000000000);
if(flow==f0){break;}
}



cout<<flow<<"\n";

for(int i=1; i<=n; i++){
    for(int j=1; j<=m; j++){
        if(r[i][j+n]==1){
            cout<<i<<" "<<j<<"\n";
        }
    }
}




return 0;
}