Pagini recente » Cod sursa (job #1923688) | Cod sursa (job #1714793) | Cod sursa (job #154004) | Cod sursa (job #1732426) | Cod sursa (job #2694438)
#include <cstring>
#include <fstream>
#include <vector>
constexpr auto max_n = 10005;
using namespace std;
FILE* fin;
FILE* fout;
vector<vector<int>> graph;
vector<int> l;
vector<int> r;
int n, m, e;
vector<bool> visited;
bool pairup(const int node)
{
if (visited[node])
return false;
visited[node] = true;
for (auto neigh : graph[node])
{
if (r[neigh])
continue;
l[node] = neigh;
r[neigh] = node;
return true;
}
for (auto neigh : graph[node])
{
if (pairup(r[neigh]))
{
l[node] = neigh;
r[neigh] = node;
return true;
}
}
return false;
}
int main()
{
fin = fopen("cuplaj.in", "r");
fout = fopen("cuplaj.out", "w");
fscanf(fin, "%d %d %d", &n, &m, &e);
l.resize(n + 1, 0);
r.resize(m + 1, 0);
graph.resize(n + 1);
for (auto i = 0; i < e; i++)
{
int x, y;
fscanf(fin, "%d %d", &x, &y);
graph[x].push_back(y);
}
auto has_solution = true;
while (has_solution)
{
has_solution = false;
visited.clear();
visited.resize(n + 1, false);
for (auto i = 1; i <= n; i++)
if (!l[i])
has_solution = has_solution || pairup(i);
}
auto result = 0;
for (auto i = 1; i <= n; i++)
if (l[i] > 0)
result++;
fprintf(fout, "%d\n", result);
for (auto i = 1; i <= n; i++)
if (l[i] > 0)
fprintf(fout, "%d %d\n", i, l[i]);
return 0;
}