Cod sursa(job #2244914)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 24 septembrie 2018 09:57:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
#define Nmax 10002
using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n,m,c,st[2*Nmax],dr[2*Nmax],a,b,nr;
bool uz[Nmax];
vector<int> v[2*Nmax];

bool cup(int nod){
    uz[nod] = 1;
    for (auto it : v[nod]){
        if (!dr[it]){
            dr[it] = nod;
            st[nod] = it;
            return 1;
        }
    }
    for (auto it : v[nod]){
        if (!uz[dr[it]] && cup(dr[it])){
            dr[it] = nod;
            st[nod] = it;
            return 1;
        }
    }

    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    f >> n >> m >> c;
    for (int i=1;i<=c;i++){
        f >> a >> b;
        v[a].push_back(b+Nmax);
        v[b+Nmax].push_back(a);
    }

    int ok = 1;
    while(ok){
        ok = 0;
        memset(uz,0,sizeof(uz));
        for (int i=1;i<=n;i++)
            if (!st[i] && !uz[i]) ok |= cup(i);
    }

    for (int i=1;i<=n;i++) if (st[i]) nr++;

    g << nr << '\n';
    for (int i=1;i<=n;i++) if (st[i]) g << i << ' ' << st[i] - Nmax << '\n';

    return 0;
}