Pagini recente » Cod sursa (job #1382927) | Cod sursa (job #1804101) | Cod sursa (job #881830) | Cod sursa (job #2264967) | Cod sursa (job #1247954)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define Nmax 10002
#define Mmax 10002
int n, C = 0;
vector< int > G[Nmax];//, st(Nmax), dr(Mmax);
int st[Nmax], dr[Mmax];
vector< bool > viz(Nmax);
bool pair_up(int) ;
bool ok() ;
int main()
{
int m, e, a, b;
fin >> n >> m >> e;
for(; e; --e)
{
fin >> a >> b;
G[a].push_back(b);
}
while( ok() )
a = 1;
fout << C << '\n';
for(a = 1; a <= n; ++a)
if(dr[a]) fout << a << ' ' << dr[a] << '\n';
return 0;
}
bool pair_up(int vf)
{
if(viz[vf]) return 0;
viz[vf] = 1;
for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
if(st[*it] == 0)
{
st[*it] = vf;
dr[vf] = *it;
return 1;
}
for(auto it = G[vf].begin(); it != G[vf].end(); ++it)
if(pair_up(st[*it]))
{
st[*it] = vf;
dr[vf] = *it;
return 1;
}
return 0;
}
bool ok()
{
bool rez = 0;
fill(viz.begin(), viz.end(), 0);
for(int i = 1; i <= n; ++i)
if(!dr[i] && pair_up(i)) rez = 1, ++C;
return rez;
}