Pagini recente » Cod sursa (job #3259537) | Cod sursa (job #2373786) | Cod sursa (job #2710673) | Cod sursa (job #2535551) | Cod sursa (job #2207213)
#include <vector>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
const int Dim = 10001;
using VI = vector < int > ;
using VVI = vector < VI > ;
int n,m,e,maxmat,L[Dim],R[Dim];
bool V[Dim];
VVI G;
void Read();
void HopcroftKarp();
bool Cupleaza(int x);
int main() {
Read();
HopcroftKarp();
fout << maxmat << "\n";
for ( int i = 1; i <= n; ++i)
if (L[i])
fout << i << " " << L[i] << "\n";
}
void HopcroftKarp() {
bool found_path = true;
while (found_path) {
found_path = false;
memset(V,0,sizeof(V));
for ( int i = 1; i <= n; ++i)
if (!L[i] and Cupleaza(i))
++maxmat, found_path = true;
}
}
bool Cupleaza(int x) {
if ( V[x] ) return false;
V[x] = true;
for ( const int & y : G[x])
if (!R[y] or Cupleaza(R[y])) {
L[x] = y;
R[y] = x;
return true;
}
return false;
}
void Read() {
fin >> n >> m >> e;
G = VVI(n+1);
int x,y;
for (; e > 0; --e) {
fin >> x >> y;
G[x].push_back(y);
}
}