Pagini recente » Cod sursa (job #2047359) | Cod sursa (job #2500151) | Cod sursa (job #637487) | Cod sursa (job #912218) | Cod sursa (job #1251396)
#include <fstream>
#include <vector>
#include <cstring>
#define Nmax 10100
#define Neighbour Graph[Node][i]
using namespace std;
vector <int> Graph[Nmax];
int N, M, Solution, Left[Nmax], Right[Nmax];
bool used[Nmax];
bool hookUp(int Node) {
int i;
if(used[Node])
return false;
used[Node] = true;
for(i = 0; i < Graph[Node].size(); i++)
if(Left[Neighbour] == 0) {
Right[Node] = Neighbour;
Left[Neighbour] = Node;
return true;
}
for(i = 0; i < Graph[Node].size(); i++)
if(hookUp(Left[Neighbour])) {
Left[Neighbour] = Node;
Right[Node] = Neighbour;
return true;
}
return false;
}
void Solve() {
int i, last = -1;
while(last < Solution) {
last = Solution;
memset(used, 0, sizeof(used));
for(i = 1; i <= N; i++)
if(Right[i] == 0)
if(hookUp(i))
++Solution;
}
}
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 << Solution << '\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;
}