Cod sursa(job #1879698)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 15 februarie 2017 09:26:36
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 1e4 + 10;
vector<int> vec[maxn] = {};
int a, b, m, st[maxn] = {}, dr[maxn] = {}, viz[maxn] = {};
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

bool pairup(const int x){
    if(viz[x]) return false;
    viz[x] = true;
    for(auto y : vec[x])
        if(!st[y]) return st[y] = x, dr[x] = y;

    for(auto y : vec[x])
        if(pairup(st[y]))
            return st[y] = x, dr[x] = y;
    return false; }

void mk_cuplaj(){
    for(bool b = true; b; ){
        fill(viz, viz + a + 5, 0);
        b = false;
        for(int i = 1; i <= a; ++i)
            if(!viz[i] && !dr[i]) b = (pairup(i) || b); } }

int main(){
    f >> a >> b >> m;
    for(int i = 0, x, y; i < m; ++i)
        f >> x >> y, vec[x].push_back(y);
    mk_cuplaj();
    g << a - count(dr+1, dr+a+1, 0) << '\n';
    for(int i = 1; i <= a; ++i)
        if(dr[i]) g << i << ' ' << dr[i] << '\n';
    return 0; }