Cod sursa(job #3352549)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 28 aprilie 2026 18:14:25
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.86 kb
//https://infoarena.ro/problema/cuplaj

#ifdef _MSC_VER
	#define _CRT_SECURE_NO_WARNINGS
#elif  __GNUC__
	#pragma GCC optimize("Ofast,unroll-loops,inline")
	#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif

//#define _USE_MATH_DEFINES

#include <iostream>
#include <fstream>
#include <utility>
#include <cstdint>
//#include <cstdio>
//#include <algorithm>
#include <vector>
//#include <array>
//#include <list>
//#include <forward_list>
//#include <string>
#include <cstring>
//#include <cmath>
//#include <bitset>
//#include <queue>
//#include <stack>
//#include <map>
//#include <set>
//#include <unordered_map>
//#include <unordered_set>
//#include <limits>
//#include <climits>
//#include <iomanip>
//#include <tuple>
//#include <numeric>
//#include <chrono>
//#include <memory>

using namespace std;

using int64 = int64_t;
using uint64 = uint64_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int16 = int16_t;
using uint16 = uint16_t;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define rfor(i, st, dr) for(auto i=(st); i<=(dr); ++i)
#define for0(i,n) for(auto i=0; i<(n); ++i)
#define rfor0(i,n) for(auto i=(n)-1; i>=0; --i)
#define for1(i,n) for(auto i=1; i<=(n); ++i)
#define rfor1(i,n) for(auto i=(n); i>=1; --i)
#define foreach(x,a) for(auto& x : a)
#define cforeach(x,a) for(const auto& x : a)
#define ft first
#define sd second
#define cendl cout << "\n"
#define fendl fout << "\n"
#define FASTIO ios::sync_with_stdio(false); cin.tie(nullptr);

//#define int int64

//ifstream fin("cuplaj.in");
//ofstream fout("cuplaj.out");

FILE* fin = fopen("cuplaj.in", "r");
FILE* fout = fopen("cuplaj.out", "w");

const int NRMAX = 10000;

int dr[NRMAX + 5], st[NRMAX + 5];
vector<int> gr[NRMAX + 5];
int b[NRMAX + 5], poz;

bool dfs(int x) {
	if (b[x] == poz)
		return false;
	b[x] = poz;

	for (int it : gr[x]) {
		if (!dr[it]) {
			dr[it] = x;
			st[x] = it;
			return true;
		}
	}

	for (int it : gr[x]) {
		if (dfs(dr[it])) {
			dr[it] = x;
			st[x] = it;
			return true;
		}
	}

	return false;
}

int32 main() {
	//FASTIO;

	int n, m, e, i, x, y;
	int rez = 0;

	fscanf(fin, "%d %d %d", &n, &m, &e);

	for (i = 0; i < e; ++i) {
		fscanf(fin, "%d %d", &x, &y);
		gr[x].push_back(y);
	}

	for (int i = 1; i <= n; ++i) {
		for (int it : gr[i]) {
			if (!dr[it]) {
				dr[it] = i;
				st[i] = it;
				++rez;
				break;
			}
		}
	}

	for (i = 1; i <= n; ++i) {
		//memset(b, false, sizeof(b));
		if (!st[i]) {
			++poz;
			if (dfs(i))
				++rez;
		}
	}

	fprintf(fout, "%d\n", rez);
	for (i = 1; i <= n; ++i) {
		if(st[i])
			fprintf(fout, "%d %d\n", i, st[i]);
	}

	return 0;
}