Pagini recente » Cod sursa (job #2832023) | Cod sursa (job #256611) | Cod sursa (job #1449338) | Cod sursa (job #659016) | Cod sursa (job #2520998)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int DMAX = 10000;
int N, M, E;
vector <int> g[DMAX + 5];
bool d[DMAX + 5];
int matches;
int l[DMAX + 5], r[DMAX + 5];
void Read()
{
fin >> N >> M >> E;
for(int i = 1; i <= E; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back(y);
}
}
bool PairUp(int node)
{
if(d[node] == 1)
return false;
d[node] = 1;
///nod neimperecheat adiacent
for(auto it : g[node])
if(!r[it])
{
++matches;
l[node] = it;
r[it] = node;
return true;
}
///augmenting path
for(auto it : g[node])
if(PairUp(r[it]))
{
l[node] = it;
r[it] = node;
return true;
}
return false;
}
void Solve()
{
bool ok;
do
{
for(int i = 1; i <= N; i++)
d[i] = 0;
ok = false;
for(int i = 1; i <= N; i++)
if(!l[i]) ///nod neimperecheat
ok |= PairUp(i);
}
while(ok);
}
int main()
{
Read();
Solve();
fout << matches << '\n';
for(int i = 1; i <= N; i++)
if(l[i])
fout << i << ' ' << l[i] << '\n';
return 0;
}