Pagini recente » Cod sursa (job #1547350) | Cod sursa (job #1371174) | Cod sursa (job #2947502) | Cod sursa (job #2127913) | Cod sursa (job #1116324)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
bool ok=1, viz[10002];
int N, M, nrm, st[10002], dr[10002], sol, x, y;
vector < int > G[10002];
bool cuplaj(int nod)
{
if (viz[nod]) return 0;
viz[nod]=1; vector <int>::iterator it=G[nod].begin();
for (; it!=G[nod].end(); ++it)
if (!dr[*it])
{
dr[*it]=nod; st[nod]=*it; return 1;
}
it=G[nod].begin();
for (; it!=G[nod].end(); ++it)
if (dr[*it] && cuplaj(*it))
{
dr[*it]=nod; st[nod]=*it; return 1;
}
return 0;
}
int main()
{
f>>N>>M>>nrm;
for (int i=1; i<=N; ++i)
f>>x>>y, G[x].push_back(y);
while (ok)
{
for (int i=1; i<=N; ++i)
viz[i]=0; ok=0;
for (int i=1; i<=N; ++i)
if (!st[i] && cuplaj(i))
ok=1, ++sol, cuplaj(i);
}
g<<sol<<'\n';
for (int i=1; i<=N; ++i)
if (st[i]) g<<i<<' '<<st[i]<<'\n';
return 0;
}