Pagini recente » Cod sursa (job #1373528) | Cod sursa (job #512969) | Cod sursa (job #1442998) | Rating Stan George Alexandru (stangeorge10) | Cod sursa (job #2381946)
#include <bits/stdc++.h>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int DIM = 1e4 + 7;
vector <int> v[DIM];
int M1[DIM];
int M2[DIM];
bitset <DIM> vis;
bool cuplaj(int nod)
{
if(vis[nod] == 1)
return false;
vis[nod] = 1;
for(auto i : v[nod])
if(M2[i] == 0)
{
M1[nod] = i;
M2[i] = nod;
return true;
}
else
{
int x = M2[i];
if(cuplaj(x))
{
M1[nod] = i;
M2[i] = nod;
return true;
}
}
return false;
}
int main()
{
int n, m, e;
in >> n >> m >> e;
while(e--)
{
int x, y;
in >> x >> y;
v[x].push_back(y);
}
bool ok = true;
while(ok == true)
{
ok = false;
vis.reset();
for(int i = 1; i <= n; i++)
if(M1[i] == 0 && cuplaj(i) == true)
{
ok = true;
}
}
int nr = 0;
for(int i = 1; i <= n; i++)
if(M1[i] != 0)
nr++;
out << nr << '\n';
for(int i = 1; i <= n; i++)
if(M1[i] != 0)
out << i << ' ' << M1[i] << '\n';
}