Cod sursa(job #1969934)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 18 aprilie 2017 18:55:53
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>

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

const int NMax = 10003;

int n,m,k,x,y;
int viz[NMax],vL[NMax],vR[NMax];
vector<int> G[NMax];

bool cuplaj(int nod){
    if(viz[nod])
        return 0;
    viz[nod] = 1;

    for(int i = 0; i < G[nod].size(); ++i){
        if(!vL[G[nod][i]]){
            vR[nod] = G[nod][i];
            vL[G[nod][i]] = nod;
            return 1;///cuplez
        }
    }
    for(int i = 0; i < G[nod].size(); ++i){
        if(cuplaj(vL[G[nod][i]])){///daca merge cuplat cu altceva
            vR[nod] = G[nod][i];
            vL[G[nod][i]] = nod;
            return 1;///cuplez
        }
    }
    return 0;
}
int main()
{
    f >> n >> m >> k;
    for(int i = 1; i <= k; ++i){
        f >> x >> y;
        G[x].push_back(y);
    }
    int ok = 1;
    while(ok){
        ok = 0;
        memset(viz,0,sizeof(viz));
        for(int i = 1; i <= n; ++i){
            if(!vR[i]){
                if(cuplaj(i)){
                    ok = 1;
                }
            }
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        if(vR[i])
            ans++;
    }
    g << ans << '\n';
    for(int i = 1; i <= n; ++i){
        if(vR[i])
        g << i << ' ' << vR[i] << '\n';
    }
    return 0;
}