Pagini recente » Cod sursa (job #2248901) | Cod sursa (job #575760) | Cod sursa (job #2888787) | Cod sursa (job #733915) | Cod sursa (job #755986)
Cod sursa(job #755986)
#include<fstream>
#include<vector>
#include<string.h>
#define nmax 10010
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
vector <int> G[nmax];
int L[nmax], R[nmax], N, M, E;
bool used[nmax];
int Nr = 0 ;
bool pairup(int x)
{
if(used[x] == true) return false;
used[x] = true;
for(int i = 0 ; i < G[x].size(); ++i )
{
int nod = G[x][i];
if(R[nod] == 0 || pairup(R[nod]))
{
L[x] = nod;
R[nod] = x;
return true;
}
}
return false;
}
void cuplaj()
{
bool ok = true;
while(ok)
{
memset(used, 0, sizeof(bool) * (N + 1));
ok = false;
for(int i = 1; i <= N; i++)
// if( L[i] == 0 )
if( L[i] == 0 && pairup(i) )
{
//pairup(i);
++Nr;
ok = true;
}
}
}
void read()
{
fin >> N >> M>> E;
while(E)
{
int x, y ;
fin>> x>> y;
G[x].push_back(y);
--E;
}
cuplaj();
}
int main()
{
read();
fout << Nr <<'\n';
for(int i = 1; i <= N; i++)
if(L[i])
fout << i << " " <<L[i] <<'\n';
fin.close();
return 0;
}