Pagini recente » Cod sursa (job #2811393) | Cod sursa (job #2839173) | Cod sursa (job #549921) | Cod sursa (job #1109329) | Cod sursa (job #1122890)
#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], x, y, sol;
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<=nrm; ++i)
f>>x>>y, G[x].push_back(y), G[y].push_back(x);
while (ok)
{
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;
}