Pagini recente » Cod sursa (job #2082430) | Cod sursa (job #58646) | Cod sursa (job #2533532) | Cod sursa (job #1494465) | Cod sursa (job #2363757)
#include <iostream>
#include <fstream>
#include <bitset>
#include <vector>
#define nmax 10000
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n,m,e,l[nmax+5],r[nmax+5];
bitset<nmax+5> viz;
vector<int> graf[nmax+5];
bool pairup(int x)
{
if(viz[x])
return 0;
viz[x]=1;
for(auto it:graf[x])
if(!r[it])
{
l[x]=it;
r[it]=x;
return 1;
}
for(auto it:graf[x])
if(pairup(r[it]))
{
l[x]=it;
r[it]=x;
return 1;
}
return 0;
}
int main()
{
fin>>n>>m>>e;
int x,y,s=0;
for(int i=1;i<=e;i++)
{
fin>>x>>y;
graf[x].push_back(y);
}
bool change=1;
while(change)
{
change=0;
viz.reset();
for(int i=1;i<=n;i++)
if(!l[i])
change=change|pairup(i);
}
for(int i=1;i<=n;i++)
s+=(l[i]!=0);
fout<<s<<"\n";
for(int i=1;i<=n;i++)
if(l[i])
fout<<i<<" "<<l[i]<<"\n";
return 0;
}