Cod sursa(job #2663548)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 26 octombrie 2020 18:33:36
Problema Felinare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 9000;
int n, m, l[nmax], r[nmax], a[nmax], b[nmax], cuplaj;
bool viz[nmax];
vector <int> G[nmax];

bool pairup(int nod){
    if (viz[nod]){
        return false;
    }
    viz[nod] = true;
    for (auto it : G[nod]){
        if (r[it] == 0){
            l[nod] = it;
            r[it] = nod;
            ++cuplaj;
            return true;
        }
    }
    for (auto it : G[nod]){
        if (pairup(r[it])){
            l[nod] = it;
            r[it] = nod;
            return true;
        }
    }
    return false;
}

void dfs(int nod){
    viz[nod] = true;
    a[nod] = 0;
    for (auto it : G[nod]){
        if (viz[r[it]] == 0){
            b[it] = 1;
            dfs(r[it]);
        }
    }
}

int main(){
    fin >> n >> m;
    for (int i = 1; i <= m; ++i){
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    bool ok = true;
    while (ok){
        ok = false;
        memset(viz, 0, sizeof viz);
        for (int i = 1; i <= n; ++i){
            if (l[i] == 0){
                ok |= pairup(i);
            }
        }
    }
    memset(viz, 0, sizeof viz);
    for (int i = 1; i <= n; ++i){
        if (l[i] != 0){
            a[i] = 1;
        }
    }
    for (int i = 1; i <= n; ++i){
        if (l[i] == 0){
            dfs(i);
        }
    }
    fout << 2 * n - cuplaj << "\n";
    for (int i = 1; i <= n; ++i){
        a[i] = 1 - a[i];
        b[i] = 1 - b[i];
        if (a[i] == 0 && b[i] == 0){
            fout << 0 << "\n";
        }
        else if (a[i] == 0 && b[i] == 1){
            fout << 2 << "\n";
        }
        else if (a[i] == 1 && b[i] == 0){
            fout << 1 << "\n";
        }
        else{
            fout << 3 << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}