Pagini recente » Cod sursa (job #1092178) | Cod sursa (job #1383018) | Cod sursa (job #554477) | Cod sursa (job #1947366) | Cod sursa (job #1596749)
#include <fstream>
#include <array>
#include <bitset>
#include <algorithm>
#include <vector>
using namespace std;
constexpr int maxn = 10005;
static bool try_to_repair(const int x, const array<vector<int>, maxn>& graf,
array<int, maxn>& right, array<int, maxn>& left, bitset<maxn>& used){
if(used[x]){
return false; }
used[x] = true;
for(const auto y : graf[x]){
if(!left[y]){
right[x] = y;
left[y] = x;
return true; } }
for(const auto y : graf[x]){
if(try_to_repair(left[y], graf, right, left, used)){
right[x] = y;
left[y] = x;
return true; } }
return false; }
int main(){
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, e;
f >> n >> m >> e;
static array<vector<int>, maxn> graf;
for(int i = 0, a, b; i < e; ++i){
f >> a >> b;
graf[a].push_back(b); }
static array<int, maxn> left, right;
static bitset<maxn> used = 0;
for(bool changed = true; changed; ){
changed = false;
used.reset();
for(int i = 1; i <= n; ++i){
if(right[i] == -1){
changed = (try_to_repair(i, graf, right, left, used) || changed); } } }
const int sz_cuplaj = count_if(begin(right), end(right), [](const int x){ return x; });
g << sz_cuplaj << endl;
for(int i = 1; i <= n; ++i){
if(right[i]){
g << i << ' ' << right[i] << endl; } }
return 0; }