Pagini recente » Cod sursa (job #2617649) | Cod sursa (job #370715) | Istoria paginii utilizator/anaturcitu | Cod sursa (job #2291317) | Cod sursa (job #2960356)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int N, M, E, u, v, st[10011], dr[10011];
bool viz[10011];
vector<int> G[10011];
int pairup(int n)
{
if (viz[n])
return 0;
viz[n] = true;
for (int i = 0; i < G[n].size(); i++)
{
int x = G[n][i];
if (dr[x] == 0 || pairup(dr[x]))
{
st[n] = x;
dr[x] = n;
return 1;
}
}
for(int i = 0; i < G[n].size(); i++)
{
int x = G[n][i];
if (pairup(dr[x]))
{
st[n] = x;
dr[x] = n;
return 1;
}
}
return 0;
}
int main()
{
f >> N >> M >> E;
for(int i = 1; i <= E; i++)
{
f >> u >> v;
G[u].push_back(v);
}
bool ok = true;
while(ok)
{
ok = false;
memset(viz, 0, sizeof(viz));
for(int i = 1; i <= N; i++)
if (st[i] == 0)
ok |= pairup(i);
}
int cnt = 0;
for(int i = 1; i <= N; i++)
if (st[i] != 0)
cnt++;
g << cnt << '\n';
for(int i = 1; i <= N; i++)
if (st[i])
g << i << ' ' << st[i] << '\n';
return 0;
}