Pagini recente » Cod sursa (job #121372) | Cod sursa (job #1288401) | Cod sursa (job #2029044) | Cod sursa (job #2468051) | Cod sursa (job #3042002)
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
ifstream cin ("cuplaj.in");
ofstream cout ("cuplaj.out");
const int N = 1e4;
int l[N + 1], r[N + 1];
bool viz[N + 1];
vector <int> g[N + 1];
int n, m, e, x, y;
bool cuplez (int node)
{
viz[node] = 1;
for (auto it : g[node])
if (!r[it])
{
l[node] = it;
r[it] = node;
return 1;
}
for (auto it : g[node])
if (!viz[r[it]] && cuplez (r[it]))
{
l[node] = it;
r[it] = node;
return 1;
}
return 0;
}
int main()
{
for (cin >> n >> m >> e; e-- && cin >> x >> y; )
g[x].push_back(y);
int cuplaj = 0;
while (1)
{
memset (viz, 0, sizeof(viz));
bool ok = false;
for (int i = 1; i <= n; ++i)
if (!l[i] && cuplez(i))
++cuplaj, ok = true;
if (!ok)
break;
}
cout << cuplaj << '\n';
for (int i = 1; i <= n; ++i)
if (l[i])
cout << i << ' ' << l[i] << '\n';
return 0;
}