Cod sursa(job #1689553)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 14 aprilie 2016 12:42:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#define MAXN 10050

using namespace std;

int n, m, e;
vector<int> graf[MAXN];
bitset<MAXN> viz;
int cuplat[MAXN], parent[MAXN];

void citire()
{
	int x, y;
    scanf("%d %d %d", &n, &m, &e);
    for (int i = 1; i <= e; i++) {
        scanf("%d %d", &x, &y);
        graf[x].push_back(y);
    }
}

int poate(int nod)
{
	if (viz[nod]) return 0;
    viz[nod] = 1;
    for (int i = 0, t = graf[nod].size(); i < t; i++) {
        int y = graf[nod][i];
        if (!parent[y] || poate(parent[y])) {
            parent[y] = nod;
            cuplat[nod] = y;
            return 1;
        }
    }
    return 0;
}

int main()
{
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);

	citire();
    for (int ok = 1; ok; ) {
		ok = 0; viz = 0;
        for (int i = 1; i <= n; i++)
			if (!cuplat[i] && poate(i)) {
				ok = 1;
			}
    }
    int nq = 0;
    for (int i = 1; i <= n; i++)
		if (cuplat[i]) nq++;
    printf("%d\n", nq);
    for (int i = 1; i <= n; i++)
		if (cuplat[i])
			printf("%d %d\n", i, cuplat[i]);


    return 0;
}