Cod sursa(job #235060)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 22 decembrie 2008 18:30:57
Problema Cuplaj maxim in graf bipartit Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<stdio.h>
int l[10002];
int r[10002];
int solve;
int n;
int e;
int x;
int y;
int m;
int use[10002];
struct Nod {
    int val;
    Nod *next;
};
Nod *a[10002];
void insert(Nod *&q1, int q2)
{
    Nod *u = new Nod;
    u->val = q2;
    u->next = q1;
    q1 = u;
}
int PrUp(int i)
{
    if (use[i])
     return 0;
    use[i] = 1;
    for(Nod *it = a[i]; it; it = it -> next)
     if (!l[it->val])
       {
           l[it->val] = i;
           r[i] = it->val;
           return 1;
       }
    for(Nod *it = a[i]; it; it = it -> next)
     if (PrUp(it->val))
       {
           l[it->val] = i;
           r[i] = it->val;
           return 1;
       }
   return 0;
}
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);
       insert(a[x],y);
     }

     int  ok = 1;
     while (ok)
      {
          ok =0;
          for(int i = 1; i <= n; i++)
           use[i] = 0;
          for(int i = 1; i <= n; i++)
           if (!r[i])
            if (PrUp(i))
                  ok = solve++;
      }
    printf("%d \n",solve);
    for(int i = 1; i <= n; i++)
     if (r[i])
      printf("%d %d \n",i,r[i]);
    return 0;
}