Cod sursa(job #1596751)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 11 februarie 2016 12:58:45
Problema Cuplaj maxim in graf bipartit Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#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 << endl;
    for(int i = 1; i <= n; ++i){
        if(dr[i]){
            g << i << ' ' << dr[i] << endl; } }
 
    return 0; }