Cod sursa(job #1961831)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 11 aprilie 2017 13:19:57
Problema Felinare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("felinare.in");
ofstream g("felinare.out");

const int nmax = 9000;
int x, y, n, m, cnt, K, i;
vector <int> ls[nmax];
int viz[nmax], st[nmax], dr[nmax];
bool stinsst[nmax], stinsdr[nmax];

bool pereche(int x) {
    if (viz[x]==K) return 0;
    int l = ls[x].size(), i, y;
    viz[x] = K;
    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (dr[y] == 0 || pereche(dr[y])) {
            st[x] = y;
            dr[y] = x;
            return 1;
        }
    }
    return 0;
}

void aprinde(int x) {
    int l = ls[x].size(), i, y;
    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (stinsdr[y] == 0) {
            stinsst[dr[y]] = 0;
            stinsdr[y] = 1;
            aprinde(dr[y]);
        }
    }
}

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y;
        ls[x].push_back(y);
    }
    for (i = 1; i <= n; i++) {
        K = i;
        cnt += pereche(i);
    }

    g << 2*n - cnt << '\n';
    for (i = 1; i <= n; i++)
        stinsst[i] = (st[i] != 0);

    for (i = 1; i <= n; i++)
        if (st[i] == 0)
            aprinde(i);

    for (i = 1; i <= n; i++) {
        cnt = 0;
        if (stinsst[i]==0) cnt++;
        if (stinsdr[i]==0) cnt+=2;
        g << cnt << '\n';
    }

    return 0;
}