Pagini recente » Cod sursa (job #1944987) | Cod sursa (job #172722) | Cod sursa (job #495794) | Cod sursa (job #79083) | Cod sursa (job #1174026)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int NIL = -1;
vector< vector<int> > G;
int V, L, R;
vector<int> Pair;
vector<bool> Used;
vector< pair<int, int> > Matching;
int PairUp(const int x) {
if (x == NIL || Used[x])
return 0;
Used[x] = true;
for (vector<int>::const_iterator y = G[x].begin(); y != G[x].end(); ++y) {
if (Pair[*y] == NIL || PairUp(Pair[*y])) {
Pair[x] = *y;
Pair[*y] = x;
return 1;
}
}
return 0;
}
vector< pair<int, int> > GetMaximumMatching() {
Pair = vector<int>(V, NIL);
for (int change = 1; change > 0; ) {
change = 0;
Used = vector<bool>(L, false);
for (int x = 0; x < L; ++x)
if (Pair[x] == NIL)
change |= PairUp(x);
}
vector< pair<int, int> > matching;
for (int x = 0; x < L; ++x)
if (Pair[x] != NIL)
matching.push_back(make_pair(x, Pair[x]));
return matching;
}
void Solve() {
Matching = GetMaximumMatching();
}
inline void AddEdge(const int x, const int y) {
G[x].push_back(y);
G[y].push_back(x);
}
void Read() {
ifstream cin("cuplaj.in");
int E;
cin >> L >> R >> E;
V = L + R;
G = vector< vector<int> >(V, vector<int>());
for (; E > 0; --E) {
int x, y;
cin >> x >> y;
AddEdge(x - 1, y - 1 + L);
}
cin.close();
}
void Print() {
ofstream cout("cuplaj.out");
cout << int(Matching.size()) << "\n";
for (vector< pair<int, int> >::const_iterator e = Matching.begin(); e != Matching.end(); ++e)
cout << e->first + 1 << " " << e->second + 1 - L << "\n";
cout.close();
}
int main() {
Read();
Solve();
Print();
return 0;
}