Pagini recente » Cod sursa (job #2971995) | Cod sursa (job #1566550) | Cod sursa (job #2173255) | Cod sursa (job #910756) | Cod sursa (job #3004470)
#include <bits/stdc++.h>
#define N 10005
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
bitset<N>viz;
vector<int>g[N];
int st[N],dr[N],cuplaj;
bool pair_up(int x)
{
if(viz[x]) return 0;
viz[x]=1;
for(auto i:g[x])
if(!dr[i])
{
st[x]=i;dr[i]=x;
return 1;
}
for(auto i:g[x])
if(pair_up(dr[i]))
{
st[x]=i;dr[i]=x;
return 1;
}
return 0;
}
int main()
{
int n1,n2,m,i,x,y;
fin>>n1>>n2>>m;
while(m--)
{
fin>>x>>y;
g[x].push_back(y);
}
bool ok=1;
while(ok)
{
ok=0;viz.reset();
for(i=1;i<=n1;i++)
if(!st[i]) ok|=pair_up(i);
}
for(i=1;i<=n1;i++) cuplaj+=!!st[i];
fout<<cuplaj<<"\n";
for(i=1;i<=n1;i++)
if(st[i]) fout<<i<<" "<<st[i]<<"\n";
return 0;
}