Cod sursa(job #2887311)

Utilizator rares89_Dumitriu Rares rares89_ Data 9 aprilie 2022 12:50:48
Problema Felinare Scor 43
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 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], sol[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] = 1;
            d[i] = nod;
            return true;
        }
    }
    for(auto i : G[nod]) {
        if(cuplaj(d[i])) {
            st[nod] = 1;
            d[i] = nod;
            return true;
        }
    }
    return false;
}

void dfs(int nod) {
    v[nod] = true;
    for(auto i : G[nod]) {
        if(!(st[i] & 2)) {
			st[i] |= 2;
			st[d[i]] ^= 1;
			if(!v[d[i]]) {
				dfs(d[i]);
			}
		}
    }
}

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++) {
		if(!(st[i] & 1)) {
			dfs(i);
		}
    }
    for(int i = 1; i <= n; i++) {
        fout << (st[i] ^ 3) << "\n";
    }
    return 0;
}