Pagini recente » Cod sursa (job #48322) | Cod sursa (job #2175998) | Cod sursa (job #2086061) | Cod sursa (job #1574301) | Cod sursa (job #3124412)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const int N = 1e4;
const int M = 1e5;
vector <int> a1[N+1], a2[N+1], pereche1, pereche2;
bitset <N+1> viz;
int caut_pereche(int x1)
{
if (viz[x1])
{
return 0;
}
viz[x1] = 1;
for (auto x2: a1[x1])
{
if (pereche2[x2] == 0 || caut_pereche(pereche2[x2]))
{
pereche1[x1] = x2;
pereche2[x2] = x1;
return x2;
}
}
return 0;
}
int main()
{
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int n1, n2, m;
in >> n1 >> n2 >> m;
pereche1.resize(n1 + 1, 0);
pereche2.resize(n2 + 1, 0);
for (int i = 0; i < m; i++)
{
int x1, x2;
in >> x1 >> x2;
a1[x1].push_back(x2);
a2[x2].push_back(x1);
}
int cuplaj = 0;
bool cresc_c;
do
{
cresc_c = false;
viz.reset();
for (int x1 = 1; x1 <= n1; x1++)
{
if (pereche1[x1] == 0)
{
pereche1[x1] = caut_pereche(x1);
if (pereche1[x1] != 0)
{
cresc_c = true;
cuplaj++;
}
}
}
}
while (cresc_c);
out << cuplaj << "\n";
for (int x1 = 1; x1 <= n1; x1++)
{
if (pereche1[x1] != 0)
{
out << x1 << " " << pereche1[x1] << "\n";
}
}
in.close();
out.close();
return 0;
}