Pagini recente » Cod sursa (job #329774) | Cod sursa (job #1038341) | Cod sursa (job #2260295) | Cod sursa (job #1477584) | Cod sursa (job #3320308)
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const string txt = "cuplaj";
const int nmax = 1e4 + 5;
ifstream f(txt + ".in");
ofstream g(txt + ".out");
int n1, n2, m, p1[nmax], p2[nmax], viz[nmax];
vector<int> v[nmax];
static bool mbm(int node)
{
if (viz[node]) return false;
viz[node] = 1;
for(auto x : v[node])
if (!p2[x]) {
p2[x] = node; p1[node] = x;
return true;
}
for(auto x : v[node])
if (mbm(p2[x])) {
p2[x] = node; p1[node] = x;
return true;
}
return false;
}
static int cuplaj_max()
{
int oki = 1;
while (oki)
{
oki = 0;
for (int i = 1; i <= n1; i++) viz[i] = 0;
for (int i = 1; i <= n1; i++)
if (!p1[i]) oki += mbm(i);
}
int ans = 0;
for (int i = 1; i <= n1; i++)
if (p1[i]) ans++;
return ans;
}
int main()
{
f.tie(NULL);
f >> n1 >> n2 >> m;
for (int i = 1; i <= m; i++)
{
int x, y; f >> x >> y;
v[x].push_back(y);
}
g << cuplaj_max() << '\n';
for (int i = 1; i <= n1; i++)
if (p1[i])
g << i << " " << p1[i] << '\n';
return 0;
}