Cod sursa(job #572736)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 5 aprilie 2011 16:20:51
Problema Felinare Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include<cstdio>
#include<vector>
#include<bitset>
using namespace std;
#define pb push_back
#define NM 8192
#define sh short int
bitset<NM>u,st,dr;
sh N,M,x,y,r[NM],l[NM];
vector<sh>a[NM];
inline void citire()
{
	freopen("felinare.in","r",stdin);
	freopen("felinare.out","w",stdout);
	scanf("%hd%hd",&N,&M);
	sh i;
	for (i=1; i<=M; ++i)
	{
		scanf("%hd%hd",&x,&y);
		a[x].pb(y);
	}
}
inline bool pairup(sh nod)
{
	if (u[nod])
		return 0;
	u[nod]=1;
	vector<sh>:: iterator it=a[nod].begin(),e=a[nod].end();
	for (; it!=e; ++it)
	{
		if (!r[*it])
		{
			r[*it]=nod;
			l[nod]=(*it);
			return 1;
		}
	}		
	it=a[nod].begin();
	for (; it!=e; ++it)
	{
		if (pairup(r[*it]))
		{
			r[*it]=nod;
			l[nod]=(*it);
			return 1;
		}
	}
	return 0;
}
inline void solve()
{
	bool ch=1;
	while (ch)
	{
		ch=0;
		u.reset();
		for (int i=1; i<=N; ++i)
			if (!l[i])
			ch|=pairup(i);
	}
	sh num=0;
	for (sh i=1; i<=N; ++i)
	{
		num+=(l[i]>0);
		st[i]=1;
		dr[l[i]]=1;
	}
	printf("%hd\n",(N<<1)-num);
	/*for (sh i=1; i<=N; ++i)
	{
		if (st[i]&&dr[i])
		{
			printf("0\n");
			continue;
		}
		if (st[i]&&!dr[i])
		{
			printf("2\n");
			continue;
		}
		if (dr[i]&&!st[i])
		{
			printf("3\n");
			continue;
		}
		printf("1\n");
		
	}*/
}
int main()
{
	citire();
	solve();
	return 0;
}