Cod sursa(job #1967793)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 17 aprilie 2017 02:35:34
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;

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

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

void mk_cuplaj(){
    for(bool done = true; done; ){
        viz = 0;
        done = false;
        for(int i = 1; i <= n; ++i)
            if(!dr[i])
                done = (cuplaj(i) || done); } }

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