Pagini recente » Cod sursa (job #596424) | Cod sursa (job #1255107) | Cod sursa (job #1255024) | Cod sursa (job #1918232) | Cod sursa (job #2282148)
#include <bits/stdc++.h>
#define pb push_back
#define Nmax 10005
#define Mmax 10005
using namespace std;
vector < int > V[10005];
int N, M, E, x, y, Cmax, L[Nmax], R[Mmax];
bool Marc[Nmax], ok;
ifstream f ("cuplaj.in");
ofstream g ("cuplaj.out");
int cuplez (int x)
{
Marc[x] = true;
for(auto y: V[x])
if(!R[y])
{
L[x] = y;
R[y] = x;
return 1;
}
for(auto y: V[x])
if(!Marc[R[y]] && cuplez(R[y]))
{
L[x] = y;
R[y] = x;
return 1;
}
return 0;
}
void cuplaj()
{
bool ok = true;
while (ok)
{
ok = false;
for(int i = 1; i <= N; i++) Marc[i] = false;
for(int i = 1; i <= N; i++)
if(!L[i] && !Marc[i])
if( cuplez(i))
{
ok = true;
Cmax++;
}
}
}
int main()
{
f >> N >> M >> E;
for ( ; E; E--)
{
f >> x >> y;
V[x].pb(y);
}
cuplaj();
g << Cmax << '\n';
for (int i = 1; i <= N; ++i)
if(L[i]) g << i << " " << L[i] << '\n';
return 0;
}