Cod sursa(job #1290425)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 decembrie 2014 11:15:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.13 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "cuplaj";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;

const int NMAX = 10000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

int N, M, E, sol;
vector<int> V[NMAX];
int A[NMAX];
int B[NMAX];
bitset<NMAX> viz;

int pairUp(int x) {
    if(viz[x])
        return 0;

    viz[x] = 1;

    for(auto y : V[x])
        if(!B[y] || pairUp(B[y])) {
            A[x] = y;
            B[y] = x;
            return 1;
        }

    return 0;
}

int main() {
    int i, ok, x, y;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d%d%d", &N, &M, &E);

    while(E--) {
        scanf("%d%d", &x, &y);
        V[x].push_back(y);
    }

    for(ok = 1; ok;) {
        ok = 0;
        viz = 0;

        for(i = 1; i <= N; i++)
            if(!A[i])
                ok |= pairUp(i);
    }

    for(i = 1; i <= N; i++)
        if(A[i])
            sol++;

    printf("%d\n", sol);

    for(i = 1; i <= N; i++)
        if(A[i])
            printf("%d %d\n", i, A[i]);

    return 0;
}