Cod sursa(job #950277)

Utilizator RamaStanciu Mara Rama Data 16 mai 2013 14:48:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
#define max 10005

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n, m, st[max], dr[max], e;
bool mark[max];
vector<int> G[max];

void read()
{
    f>>n>>m>>e;
    int x,y;
    for(int i=1;i<=e;++i)
    {
        f>>x>>y;
        G[x].push_back(y);
    }
}

inline bool match(int node)
{
    if (mark[node]!=0) return 0;
    mark[node]=1;
    for(int i=0;i<G[node].size();++i)
    {
        int vc = G[node][i];
        if (dr[vc]==0)
        {
            st[node] = vc;
            dr[vc] = node;
            return 1;
        }
    }

    for(int i=0;i<G[node].size();++i)
    {
        int vc = G[node][i];
        if (match(dr[vc]))
        {
            st[node] = vc;
            dr[vc] = node;
            return 1;
        }
    }
    return 0;
}

void solve()
{
    int k=0,ok=1;
    while(ok==1)
    {
        ok=0;
        for(int i=1;i<=n;++i) mark[i] = 0;
        for(int i=1;i<=n;++i)
        {
            if (st[i]==0 && match(i)>0)
            {
                ok=1;
                ++k;
            }
        }
    }

    g<<k<<"\n";
    for(int i=1;i<=n;++i)
    {
        if (st[i]!=0) g<<i<<" "<<st[i]<<"\n";
    }
}

int main()
{
    read();
    solve();

    return 0;
}