Cod sursa(job #2887302)

Utilizator rares89_Dumitriu Rares rares89_ Data 9 aprilie 2022 12:45:11
Problema Felinare Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

ifstream fin("felinare.in");
ofstream fout("felinare.out");

int n, m, x, y, st[8200], d[8200], ans;
vector<int> G[8200];
bitset<8200> v;
	
bool cuplaj(int nod) {
    if(v[nod]) {
        return false;
    }
    v[nod] = true;
    for(auto i : G[nod]) {
        if(d[i] == 0) {
            st[nod] = i;
            d[i] = nod;
            return true;
        }
    }
    for(auto i : G[nod]) {
        if(cuplaj(d[i])) {
            st[nod] = i;
            d[i] = nod;
            return true;
        }
    }
    return false;
}

int main() {
    fin >> n >> m;
    for(int i = 1; i <= m; i++) {
        fin >> x >> y;
        G[x].push_back(y);
    }
    fin.close();
    bool gasit = true;
    while(gasit) {
        gasit = false;
        v.reset();
        for(int i = 1; i <= n; i++) {
            if(st[i] == 0) {
                gasit |= cuplaj(i);
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        ans += (st[i] > 0);
    }
    fout << 2 * n - ans << "\n";
    for(int i = 1; i <= n; i++) {
        fout << (st[i] ^ 3) << "\n";
    }
    return 0;
}