Pagini recente » Cod sursa (job #284455) | Monitorul de evaluare | Cod sursa (job #281860) | Cod sursa (job #582486) | Cod sursa (job #2844970)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
vector<int> G[10001];
int N, M, E, L[10001], R[10001];
bool Use[10001];
void Read()
{
in >> N >> M >> E;
for(int i = 1, x, y; i <= E; ++i)
{
in >> x >> y;
G[x].push_back(y);
}
}
bool Pairup(int Nod)
{
if(Use[Nod]) return 0;
Use[Nod] = 1;
for(auto Vecin : G[Nod])
if(!R[Vecin])
{
L[Nod] = Vecin;
R[Vecin] = Nod;
return 1;
}
for(auto Vecin : G[Nod])
if(Pairup(R[Vecin]))
{
L[Nod] = Vecin;
R[Vecin] = Nod;
return 1;
}
return 0;
}
void Solve()
{
bool Ok;
do
{
Ok = 0;
memset(Use, 0, sizeof(Use));
for(int i = 1; i <= N; ++i)
if(!L[i])
Ok |= Pairup(i);
}
while(Ok);
}
void Print()
{
int Cuplaj = 0;
for(int i = 1; i <= N; ++i)
if(L[i]) Cuplaj++;
out << Cuplaj << '\n';
for(int i = 1; i <= N; ++i)
if(L[i])
out << i << ' ' << L[i] << '\n';
}
int main()
{
Read();
Solve();
Print();
return 0;
}