Cod sursa(job #1214192)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 29 iulie 2014 19:24:45
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.02 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, r, _pair[MAXN], match[MAXN];
Graph G;
bitset <MAXN> Used;

inline 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() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    fin >> n >> m >> r;
    for(int i = 1 ; i <= r ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    int ans = 0;
    for(bool change = true ; change ; ) {
        change = false;
        Used.reset();
        for(int i = 1 ; i <= n ; ++ i)
            if(!match[i] && pairUp(i)) {
                ++ ans;
                change = true;
            }
    }
    fout << ans << '\n';
    for(int i = 1 ; i <= n ; ++ i)
        if(match[i])
            fout << i << ' ' << match[i] << '\n';
    fin.close();
    fout.close();
    return 0;
}