Pagini recente » Cod sursa (job #1108397) | Cod sursa (job #1477743) | Rating Tintelecan Raluca (ralucatintelecan) | Cod sursa (job #1492048) | Cod sursa (job #1596754)
#include <fstream>
#include <array>
#include <bitset>
#include <algorithm>
#include <vector>
using namespace std;
constexpr int maxn = 10005;
array<vector<int>, maxn> graf;
array<int, maxn> st, dr;
bitset<maxn> used = 0;
static bool try_to_repair(const int x){
if(used[x]){
return false; }
used[x] = true;
for(const auto y : graf[x]){
if(!st[y]){
dr[x] = y;
st[y] = x;
return true; } }
for(const auto y : graf[x]){
if(try_to_repair(st[y])){
dr[x] = y;
st[y] = x;
return true; } }
return false; }
int main(){
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n, m, e;
f >> n >> m >> e;
for(int i = 0, a, b; i < e; ++i){
f >> a >> b;
graf[a].push_back(b); }
for(bool changed = true; changed; ){
changed = false;
used.reset();
for(int i = 1; i <= n; ++i){
if(!dr[i]){
changed = (try_to_repair(i) || changed); } } }
const int sz_cuplaj = count_if(begin(dr)+1, begin(dr)+n+1, [](const int x){ return x; });
g << sz_cuplaj << '\n';
for(int i = 1; i <= n; ++i){
if(dr[i]){
g << i << ' ' << dr[i] << '\n'; } }
return 0; }