Pagini recente » Cod sursa (job #1053176) | Cod sursa (job #2006794) | Cod sursa (job #1732512) | Cod sursa (job #2569747) | Cod sursa (job #2572066)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax = 10005;
int n, m, M;
vector <int> g[nmax];
int st[nmax], dr[nmax], viz[nmax];
inline void read()
{
fin >> n >> m >> M;
while (M--)
{
int a, b;
fin >> a >> b;
g[a].push_back(b);
}
}
inline bool check(int nod)
{
if (viz[nod])
return false;
viz[nod] = 1;
for (unsigned int i = 0; i < g[nod].size(); i++)
{
int vecin = g[nod][i];
if (!dr[vecin])
{
st[nod] = vecin;
dr[vecin] = nod;
return true;
}
}
for (unsigned int i = 0; i < g[nod].size(); i++)
{
int vecin = g[nod][i];
if (check(dr[vecin]))
{
st[nod] = vecin;
dr[vecin] = nod;
return true;
}
}
return false;
}
inline void cupleaza()
{
int ok = 0;
int ans = 0;
while (!ok)
{
ok = 1;
memset(viz, 0, sizeof(viz));
for (int i = 1; i <= n; i++)
if (!st[i] && check(i))
ans++, ok = 0;
}
fout << ans << "\n";
for (int i = 1; i <= n; i++)
if (st[i])
fout << i << " " << st[i] << "\n";
}
int main()
{
read();
cupleaza();
return 0;
}