Pagini recente » Cod sursa (job #612781) | Clasament preoji2010-2 | Cod sursa (job #641304) | Cod sursa (job #70655) | Cod sursa (job #2886847)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const string filename = "cuplaj";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");
const int maxN = 10005;
int n, n2, m;
int l[maxN], r[maxN];
vector <int> G[maxN];
bool used[maxN];
bool pairup(int nod)
{
if(used[nod])
return 0;
used[nod] = 1;
for(int vecin : G[nod])
{
if(!r[vecin])
{
l[nod] = vecin;
r[vecin] = nod;
return 1;
}
}
for(int vecin : G[nod])
{
if(pairup(r[vecin]))
{
l[nod] = vecin;
r[vecin] = nod;
return 1;
}
}
return 0;
}
int main()
{
fin >> n >> n2 >> m;
for(int x, y, i = 1; i <= m; i++)
{
fin >> x >> y;
G[x].push_back(y);
}
bool changed = 1;
while(changed)
{
changed = 0;
for(int i = 1; i <= n; i++)
used[i] = 0;
for(int i = 1; i <= n; i++)
if(!l[i])
changed |= pairup(i);
}
int ans = 0;
for(int i = 1; i <= n; i++)
if(l[i])
ans++;
fout << ans << '\n';
for(int i = 1; i <= n; i++)
if(l[i])
fout << i << ' ' << l[i] << '\n';
return 0;
}