Cod sursa(job #918328)

Utilizator apopeid13Apopeid Alejandro apopeid13 Data 18 martie 2013 20:05:29
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int rez,st[10005],dr[10005],x,y,i,n,m,e;
bool ok,viz[10005];
vector<int>v[10005];
 
bool dfs(int x)
{
    if (viz[x]) return false;
    viz[x]=true;
    for (int j=0;j<v[x].size();j++)
        if (!dr[v[x][j]])
        {
            st[x]=v[x][j];
            dr[v[x][j]]=x;
            rez++;
            return true;
        }
    for (int j=0;j<v[x].size();j++)
        if (dfs(dr[v[x][j]]))
        {
            st[x]=v[x][j];
            dr[v[x][j]]=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 (i=1;i<=e;i++) scanf("%d %d",&x,&y),v[x].push_back(y);
    ok=true;
    while (ok)
    {
        ok=false;
        memset(viz,false,sizeof(viz));
        for (i=1;i<=n;i++)if (!st[i]) if (dfs(i)) ok=true;
    }
    printf("%d\n",rez);
    for (i=1;i<=n;i++)
        if (st[i]) printf("%d %d\n",i,st[i]);
    return 0;
}