Pagini recente » Cod sursa (job #57462) | Cod sursa (job #2579219) | Cod sursa (job #2686102) | Cod sursa (job #1753474) | Cod sursa (job #1163688)
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#define nmax 10005
using namespace std;
int n, m, e;
vector <int> G[nmax];
bitset <nmax> viz;
int L[nmax], R[nmax];
int nrm = 0;
void citire()
{
scanf("%d %d %d",&n, &m, &e);
for (int i=1; i<=e; i++)
{
int x, y;
scanf("%d %d",&x, &y);
G[x].push_back(y);
}
}
int cuplaj(int x)
{
if (viz[x])
return 0;
viz[x] = 1;
for (int i=0; i<G[x].size(); i++)
if (!R[G[x][i]] || cuplaj(R[G[x][i]]))
{
L[x] = G[x][i];
R[G[x][i]] = x;
return 1;
}
return 0;
}
void rezolvare()
{
int ok = 1;
while (ok)
{
ok = 0;
viz = 0;
for(int i=1; i <= n; i++)
if(!L[i] && cuplaj(i))
{
nrm++;
ok = 1;
}
}
}
void afisare()
{
printf("%d\n",nrm);
for (int i=1; i<=n; i++)
if (L[i])
printf("%d %d\n",i, L[i]);
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
citire();
rezolvare();
afisare();
return 0;
}