Cod sursa(job #1166471)

Utilizator muresan_bogdanMuresan Bogdan muresan_bogdan Data 3 aprilie 2014 16:54:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include<iostream>
#include<fstream>
#include<cstring>
#include<vector>
using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int MAXN = 10005;

int n, m, k, i, x, y, st[MAXN], dr[MAXN], cuplaj;
bool viz[MAXN], ok;
vector<int> g[MAXN];

bool cuplat(int x) {
    vector<int>::iterator it;
    if(!viz[x]) {
        viz[x] = 1;
        for(it = g[x].begin(); it != g[x].end(); it++) {
            int y = *it;
            if(!st[y] || cuplat(st[y])) {
                dr[x] = y;
                st[y] = x;
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    fin >> n >> m >> k;
    for(i = 0; i < k; i++) {
        fin >> x >> y;
        g[x].push_back(y);
    }
    ok = 1;
    while(ok) {
        ok = 0;
        memset(viz, 0, sizeof(viz));
        viz[0] = 1;
        for(i = 1; i <= n; i++) {
            if(!dr[i] && cuplat(i)) {
                cuplaj++;
                ok = 1;
            }
        }
    }
    fout << cuplaj << '\n';
    for(i = 1; i <= n; i++) {
        if(dr[i] != 0) {
            fout << i << ' ' << dr[i] << '\n';
        }
    }
    fin.close();
    fout.close();
    return 0;
}