Cod sursa(job #2145160)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 27 februarie 2018 10:14:44
Problema Felinare Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int nmax = (1<<13);
vector <int> ls[nmax];
int lft[nmax], rgt[nmax], viz[nmax], K, n, m, i, j, x, y, sol1, stinsst[nmax], stinsdr[nmax];

bool pairup(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 (rgt[y] == 0) {
            rgt[y] = x;
            lft[x] = y;
            return 1;
        }
    }

    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (pairup(rgt[y])) {
            rgt[y] = x;
            lft[x] = y;
            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) {
            stinsdr[y] = 1;
            stinsst[x] = 0;
            aprinde(rgt[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;
        sol1 += pairup(i);
    }
    g << 2*n-sol1 << '\n';

    for (i = 1; i <= n; i++)
        stinsst[i] = (lft[i]!=0);

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

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

    return 0;
}