Cod sursa(job #1895899)

Utilizator mariakKapros Maria mariak Data 28 februarie 2017 11:57:16
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
#define maxN 10002

FILE *fin  = freopen("cuplaj.in", "r", stdin);
FILE *fout = freopen("cuplaj.out", "w", stdout);

using namespace std;
int N, M, E, ans;
vector <int> G[maxN];
int Left[maxN], Right[maxN];
bitset <maxN> used;

void read(){
    int u, v;
    scanf("%d %d %d", &N, &M, &E);
    for(int i = 0; i < E; ++ i){
        scanf("%d %d", &u, &v);
        G[u].push_back(v);
    }
}

bool Augmenting_Path(int node){
    if(used.test(node)) return 0;
    used.set(node);

    for(int son : G[node]){ // search for exposed(unmatched) son
        if(!Left[son]){
            Left[son] = node;
            Right[node] = son;
            return 1;
        }
    }
    for(int son : G[node]){ // recursive search
        if(Augmenting_Path(Left[son])){ // for exposed vertex using edges int current matching
            Left[son] = node;
            Right[node] = son;
            return 1;
        }
    }
    return 0;
}

void solve(){
    bool change = 1;
    while(change){
        change = false;
        used.reset();
        for(int i = 1; i <= N; ++ i)
            if(!Right[i]) // exposed vertex
                change |= Augmenting_Path(i);
    }
    for(int i = 1; i <= N; ++ i)
        ans += (Right[i] != 0);
}
int main(){
    read();
    solve();
    printf("%d\n", ans);
    for(int i = 1; i <= N; ++ i)
        if(Right[i])
            printf("%d %d\n", i, Right[i]);
    return 0;
}