Cod sursa(job #275701)

Utilizator marcelcodreaCodrea Marcel marcelcodrea Data 10 martie 2009 16:59:16
Problema Felinare Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
// suport minim in graf bipartit + algoritm complet de cuplaj maxim

#include<stdio.h>
struct Nod {
    int val;
    Nod *next;
};
Nod *a[100001];
int use[100001];
int n;
int m;
int ok;
int nrcupl;
int x;
int y;
int l[100001];
int r[100001];
void insert(Nod *&u, int k)
{
    Nod *v = new Nod;
    v -> val = k;
    v -> next = u;
    u = v;
}
int PrUp(int nod)
{
    if (use[nod]) return 0;
    use[nod] = 1;
    for(Nod *it = a[nod]; it; it = it -> next)
     if (!l[it->val])
       {
           l[it->val] = nod;
           r[nod] = it->val;
           return 1;
       }
    for(Nod *it = a[nod]; it; it = it -> next)
     if (PrUp(l[it->val]))
       {
           l[it->val] = nod;
           r[nod] = it->val;
           return 1;
       }
    return 0;
}
int main()
{
    freopen("felinare.in","r",stdin);
    freopen("felinare.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i = 1; i <= m; i++)
    {
     scanf("%d %d",&x,&y);
     insert(a[x],y);
    }
    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 = ++nrcupl;
     }
    printf("%d",2 * n - nrcupl);
    return 0;
}