Pagini recente » Cod sursa (job #3181727) | Clasament pre002 | Cod sursa (job #1676839) | Cod sursa (job #1006127) | Cod sursa (job #1124686)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int AMax = 10010, BMax = 10010, MMax = 100010;
bool viz[AMax];
int nra, nrb, ncuplaje;
int st[AMax], dr[BMax];
vector <int> G[AMax];
void Read()
{
ifstream f("cuplaj.in");
int m;
f>>nra>>nrb>>m;
for (int i = 1; i<=m; ++i)
{
int x, y; f>>x>>y;
G[x].push_back(y);
}
f.close();
}
inline bool Hopcroft_Karp(const int &node)
{
if (viz[node])
return false;
viz[node] = true;
for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
if (!st[*it])
{
st[*it] = node;
dr[node] = *it;
return true;
}
for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
if (Hopcroft_Karp(st[*it]))
{
dr[node] = *it;
st[*it] = node;
return true;
}
return false;
}
void Solve()
{
for (bool change = true; change; )
{
change = false;
for (int i = 1; i<=nra; ++i)
if (!dr[i])
if (Hopcroft_Karp(i))
change = true;
for (int i = 1; i<=nra; ++i)
viz[i] = false;
}
}
void Write()
{
for (int i = 1; i<=nra; ++i)
if (dr[i])
++ncuplaje;
ofstream g("cuplaj.out");
g<<ncuplaje<<"\n";
for (int i = 1; i<=nra; ++i)
if (dr[i])
g<<i<<" "<<dr[i]<<"\n";
g.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}