Cod sursa(job #1465842)

Utilizator RazvanR104Razvan-Andrei Ciocoiu RazvanR104 Data 28 iulie 2015 03:32:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 10010;

int N, M, E;
int Left[NMAX], Right[NMAX];
bool used[NMAX];
vector<int> G[NMAX];

bool PairUp(int node) {
    if (used[node])
        return 0;
    used[node] = 1;

    for (int i = 0; i < int(G[node].size()); ++i) {
        int _node = G[node][i];
        if (Right[_node]) continue;
        Left[node] = _node;
        Right[_node] = node;
        return 1;
    }

    for (int i = 0; i < int(G[node].size()); ++i) {
        int _node = G[node][i];
        if (PairUp(Right[_node]) == false) continue;
        Left[node] = _node;
        Right[_node] = node;
        return 1;
    }

    return 0;
}

int main() {
    ios_base::sync_with_stdio(false);
	ifstream in("cuplaj.in");
	ofstream out("cuplaj.out");

    int i, x, y;

    in >> N >> M >> E;

    while (E--) {
        in >> x >> y;
        G[x].push_back(y);
    }

    bool changed = 1;
    while (changed) {
        changed = 0;
        memset(used, 0, sizeof(used));
        for (i = 1; i <= N; ++i) {
            if (Left[i]) continue;
            changed |= PairUp(i);
        }
    }

    int number = 0;
    for (i = 1; i <= N; ++i)
        number += Left[i] > 0;

    out << number << '\n';
    for (i = 1; i <= N; ++i) {
        if (Left[i] == 0) continue;
        out << i << ' ' << Left[i] << '\n';
    }

	in.close();
	out.close();
	return 0;
}