Pagini recente » Cod sursa (job #1742271) | Cod sursa (job #1086571) | Cod sursa (job #2114012) | Cod sursa (job #981097) | Cod sursa (job #3347683)
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define ll long long
using namespace std;
ifstream fin("cuplaj.in");
ofstream gout("cuplaj.out");
vector<vector<int>>g(10001);
int n,m,e,l[10001],r[10001],maxMatch;
bool viz[10001];
bool match(int x)
{
if(viz[x])return 0;
viz[x]=1;
for(const auto &y:g[x])if(!r[y])
{
l[x]=y;
r[y]=x;
return 1;
}
for(const auto &y:g[x])if(match(r[y]))
{
l[x]=y;
r[y]=x;
return 1;
}
return 0;
}
void hk()
{
bool changed=1;
int i;
while(changed)
{
changed=0;
for(i=1; i<=n; ++i)viz[i]=0;
for(i=1; i<=n; ++i)if(!l[i]&&match(i))
{
changed=1;
++maxMatch;
}
}
}
int main()
{
int x,y;
fin>>n>>m>>e;
while(e--)
{
fin>>x>>y;
g[x].push_back(y);
}
hk();
gout<<maxMatch<<'\n';
for(int i=1; i<=n; ++i)if(l[i])gout<<i<<' '<<l[i]<<'\n';
return 0;
}