Pagini recente » Cod sursa (job #1467787) | Cod sursa (job #147088) | Cod sursa (job #45430) | Cod sursa (job #876100) | Cod sursa (job #1333847)
#include <fstream>
#include <vector>
#include <cstring>
#define Nmax 10100
#define neighbour Graph[node][i]
using namespace std;
vector <int> Graph[Nmax];
int couples, N, M, Left[Nmax], Right[Nmax];
bool seen[Nmax];
bool hookup(int node) {
if(seen[node])
return false;
else
seen[node] = true;
for(int i = 0; i < Graph[node].size(); i++)
if(Left[neighbour] == 0) {
Right[node] = neighbour;
Left[neighbour] = node;
return true;
}
for(int i = 0; i < Graph[node].size(); i++)
if(hookup(Left[neighbour])) {
Right[node] = neighbour;
Left[neighbour] = node;
return true;
}
return false;
}
void Solve() {
int i, last;
last = -1;
while(last < couples) {
last = couples;
memset(seen, 0, sizeof(seen));
for(i = 1; i <= N; i++)
if(Right[i] == 0 && hookup(i))
couples++;
}
}
void Read() {
int x, y, E;
ifstream in("cuplaj.in");
in >> N >> M >> E;
while(E--) {
in >> x >> y;
Graph[x].push_back(y);
}
in.close();
}
void Write() {
ofstream out("cuplaj.out");
out << couples << '\n';
for(int i = 1; i <= N; i++)
if(Right[i] != 0)
out << i << ' ' << Right[i] << '\n';
out.close();
}
int main() {
Read();
Solve();
Write();
return 0;
}