Cod sursa(job #1494874)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 1 octombrie 2015 22:32:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>
#include <vector>
#include <bitset>

#define DIM (1<<17)
using namespace std;

int N, M, K, X, Y, sol;
int Right[DIM/10], Left[DIM/10];

vector<int> Vector[DIM];
bitset<DIM/10> Marked;

bool VertexCover(int nod){

    if(Marked[nod] == 1)
        return 0;
    Marked[nod] = 1;

    for(int i = 0; i < Vector[nod].size(); i ++){
        int vec = Vector[nod][i];

        if(Right[vec] == 0){
            Left [nod] = vec;
            Right[vec] = nod;
            sol ++;
            return 1;
        }
    }

    for(int i = 0; i < Vector[nod].size(); i ++){
        int vec = Vector[nod][i];

        if(VertexCover(Right[vec])){
            Left [nod] = vec;
            Right[vec] = nod;
            return 1;
        }
    }

    return 0;
}

int main(){

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

    scanf("%d %d %d", &N, &M, &K);
    for(int i = 1; i <= K; i ++){
        scanf("%d %d", &X, &Y);
        Vector[X].push_back(Y);
    }

    int ok = 1;
    do {
        ok = 0;

        Marked.reset();

        for(int i = 1; i <= N; i ++)
            if(!Left[i] && VertexCover(i))
                ok = 1;

    } while(ok);

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

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

    fclose(stdin );
    fclose(stdout);

    return 0;
}