Pagini recente » Cod sursa (job #1009769) | Cod sursa (job #2204016) | Cod sursa (job #1174595) | Cod sursa (job #1574428) | Cod sursa (job #1706566)
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1 + 1e5;
vector<int> adj[N];
int match[N], pairof[N], nA, nB;
bool use[N];
bool pairup(int x){
if ( use[x] ) return false;
use[x] = true;
for (int y : adj[x])
if ( pairof[y] == -1 ){
match[x] = y;
pairof[y] = x;
return true;
}
for (int y : adj[x])
if ( pairup( pairof[y] ) ){
match[x] = y;
pairof[y] = x;
return true;
}
return false;
}
int maxMatch(){
memset(match, -1, sizeof(match));
memset(pairof, -1, sizeof(pairof));
bool go;
do {
memset(use, false, sizeof(use));
go = false;
for (int i = 1; i <= nA; i++)
if ( match[i] == -1 )
go |= pairup(i);
} while (go);
}
int main(){
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int times, x, y;
in >> nA >> nB >> times;
while (times--){
in >> x >> y;
adj[x].push_back(y);
}
maxMatch();
int ans = 0;
for (int i = 1; i <= nA; i++)
ans += match[i] != -1;
out << ans << '\n';
for (int i = 1; i <= nA; i++)
if ( match[i] != -1 )
out << i << ' ' << match[i] << '\n';
return 0;
}