Cod sursa(job #2642789)

Utilizator OvidRata Ovidiu Ovid Data 17 august 2020 11:48:08
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 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;
map<pii, int> c, r;
vector<int> g[40010];
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[mp(s, i)]<c[mp(s, i)]) ){
             int pl=dfs(i, min(add, c[mp(s, i)]-r[mp(s, i)]));
             r[mp(s, i)]+=pl; r[mp(i, s)]-=pl;
             if(pl>0){
                    v[s]=false;
                return pl;
             }
        }
    }
    v[s]=false;
    return 0;
}




int32_t main(){
INIT
cin>>n>>m>>e;

src=n+m+1, t=n+m+2;
for(int i=1; i<=n; i++){
    g[src].pb(i); c[mp(src, i)]=1;
}
for(int i=1; i<=m; i++){
    g[n+i].pb(t); c[mp(i+n, t)]=1;
}

for(int i=1; i<=e; i++){
        int u, v; cin>>u>>v;
        g[u].pb(n+v); g[n+v].pb(u); c[mp(u, n+v)]=1;
}
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[mp(i, j+n)]==1){
            cout<<i<<" "<<j<<"\n";
        }
    }
}




return 0;
}