Cod sursa(job #2716918)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 5 martie 2021 22:19:01
Problema Felinare Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
//
// Created by mihai145 on 05.03.2021.
//

#include <fstream>
#include <vector>

using namespace std;

ifstream cin("felinare.in");
ofstream cout("felinare.out");

const int NMAX = 9000;

int N, M;
vector<int> g[NMAX + 2];

int matching;
int l[NMAX + 2], r[NMAX + 2];
bool d[NMAX + 2];

bool PairUp(int node) {
    if(d[node]) {
        return false;
    }

    d[node] = true;

    for(int it : g[node]) {
        if(!r[it]) {
            ++matching;
            r[it] = node;
            l[node] = it;
            return true;
        }
    }

    for(int it : g[node]) {
        if(PairUp(r[it])) {
            r[it] = node;
            l[node] = it;
            return true;
        }
    }

    return false;
}

int main() {
    cin >> N >> M;

    for(int i = 1; i <= M; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
    }

    bool ok = false;
    do {
        ok = false;

        for(int i = 1; i <= N; i++) {
            d[i] = false;
        }

        for(int i = 1; i <= N; i++) {
            if(!l[i]) {
                ok |= PairUp(i);
            }
        }
    } while(ok);

    int maxIndepSet = 2 * N - matching;
    cout << maxIndepSet << '\n';

    return 0;
}