Pagini recente » Cod sursa (job #706770) | Cod sursa (job #1329437) | Cod sursa (job #1289649) | Cod sursa (job #646828) | Cod sursa (job #2137039)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int viz[10005], dr[10005], st[10005], n, m, e;
vector <int> L[10005];
void Citire()
{
int x, y;
fin >> n >> m >> e;
while(e--)
{
fin >> x >> y;
L[x].push_back(y);
}
}
int Cupleaza(int x)
{
if(viz[x]) return 0;
viz[x] = 1;
for(auto i : L[x])
if(dr[i] == 0)
{
st[x] = i;
dr[i] = x;
return 1;
}
for(auto i : L[x])
if(Cupleaza(dr[i]))
{
st[x] = i;
dr[i] = x;
return 1;
}
return 0;
}
void Rezolva()
{
int gata = 0, cuplaje = 0;
while(gata == 0)
{
gata = 1;
for(int i = 1; i <= n; i++) viz[i] = 0;
for(int i = 1; i <= n; i++)
if(st[i] == 0 && Cupleaza(i))
cuplaje++,
gata = 0;
}
fout << cuplaje << "\n";
for(int i = 1; i <= n; i++)
if(st[i] != 0)
fout << i << " " << st[i] << "\n";
}
int main()
{
Citire();
Rezolva();
return 0;
}