Pagini recente » Cod sursa (job #1275300) | Cod sursa (job #2546051) | Cod sursa (job #1155645) | Cod sursa (job #2904604) | Cod sursa (job #1332738)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
#define nmax 10010
int n,m,e,x,y,match1[nmax],match2[nmax],ok,c;
vector<int> a[nmax];
bitset<nmax> v;
int match(int nod)
{
vector<int>::iterator it;
if (v.test(nod)==true) return 0;
v.set(nod);
for (it=a[nod].begin();it!=a[nod].end();it++)
if (!match2[*it])
{
match1[nod]=*it;
match2[*it]=nod;
return 1;
}
for (it=a[nod].begin();it!=a[nod].end();it++)
if (match(match2[*it]))
{
match1[nod]=*it;
match2[*it]=nod;
return 1;
}
return 0;
}
int main()
{
int i,j;
cin>>n>>m>>e;
for (i=1;i<=e;i++)
{
cin>>x>>y;
a[x].push_back(y);
}
ok=1;
while(ok)
{
ok=0;
v.reset();
for (i=1;i<=n;i++)
if (!match1[i])
ok+=match(i);
}
for (i=1;i<=n;i++)
if (match1[i]) c++;
cout<<c<<'\n';
for (i=1;i<=n;i++)
if (match1[i])
cout<<i<<" "<<match1[i]<<'\n';
}