Cod sursa(job #1181031)

Utilizator dropsdrop source drops Data 1 mai 2014 17:42:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 10001

vector<int>edge[MAX];
int n, m, e, x, y, st[MAX], dr[MAX];
bool viz[MAX];

bool dfs(int x) {
    if (viz[x]) {
        return false;
    }
    viz[x] = 1;
    for (int i = 0; i < edge[x].size(); i++) {
            int y = edge[x][i];
            if (dr[y] == 0 || dfs(dr[y])) {
                st[x] = y;
                dr[y] = x;
                return true;
            }
    }
    return false;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d", &n, &m, &e);

    for (int i = 1; i <= e; i++) {
        scanf("%d %d", &x, &y);
        edge[x].push_back(y);
    }

    bool more = true;
    int match = 0;
    while (more) {
        more = false;
        memset(viz,0,sizeof(viz));
        for (int i = 1; i <= n; i++)
        if (st[i] == 0 && dfs(i)){
            more = true;
            match++;
        }
    }

    printf("%d\n", match);
    for (int i = 1; i <= n; i++)
    if (st[i]){
        printf("%d %d\n", i, st[i]);
    }

    return 0;
}