Pagini recente » Cod sursa (job #2424334) | Cod sursa (job #673266) | Cod sursa (job #1966369) | Cod sursa (job #2735062) | Cod sursa (job #275701)
Cod sursa(job #275701)
// 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;
}