Cod sursa(job #1126679)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 27 februarie 2014 08:56:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "cuplaj.in";
const char outfile[] = "cuplaj.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, M, E, match[MAXN], _pair[MAXN];
Graph G;
bitset <MAXN> Used;
int maxMatching;

bool pairUp(int Node) {
    if(Used[Node])
        return false;
    Used[Node] = true;
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
        if(!_pair[*it] || pairUp(_pair[*it])) {
            _pair[*it] = Node;
            match[Node] = *it;
            return true;
        }
    return false;
}

int main() {
    fin >> N >> M >> E;
    for(int i = 1 ; i <= E ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    for(bool change = true ; change ; ) {
        change = false;
        Used.reset();
        for(int i = 1 ; i <= N ; ++ i)
            if(!match[i])
                if(pairUp(i)) {
                    change = true;
                    ++ maxMatching;
                }
    }
    fout << maxMatching << '\n';
    for(int i = 1 ; i <= N ; ++ i)
        if(match[i])
            fout << i << ' ' << match[i] << '\n';
    fin.close();
    fout.close();
    return 0;
}