Cod sursa(job #2155004)

Utilizator horiainfoTurcuman Horia horiainfo Data 7 martie 2018 15:09:59
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>

#define N 10002
using namespace std;

bool u[N];
int r[N], l[N];
vector<int> G[N];

bool cuple(int node){
    if(u[node]) return 0;
    u[node] = 1;
    for(auto next : G[node])
        if(!r[next]) {r[next] = node; l[node] = next; return 1;}
    for(auto next : G[node])
        if(cuple(r[next])) {r[next] = node; l[node] = next; return 1;}
    return 0;
}

int main(){
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    int n, m, e;
    scanf("%d%d%d", &n, &m, &e);
    for(int i = 1, x, y; i <= e; i ++){
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
    }
    bool ok = 1;
    while(ok){
        ok = 0;
        memset(u, 0, sizeof(u));
        for(int i = 1; i <= n; i ++)
            if(!u[i] && !l[i]) ok |= cuple(i);
    }
    int nr = 0;
    for(int i = 1; i <= n; i ++)
        if(l[i]) nr ++;
    printf("%d\n", nr);
    for(int i = 1; i <= n; i ++)
        if(l[i]) printf("%d %d\n", i, l[i]);
}