Pagini recente » Cod sursa (job #2074682) | Cod sursa (job #1857437) | Cod sursa (job #1132864) | Cod sursa (job #798761) | Cod sursa (job #2937629)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int a[10001][10001];
int n, m, e;
int a1, a2;
int viz[20002];
int st[10001];
int dr[10001];
void citire()
{
fin >> n >> m >> e;
for(int i = 1; i <= e; i++)
{
fin >> a1 >> a2;
a[a1][a2] = 1;
}
}
int rez(int p)
{
if(viz[p] == 1) return 0;
viz[p] = 1;
for(int i = 1; i <= n; i++)
{
if(a[p][i] == 1 && !dr[i])
{
dr[i] = p;
st[p] = i;
return 1;
}
if(a[p][i] == 1 && rez(dr[i]) == 1)
{
st[p] = i;
dr[i] = p;
return 1;
}
}
return 0;
}
int main()
{
citire();
int ok = 1;
int c = 0;
while(ok == 1)
{
ok = 0;
for(int i = 1; i <= n; i++)
viz[i] = 0;
for(int i = 1; i <= n; i++)
if(st[i] == 0 && rez(i) == 1)
ok = 1;
}
for(int i = 1; i <= n; i++)
{
if(st[i]) c++;
}
fout << c << endl;
for(int i = 1; i <= n; i++)
{
if(st[i]) fout << i << ' ' << st[i] << endl;
}
return 0;
}